つばろぐ

主に C#, .NET, Azure の備忘録です。たまに日記。

wgetでファイルの更新チェックを行うにはNオプション(timestamping)を使う

先日合格した AZ-104 試験の勉強の中で、こちらの Microsoft Learn を進めていました。

learn.microsoft.com

このテキストの中で wget -N コマンドが登場しました。

wget -N https://raw.githubusercontent.com/MicrosoftDocs/mslearn-secure-and-isolate-with-nsg-and-service-endpoints/master/cloud-init.yml && \
az vm create \
    --resource-group $rg \
    --name AppServer \
    --vnet-name ERP-servers \
    --subnet Applications \
    --nsg ERP-SERVERS-NSG \
    --image UbuntuLTS \
    --size Standard_DS1_v2 \
     --generate-ssh-keys \
    --admin-username azureuser \
    --custom-data cloud-init.yml \
    --no-wait \
    --admin-password <password>

-N オプションの内容が分からなかったので、改めてヘルプを見てみました。

$ wget -h | grep -- -N
  -N,  --timestamping              don't re-retrieve files unless newer than
  -m,  --mirror                    shortcut for -N -r -l inf --no-remove-listing

動作としては、ダウンロード対象のファイルのタイムスタンプより、ローカルの同ファイルのタイムスタンプが新しい場合にはダウンロードしない、というもののようです。

動作確認をしてみます。検証対象のファイルは最終更新から3年以上経過しています。

# 初回ダウンロード
$ wget -N https://raw.githubusercontent.com/MicrosoftDocs/mslearn-secure-and-isolate-with-nsg-and-service-endpoints/master/cloud-init.yml
--2023-02-05 00:28:25--  https://raw.githubusercontent.com/MicrosoftDocs/mslearn-secure-and-isolate-with-nsg-and-service-endpoints/master/cloud-init.yml
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.111.133, 185.199.108.133, 185.199.109.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.111.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 58 [text/plain]
Saving to: ‘cloud-init.yml’

cloud-init.yml                         100%[===========================================================================>]      58  --.-KB/s    in 0s

Last-modified header missing -- time-stamps turned off.
2023-02-05 00:28:25 (7.00 MB/s) - ‘cloud-init.yml’ saved [58/58]

# タイムスタンプは「00:28」
$ ls -al cloud-init.yml
-rw-r--r-- 1 yuta yuta 58 Feb  5 00:28 cloud-init.yml

# 数分後
$ date
Sun Feb  5 00:31:51 JST 2023

# ローカルのファイルのほうが新しいためダウンロードしない
$ wget -N https://raw.githubusercontent.com/MicrosoftDocs/mslearn-secure-and-isolate-with-nsg-and-service-endpoints/master/cloud-init.yml
--2023-02-05 00:31:54--  https://raw.githubusercontent.com/MicrosoftDocs/mslearn-secure-and-isolate-with-nsg-and-service-endpoints/master/cloud-init.yml
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.111.133, 185.199.108.133, 185.199.109.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.111.133|:443... connected.
HTTP request sent, awaiting response... 416 Range Not Satisfiable

    The file is already fully retrieved; nothing to do.

関連

tsubalog.hatenablog.com