Qiitaに投稿した記事をバックアップしてみました。
対象
こちらの投稿を対象にやってみます。
まるでDocker Desktop!!Rancher Desktopの登場です
Markdownファイルを取得
URLの末尾に.md
をつけるとMarkdown形式のファイルが取得できます。
wget /14d4099023981dcf4fd2.md
画像URLの書き換え
Qiitaに投稿した記事中の画像はimages/
のURLで保存されています。画像もバックアップしたいので以下の手順を実行します。
該当の行を取得
Markdown記法で画像の貼り付けは
ですので、まずは












キャプションまでの文字列をカット
続いて、キャプション部分をカットします。cut
コマンドを使います。
まずは]
までの文字列をカットし、その後(
をカットします。(キャプション中に(
が含まれる場合があるので)
最後に)
以降をカットします。
cat 14d4099023981dcf4fd2.md | grep '\!\[' \
| cut -d ']' -f 2 \
| cut -d '(' -f 2 \
| cut -d ')' -f 1
images/0/41574/6b84ba3b-1199-fb81-cd97-2e386a6e5a59.png
images/0/41574/b439a837-e23e-3822-b8f6-484b32321d95.png
images/0/41574/c4471ec9-b86e-415d-4acc-b8e3bb8407ef.png
images/0/41574/733ff381-a7dc-1c1e-01b3-7b869bf43a22.png
images/0/41574/6dd1be6e-0e8d-fc99-7275-6bc4fa0966a4.png
images/0/41574/f00185bf-797b-b6af-2403-9c281d55fe55.png
images/0/41574/c393cdd9-848c-a628-cd99-1bf5e866bf1e.png
images/0/41574/11025965-e17f-8236-14c8-2bb602473739.png
images/0/41574/d09c196f-188f-c199-820d-8a67e7f42db1.png
images/0/41574/863ee573-fe1f-0727-1062-10c8c898ecb8.png
images/0/41574/c754aedc-15a7-4f4e-d655-6d726582e17b.png
images/0/41574/f47b39e2-a97a-bb42-c69e-f71c00e907f4.png
images/0/41574/2f00dcdc-8d5e-6a98-c872-7ad2c739d5fe.png
これをファイルに出力します。
cat 14d4099023981dcf4fd2.md | grep '\!\[' \
| cut -d ']' -f 2 \
| cut -d '(' -f 2 \
| cut -d ')' -f 1 > image-list.txt
画像を取得する
wgetで画像を取得し、images
フォルダー配下に出力します。
wget -i image-list.txt -x -nH -P images
上記オプションの場合は以下のようなディレクトリ構成で出力されます。
tree images
images
└── 0
└── 41574
├── 11025965-e17f-8236-14c8-2bb602473739.png
├── 2f00dcdc-8d5e-6a98-c872-7ad2c739d5fe.png
├── 6b84ba3b-1199-fb81-cd97-2e386a6e5a59.png
├── 6dd1be6e-0e8d-fc99-7275-6bc4fa0966a4.png
├── 733ff381-a7dc-1c1e-01b3-7b869bf43a22.png
├── 863ee573-fe1f-0727-1062-10c8c898ecb8.png
├── b439a837-e23e-3822-b8f6-484b32321d95.png
├── c393cdd9-848c-a628-cd99-1bf5e866bf1e.png
├── c4471ec9-b86e-415d-4acc-b8e3bb8407ef.png
├── c754aedc-15a7-4f4e-d655-6d726582e17b.png
├── d09c196f-188f-c199-820d-8a67e7f42db1.png
├── f00185bf-797b-b6af-2403-9c281d55fe55.png
└── f47b39e2-a97a-bb42-c69e-f71c00e907f4.png
2 directories, 13 files
Markdownファイルが参照している画像のパスを置換
sedでパスを置換します。
sed -i -e 's/https:\/\/qiita-image-store.s3.ap-northeast-1.amazonaws.com/images/g' 14d4099023981dcf4fd2.md
完成しました。
まとめ
まとめるとこうなります
export item_id=14d4099023981dcf4fd2
wget /14d4099023981dcf4fd2.md
cat ${item_id}.md | \
grep '\!\[' | \
cut -d ']' -f 2 | \
cut -d '(' -f 2 | cut -d ')' -f 1 > image-list.txt
wget -i image-list.txt -x -nH -P images
sed -i -e 's/https:\/\/qiita-image-store.s3.ap-northeast-1.amazonaws.com/images/g' ${item_id}.md
rm image-list.txt