Ubuntu Weekly Recipe

第766回高度なことが簡単にできる多機能バックアップツール、Restic[後編]

今回は前回に続いてResticを紹介します。必ず前回を先に読んでください。

Rclone経由でWebDAVサーバーにバックアップ

Rclone第725回で紹介した、さまざまなクラウドストレージサービスに接続するフロントエンドです。ResticはRcloneをバックエンドにする機能があるため、Rcloneが対応しているクラウドストレージサービスであればResticのバックアップ先となります。

Rcloneが対応しているクラウドストレージサービスのリストを見てみると、対応していないサービス[1]はもう諦めてもいいかと思えるほどの充実ぶりです。

第725回と重複しますが、試しにTeraCLOUDあらためInfiniCLOUDに接続してみましょう。無料で20GBまで使えるので、練習には充分な容量ですし、気に入ればそのまま契約するといいでしょう。

RcloneもGo言語で書かれており、オープンソースでマルチプラットフォームです。やはりリポジトリにもありますがバージョンが古いため、公式サイトからインストールから行います。次のコマンドを実行してください。

$ curl https://rclone.org/install.sh | sudo bash

早速RcloneでInfiniCLOUDに接続する設定をします。詳しい説明は第725回を参照するとして、設定項目としては以下のとおりです。

$ rclone config
2023/05/14 14:46:39 NOTICE: Config file "/home/ikuya/.config/rclone/rclone.conf" not found - using defaults
No remotes found, make a new one?
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n

Enter name for new remote.
name> InfiniCLOUD

Option Storage.
Type of storage to configure.
Choose a number from below, or type in your own value.
 1 / 1Fichier
   \ (fichier)
(略)
46 / WebDAV
   \ (webdav)
47 / Yandex Disk
   \ (yandex)
48 / Zoho
   \ (zoho)
49 / premiumize.me
   \ (premiumizeme)
50 / seafile
   \ (seafile)
Storage> 46

Option url.
URL of http host to connect to.
E.g. https://example.com.
Enter a value.
url> (略)

Option vendor.
Name of the WebDAV site/service/software you are using.
Choose a number from below, or type in your own value.
Press Enter to leave empty.
 1 / Nextcloud
   \ (nextcloud)
 2 / Owncloud
   \ (owncloud)
 3 / Sharepoint Online, authenticated by Microsoft account
   \ (sharepoint)
 4 / Sharepoint with NTLM authentication, usually self-hosted or on-premises
   \ (sharepoint-ntlm)
 5 / Other site/service or software 
   \ (other)
vendor> 5

Option user.
User name.
In case NTLM authentication is used, the username should be in the format 'Domain\User'.
Enter a value. Press Enter to leave empty.
user> (略)

Option pass.                                                                   Password.
Choose an alternative below. Press Enter for the default (n).
y) Yes, type in my own password
g) Generate random password
n) No, leave this optional password blank (default)
y/g/n> y
Enter the password:
password:
Confirm the password:
password:

Option bearer_token.
Bearer token instead of user/pass (e.g. a Macaroon).
Enter a value. Press Enter to leave empty.
bearer_token> 

Edit advanced config?
y) Yes
n) No (default)
y/n> 

Configuration complete.
Options:
- type: webdav
- url: (略)
- vendor: other
- user: (略)
- pass: *** ENCRYPTED ***
Keep this "InfiniCLOUD" remote?
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> 

Current remotes:

Name                 Type
====                 ====
InfiniCLOUD          webdav

e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q> q

事前にRcloneで設定したクラウドストレージサーバーに接続できるか試してみましょう。今回の例だと次のコマンドを実行します。

$ rclone ls InfiniCLOUD:

エラーが出なければ問題ありません。

では登録したクラウドストレージをResticに登録します。次のコマンドを実行してください。

$ restic -r rclone:InfiniCLOUD:restic init

バックアップを実行します。

$ restic -r rclone:InfiniCLOUD:restic --verbose backup /srv/samba/

バックアップに関しては、前回紹介した2パターンと前述の合計3パターンを踏まえておけばおおむね対応できるでしょう。

リストア

バックアップの次に気にすることはリストアです。Resticには通常のリストア方法もあるのですが、今回は紹介しません。

というのも、Resticにはバックアップ先をマウントする機能があるので、こちらを紹介します。マウント[2]を行ってしまえば、あとはrsyncなりなんなりで元通りに復元すればいいので、ツール(今回の例だとrsync)の使い方に精通していれば新たにResticでの作法を覚える必要はありません。確実なリストアができるようにrsyncの使い方をマスターすると、いくらでも応用できるところがあります。

マウント方法も簡単です。今回は例としてMinIOに作成したバックアップ先(リポジトリ)をマウントしてみます。次のコマンドを実行してください。

$ mkdir /tmp/mnt
$ restic -r s3:http://hamana.local:9000/restic mount /tmp/mnt/
enter password for repository: 
repository 2c8b0232 opened (version 2, compression level auto)
Now serving the repository at /tmp/mnt/
Use another terminal or tool to browse the contents of this folder.
When finished, quit with Ctrl-c here or umount the mountpoint.

