前提と注意事項
- Ubuntu 22.04 を使用します。
- Kopia は、CLI 版と GUI 版がありますが、この記事では CLI 版を使用します。
Kopia について
Kopia は、スナップショットに対応したオープンソースのバックアップツールであり、スナップショットの暗号化対応、高速であることが特徴です。 また、Linux だけでなく、Windows、macOS にも対応しています。 以下が Kopia の公式リポジトリと公式サイトです。
KopiaKopia is a fast and secure open-source backup/restore tool that allows you to create encrypted snapshots of your data and save the snapshots to remote or cloud storage of your choice, to network-attached storage or server, or locally on your machine. github.com KOPIAFast and Secure Open-Source Backup Softwarekopia.ioこの記事のゴール
Ubuntu で指定したディレクトリのスナップショットを毎日決まった時間に自動取得する環境を構築します。
最終的に、以下のように毎日3:30に自動的に/mnt/targetという指定したディレクトリのスナップショットを作成します。
$ kopia snapshot list
userename@mypc:/mnt/target
2023-11-28 03:30:02 JST kd9bad263afb71e970de45f57aff09bfe 39.1 GB drwxr-xr-x files:62068 dirs:10278 (latest-3,daily-3)
2023-11-29 03:30:02 JST kbd726e3af787080df976b2ad18283e46 39.1 GB drwxr-xr-x files:62070 dirs:10278 (latest-2,hourly-2,daily-2)
2023-11-30 03:30:02 JST kce952df4ac2ab658e3a2bfbc36a62458 39.1 GB drwxr-xr-x files:62072 dirs:10278 (latest-1,hourly-1,daily-1,weekly-1,monthly-1,annual-1)Kopia をインストールする
Kopia の公式ドキュメントに従って Ubuntu へのインストールを行います。
以下を実行して Kopia の GPG キーを取得、保存します。
$ curl -s https://kopia.io/signing-key | sudo gpg --dearmor -o /etc/apt/keyrings/kopia-keyring.gpgKopia をaptコマンドでインストールできるよう、パッケージリストに追加します。
$ echo "deb [signed-by=/etc/apt/keyrings/kopia-keyring.gpg] http://packages.kopia.io/apt/ stable main" | sudo tee /etc/apt/sources.list.d/kopia.list
$ sudo apt update以上でapt経由でインストールする準備が整いました。以下を実行して Kopia をインストールします。
$ sudo apt install kopia以上で Kopia のインストールが完了です。
リポジトリを作成する
Kopia では、スナップショットを保存・管理するためのディレクトリを「リポジトリ」と呼びます。このリポジトリを最初に作成して、スナップショットの保存先として指定します。
ローカルディレクトリをリポジトリとする
以下のようにkopia repository createを使ってリポジトリを作成します。ここでは、テスト用に/tmp/my-backup-repositoryをリポジトリとして作成します。
$ kopia repository create filesystem --path /tmp/my-backup-repository上記を実行すると、以下のようにパスワードを聞かれるので好きなパスワードを入力して Enter キーを押下します。
$ kopia repository create filesystem --path /tmp/my-backup-repository
Enter password to create new repository:
Re-enter password for verification:2 回パスワードを入力して Enter を押下すると、以下のように表示されます。
$ kopia repository create filesystem --path /tmp/my-backup-repository
Enter password to create new repository:
Re-enter password for verification:
Initializing repository with:
block hash: BLAKE2B-256-128
encryption: AES256-GCM-HMAC-SHA256
splitter: DYNAMIC-4M-BUZHASH
Connected to repository.
NOTICE: Kopia will check for updates on GitHub every 7 days, starting 24 hours after first use.
To disable this behavior, set environment variable KOPIA_CHECK_FOR_UPDATES=false
Alternatively you can remove the file "/home/username/.config/kopia/repository.config.update-info.json".
Retention:
Annual snapshots: 3 (defined for this target)
Monthly snapshots: 24 (defined for this target)
Weekly snapshots: 4 (defined for this target)
Daily snapshots: 7 (defined for this target)
Hourly snapshots: 48 (defined for this target)
Latest snapshots: 10 (defined for this target)
Ignore identical snapshots: false (defined for this target)
Compression disabled.
To find more information about default policy run 'kopia policy get'.
To change the policy use 'kopia policy set' command.
NOTE: Kopia will perform quick maintenance of the repository automatically every 1h0m0s
and full maintenance every 24h0m0s when running as username@mypc.
See https://kopia.io/docs/advanced/maintenance/ for more information.
NOTE: To validate that your provider is compatible with Kopia, please run:
$ kopia repository validate-provider
以上で Kopia のリポジトリを作成できました。 続いて作成したリポジトリに接続してスナップショットを作成します。 なお、パスワードを紛失した場合、スナップショットを復元することは不可能であるという旨の内容が公式ドキュメンントにも記載されています。パスワードを紛失しないよう保存してください。
リポジトリに接続する
作成したリポジトリを使用するために、以下のコマンドで接続します。
$ kopia repository connect filesystem --path /tmp/my-backup-repository次にスナップショットを作成します。
スナップショットを作成する
以下のコマンドを実行することでスナップショットを作成します。以下では、$HOME/workspaceをスナップショットの対象としています。
$ kopia snapshot create $HOME/workspace/上記を実行すると、以下のように表示されます。
$ kopia snapshot create $HOME/workspace/
Snapshotting username@mypc:/home/username/workspace ...
/ 1 hashing, 19382 hashed (388.1 MB), 0 cached (0 B), uploaded 357.9 MB, estimated 802.2 MB (48.4%) 4s le - 1 hashing, 20113 hashed (392.4 MB), 0 cached (0 B), uploaded 379 MB, estimated 802.2 MB (48.9%) 3s left | 1 hashing, 23530 hashed (424.7 MB), 0 cached (0 B), uploaded 400.1 MB, estimated 802.2 MB (52.9%) 4s le / 2 hashing, 24592 hashed (446.7 MB), 0 cached (0 B), uploaded 421.8 MB, estimated 802.2 MB (55.7%) 3s le - 1 hashing, 26902 hashed (456.7 MB), 0 cached (0 B), uploaded 421.8 MB, estimated 802.2 MB (56.9%) 3s le \ 1 hashing, 27924 hashed (482.8 MB), 0 cached (0 B), uploaded 442.9 MB, estimated 802.2 MB (60.2%) 3s le | 1 hashing, 27924 hashed (510.2 MB), 0 cached (0 B), uploaded 493 MB, estimated 802.2 MB (63.6%) 2s left - 1 hashing, 27924 hashed (580.7 MB), 0 cached (0 B), uploaded 565.4 MB, estimated 802.2 MB (72.4%) 2s le \ 2 hashing, 29404 hashed (597.9 MB), 0 cached (0 B), uploaded 565.4 MB, estimated 802.2 MB (74.5%) 2s le | 2 hashing, 31026 hashed (626 MB), 0 cached (0 B), uploaded 586.8 MB, estimated 802.2 MB (78.0%) 1s left / 1 hashing, 33750 hashed (633.8 MB), 0 cached (0 B), uploaded 586.8 MB, estimated 802.2 MB (79.0%) 1s le - 2 hashing, 35205 hashed (670.1 MB), 0 cached (0 B), uploaded 614 MB, estimated 802.2 MB (83.5%) 1s left \ 2 hashing, 35948 hashed (699.1 MB), 0 cached (0 B), uploaded 637.8 MB, estimated 802.2 MB (87.1%) 0s le | 3 hashing, 37444 hashed (759 MB), 0 cached (0 B), uploaded 680.8 MB, estimated 802.2 MB (94.6%) 0s left - 1 hashing, 40854 hashed (798.2 MB), 0 cached (0 B), uploaded 724.1 MB, estimated 802.2 MB (99.5%) 0s le * 0 hashing, 41052 hashed (802.4 MB), 0 cached (0 B), uploaded 724.1 MB, estimated 802.2 MB (100.0%) 0s left
Created snapshot with root k2ce13b67ae2b49b3d5e6390a0c16d7fe and ID 5d6432646f6069d81a602351938b6f7e in 9s
Running full maintenance...
Looking for active contents...
Looking for unreferenced contents...
GC found 0 unused contents (0 B)
GC found 0 unused contents that are too recent to delete (0 B)
GC found 41360 in-use contents (731.4 MB)
GC found 2 in-use system-contents (1.2 KB)
Rewriting contents from short packs...
Total bytes rewritten 0 B
Not enough time has passed since previous successful Snapshot GC. Will try again next time.
Skipping blob deletion because not enough time has passed yet (59m59s left).
Cleaned up 0 logs.
Cleaning up old index blobs which have already been compacted...
Finished full maintenance.これで最初のスナップショットを作成できました。
再度上記のkopia snapshot create実行すると、最新のスナップショットを作成してくれます。
$ kopia snapshot create $HOME/workspace/
Snapshotting username@mypc:/home/username/workspace ...
* 0 hashing, 0 hashed (0 B), 41052 cached (802.4 MB), uploaded 229 B, estimated 802.2 MB (100.0%) 0s left
Created snapshot with root k6cbfc8676bb6308f2944890631b8d75f and ID 65f8ada4bb9bba31e697b0544695a9ed in 1sスナップショットを確認する
以下のようにkopia snapshot listを使うことで作成済みのスナップショット一覧を確認できます。現在は 2 回実行したため、2つのスナップショットが表示されます。
$ kopia snapshot list
username@mypc:/home/username/workspace
2023-11-27 10:56:17 JST k2ce13b67ae2b49b3d5e6390a0c16d7fe 802.2 MB drwxrwxr-x files:37445 dirs:9448 (latest-2,hourly-2)
2023-11-27 11:00:27 JST k6cbfc8676bb6308f2944890631b8d75f 802.2 MB drwxrwxr-x files:37445 dirs:9448 (latest-1,hourly-1,daily-1,weekly-1,monthly-1,annual-1)上記に表示されている ID を指定してkopia showを使うと、そのバックアップに含まれるディレクトリ情報を確認できます。
$ kopia show k6cbfc8676bb6308f2944890631b8d75f
{
"stream": "kopia:directory",
"entries": [
{
"name": "Documents",
"type": "d",
"mode": "0775",
"mtime": "2023-10-25T10:01:49.262954551Z",
"uid": 1000,
"gid": 1000,
"obj": "k89f5a98c58b9cc30a361cafc443f4ef2",
"summ": {
"size": 802226487,
"files": 37434,
"symlinks": 0,
"dirs": 9443,
"maxTime": "2023-10-25T10:03:23.623971355Z",
"numFailed": 0
}
},
{
"name": "Photos",
"type": "d",
"mode": "0775",
"mtime": "2023-10-25T03:06:16.726345192Z",
"uid": 1000,
"gid": 1000,
"obj": "k637b1b46714a536be6a84e3022b61133",
"summ": {
"size": 0,
"files": 0,
"symlinks": 0,
"dirs": 1,
"maxTime": "2023-10-25T03:06:16.726345192Z",
"numFailed": 0
}
},
{
"name": "Videos",
"type": "d",
"mode": "0775",
"mtime": "2023-11-20T10:17:07.661289024Z",
"uid": 1000,
"gid": 1000,
"obj": "k35e420847a4db75d218d9d9c5d64f64e",
"summ": {
"size": 1167,
"files": 11,
"symlinks": 0,
"dirs": 3,
"maxTime": "2023-11-27T01:59:51.239416748Z",
"numFailed": 0
}
}
],
"summary": {
"size": 802227654,
"files": 37445,
"symlinks": 0,
"dirs": 9448,
"maxTime": "2023-11-27T01:59:51.239416748Z",
"numFailed": 0
}
}スナップショットのファイルを開く
Kopia では、作成したスナップショットを指定したパスにマウントすることでそのスナップショットの中身を開くことができます。
例えば、ここではスナップショットのマウント先として/tmp/my-mount-pointを作成して、そこにk6cbfc8676bb6308f2944890631b8d75fをマウントしてみます。
$ mkdir /tmp/my-mount-point
$ kopia mount k6cbfc8676bb6308f2944890631b8d75f /tmp/my-mount-pointマウントすると以下のように表示されます。
$ kopia mount k6cbfc8676bb6308f2944890631b8d75f /tmp/my-mount-point
Mounted 'k6cbfc8676bb6308f2944890631b8d75f' on /tmp/my-mount-point
Press Ctrl-C to unmount.上記の状態で別のターミナルを開いて/tmp/my-mount-pointを開くと、スナップショットの中身を確認したり、ファイルをコピーしたりなど通常のディレクトリとして操作できます。
$ cd /tmp/my-mount-point
$ ls確認や操作が完了したら、Ctrl + Cでアンマウントできます。
$ kopia mount k6cbfc8676bb6308f2944890631b8d75f /tmp/my-mount-point
Mounted 'k6cbfc8676bb6308f2944890631b8d75f' on /tmp/my-mount-point
Press Ctrl-C to unmount.
^CUnmounting...
Unmounted.ポリシーを作成する
Kopia では、以下の設定を編集でき、これらの編集を「ポリシー」と呼んでいます。
- スナップショットに含めないファイルの種類
- スナップショットを圧縮するか
以下のようにpolicy showコマンドで現在のポリシーを確認できます。
$ kopia policy show --global
Policy for (global):
Retention:
Annual snapshots: 3 (defined for this target)
Monthly snapshots: 24 (defined for this target)
Weekly snapshots: 4 (defined for this target)
Daily snapshots: 7 (defined for this target)
Hourly snapshots: 48 (defined for this target)
Latest snapshots: 10 (defined for this target)
Ignore identical snapshots: false (defined for this target)
Files policy:
Ignore cache directories: true (defined for this target)
No ignore rules:
Read ignore rules from files: (defined for this target)
.kopiaignore
Scan one filesystem only: false (defined for this target)
Error handling policy:
Ignore file read errors: false (defined for this target)
Ignore directory read errors: false (defined for this target)
Ignore unknown types: true (defined for this target)
Scheduling policy:
Scheduled snapshots:
None.
Manual snapshot: false (defined for this target)
Uploads:
Max parallel snapshots (server/UI): 1 (defined for this target)
Max parallel file reads: - (defined for this target)
Parallel upload above size: 2.1 GB (defined for this target)
Compression disabled.
No actions defined.
Logging details (0-none, 10-maximum):
Directory snapshotted: 5 (defined for this target)
Directory ignored: 5 (defined for this target)
Entry snapshotted: 0 (defined for this target)
Entry ignored: 5 (defined for this target)
Entry cache hit: 0 (defined for this target)
Entry cache miss: 0 (defined for this target)特定のディレクトリのポリシーを確認するには、kopia policy showの後にディレクトリのパスを指定します。
例えば、~/workspaceのポリシーを確認するには以下のようにします。
$ kopia policy show ~/workspace除外するファイル、ディレクトリを指定する
以下のように--add-ignore ディレクトリパスを使うことで、そのディレクトリをスナップショットに含めず無視することができます。
$ kopia policy set --add-ignore node_modules/ ~/workspace実行結果は以下です。
$ kopia policy set --add-ignore node_modules/ ~/workspace
Setting policy for username@mypc:/home/username/workspace
- adding "node_modules/" to "ignore rules"もしくは、以下のように、.kopiaignoreファイルをスナップショット作成の対象としていディレクトリの直下に作成しおくことで除外するファイルを指定できます。
# Ignoring all files that end with ".dat"
*.dat
# Ignoring all files and folders within the "thesis/logs" directory
/logs/*
# Ignoring "tmp.db" files within the whole directory
tmp.db.kopiaignoreの書き方は以下の公式ドキュメントに詳しく記載されています。
cron で定期実行する
以下のように、crontabを使って Kopia によるスナップショット作成を自動化します。
$ crontab -e以下のようにします。
30 3 * * * /usr/bin/kopia snapshot create $HOME/workspace/これで毎日 3 時 30 分に Kopia がスナップショットを作成します。
まとめ
Ubuntu で Kopia と cron を使って指定したディレクトリのスナップショットを自動作成する手順を解説しました。 今回はリポジトリの作成先をローカルのディレクトリにしましたが、ローカルではなくマウントしている NAS の共有ディレクトリや、クラウドストレージなどにリポジトリを作成することもできます。 そうすれば毎日定期的にスナップショットをローカル以外にも作成することができます。