ユーザー権限でマウントするため、マウント先となる/tmp/mntを作成し、Resticのマウントするコマンドを実行しています。

あとはローカルのフォルダーとして扱えるので、ここから復元します。例としてlsコマンドを実行してみます。

$ ls /tmp/mnt/snapshots/
2023-05-14T14:24:30+09:00  2023-05-14T14:25:51+09:00  2023-05-14T14:40:59+09:00  2023-05-14T15:46:09+09:00  latest

前述のように、あとはrsyncでリストアするのが簡単で確実でしょう。ちょっとにわかには信じがたいお手軽さです。

スナップショットの削除

スナップショットはどんどん増えていくので、不要なものは適宜削除したほうがいいでしょう。まずは現在のスナップショットを確認してみます。次のコマンドを実行してください。

$ restic -r s3:http://hamana.local:9000/restic snapshots
enter password for repository: 
repository 2c8b0232 opened (version 2, compression level auto)
ID        Time                 Host        Tags        Paths
-----------------------------------------------------------------
6f0f007a  2023-05-14 14:24:30  hamana                  /srv/samba
47ef78b2  2023-05-14 14:25:51  hamana                  /srv/samba
62384e6b  2023-05-14 14:40:59  hamana                  /srv/samba
63369c03  2023-05-14 15:46:09  hamana                  /srv/samba
-----------------------------------------------------------------
4 snapshots

4つあることがわかります。うち2つを消してみましょう。まずは問題ないかを確認します。次のコマンドを実行してください。

$ restic -r s3:http://hamana.local:9000/restic forget --keep-last 2 --dry-run --prune
enter password for repository: 
repository 2c8b0232 opened (version 2, compression level auto)
Applying Policy: keep 2 latest snapshots
keep 2 snapshots:
ID        Time                 Host        Tags        Reasons        Paths
--------------------------------------------------------------------------------
62384e6b  2023-05-14 14:40:59  hamana                  last snapshot  /srv/samba
63369c03  2023-05-14 15:46:09  hamana                  last snapshot  /srv/samba
--------------------------------------------------------------------------------
2 snapshots

remove 2 snapshots:
ID        Time                 Host        Tags        Paths
-----------------------------------------------------------------
6f0f007a  2023-05-14 14:24:30  hamana                  /srv/samba
47ef78b2  2023-05-14 14:25:51  hamana                  /srv/samba
-----------------------------------------------------------------
2 snapshots

Would have removed the following snapshots:
{47ef78b2 6f0f007a}

--dry-runオプションをつけると確認できます。特に問題ないので、実際に削除します。次のコマンドを実行してください。

$ restic -r s3:http://hamana.local:9000/restic forget --keep-last 2 --prune
enter password for repository: 
repository 2c8b0232 opened (version 2, compression level auto)
Applying Policy: keep 2 latest snapshots
keep 2 snapshots:
ID        Time                 Host        Tags        Reasons        Paths
--------------------------------------------------------------------------------
62384e6b  2023-05-14 14:40:59  hamana                  last snapshot  /srv/samba
63369c03  2023-05-14 15:46:09  hamana                  last snapshot  /srv/samba
--------------------------------------------------------------------------------
2 snapshots

remove 2 snapshots:
ID        Time                 Host        Tags        Paths
-----------------------------------------------------------------
6f0f007a  2023-05-14 14:24:30  hamana                  /srv/samba
47ef78b2  2023-05-14 14:25:51  hamana                  /srv/samba
-----------------------------------------------------------------
2 snapshots

[0:00] 100.00%  2 / 2 files deleted

スナップショットが2つに減りました。

スクリプト化

Resticのコマンドを実行してみるとわかりますが、常にパスワードを入力しないといけません。しかしそれはスクリプトにして自動バックアップできないことを意味しません。以下にバックアップのサンプルスクリプトを提示します。

$ cat backup.sh 
#!/bin/sh

export AWS_ACCESS_KEY_ID=unique-minioadmin
export AWS_SECRET_ACCESS_KEY=secure-minioadmin-password
export RESTIC_PASSWORD=gihyojp-restic
restic -r s3:http://hamana.local:9000/restic --verbose backup /srv/samba/

このように、環境変数を指定すればパスワードの入力を省くことができます。

実はResticはたくさんの環境変数が用意されており、およそ困ることはありません。存分にバックアップを自動化しましょう。

終わりに

Resticは素晴らしいドキュメントがあり、使用前に一読することをお勧めします。サンプルが豊富なので、英語が苦手でも読み込めるでしょう。

バックアップやリストアが簡単であるという事実はとても重要です。そして上記のドキュメントをよく読むとリポジトリをコピーする機能があり、それはすなわち同じバックアップを複数箇所に保存できるということです。今までさんざん目にしてきた3-2-1ルールや[3]、場合によってはそれ以上の対策が必要であっても、Resticであれば簡単にいくらでもバックアップ先を増やせます。

Resticをうまく活用して、万全なバックアップ体制を整えてください。

おすすめ記事

記事・ニュース一覧