LoginSignup
18
20

More than 5 years have passed since last update.

差分バックアップツール「borg」を試してみた

Last updated at Posted at 2017-10-16

borgの検証

背景

差分バックアップできるツールを探していたところ、
シンプルなborgというバックアップツールを紹介してもらった。
動画を見るとすごくシンプルで使いやすそうだったのめ検証してみた。

本家

動画あり:https://borgbackup.readthedocs.io/en/stable/index.html
クイックスタート:https://borgbackup.readthedocs.io/en/stable/quickstart.html

環境

$ cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)

borgインストール

参考:https://borgbackup.readthedocs.io/en/stable/installation.html

$ wget https://github.com/borgbackup/borg/releases/download/1.1.0/borg-linux64
$ sudo cp borg-linux64 /usr/local/bin/borg
$ sudo chown root:root /usr/local/bin/borg
$ sudo chmod 755 /usr/local/bin/borg

バックアップ検証その1 「まずは触ってみる」

チュートリアルを参考にさわってみます。
まずリポジトリを初期化します。
--encryptionオプションで暗号化しています。後から変更することはできないようです。

$ mkdir /var/tmp/borg

# リポジトリを初期化
$ borg init --encryption=repokey /var/tmp/borg/
Enter new passphrase:
Enter same passphrase again:
Do you want your passphrase to be displayed for verification? [yN]:

By default repositories initialized with this version will produce security
errors if written to with an older version (up to and including Borg 1.0.8).

If you want to use these older versions, you can disable the check by running:
borg upgrade --disable-tam '/var/tmp/borg'

See https://borgbackup.readthedocs.io/en/stable/changes.html#pre-1-0-9-manifest-spoofing-vulnerability for details about the security implications.

次にテストデータを作成して、createオプションでバックアップを取得してみます。
::Mondayの部分は、Mondayというアーカイブ名を付けています。
パスフレーズが聞かれるので任意で設定します。

# バックアップ用のテストファイル作成
$ mkdir /var/tmp/test_data
$ touch /var/tmp/test_data/Monday

# バックアップ開始
$ borg create /var/tmp/borg::Monday /var/tmp/test_data
Enter passphrase for key /var/tmp/borg: 

次にテストデータを追加してバックアップします。
--statsで作成したアーカイブの情報を出力します。

# バックアップ用のテストファイル作成
$ touch /var/tmp/test_data/Tuesday

# バックアップ開始
$ borg create --stats /var/tmp/borg::Tuesday /var/tmp/test_data
Enter passphrase for key /var/tmp/borg:
------------------------------------------------------------------------------
Archive name: Tuesday
Archive fingerprint: 7166419cd7aa44664300fc5990f42e2ab666500a685c716c5e3ae9cf72a08829
Time (start): Thu, 2017-10-12 18:00:03
Time (end):   Thu, 2017-10-12 18:00:03
Duration: 0.13 seconds
Number of files: 2
Utilization of max. archive size: 0%
------------------------------------------------------------------------------
                       Original size      Compressed size    Deduplicated size
This archive:                  893 B                735 B                735 B
All archives:                1.62 kB              1.42 kB              1.42 kB

                       Unique chunks         Total chunks
Chunk index:                       4                    4
------------------------------------------------------------------------------

取得したバックアップのリストを表示します。

# リポジトリの中のアーカイブリストを表示
$ borg list /var/tmp/borg
Enter passphrase for key /var/tmp/borg:
Monday                               Thu, 2017-10-12 17:52:36 [15356d50f44fe5f988573e6e1fdada7752b51105a3aeefa07b80bb5f2b8e4f37]
Tuesday                              Thu, 2017-10-12 18:00:03 [7166419cd7aa44664300fc5990f42e2ab666500a685c716c5e3ae9cf72a08829]

# 指定したアーカイブのコンテンツのリストを表示
$ borg list /var/tmp/borg::Monday
Enter passphrase for key /var/tmp/borg:
drwxr-xr-x root root        0 Thu, 2017-10-12 17:48:48 var/tmp/test_data
-rw-r--r-- root root        0 Thu, 2017-10-12 17:48:48 var/tmp/test_data/Monday
$
$ borg list /var/tmp/borg::Tuesday
Enter passphrase for key /var/tmp/borg:
drwxr-xr-x root root        0 Thu, 2017-10-12 17:59:49 var/tmp/test_data
-rw-r--r-- root root        0 Thu, 2017-10-12 17:59:49 var/tmp/test_data/Tuesday
-rw-r--r-- root root        0 Thu, 2017-10-12 17:48:48 var/tmp/test_data/Monday

extractオプションでリストアしてみます。
バックアップは、カレントディレクトリにリストアされます。 ※出力先は指定できない模様

$ borg extract /var/tmp/borg::Monday

$ ll var/tmp/test_data/
total 0
-rw-r--r-- 1 root root 0 Oct 12 18:48 Monday

バックアップ検証その2 「速度の検証」

バックアップ容量を大きくして速度の検証してみます。

前提条件

  • 対象サイズ:約25G
  • 対象ファイル:rpm(yumのリポジトリ)
  • IOがあまり出ないインスタンス上

手順

初期化します。

$ borg init --encryption=repokey /backup
Enter new passphrase:
Enter same passphrase again:
Do you want your passphrase to be displayed for verification? [yN]:

By default repositories initialized with this version will produce security
errors if written to with an older version (up to and including Borg 1.0.8).

If you want to use these older versions, you can disable the check by running:
borg upgrade --disable-tam '/backup'

See https://borgbackup.readthedocs.io/en/stable/changes.html#pre-1-0-9-manifest-spoofing-vulnerability for details about the security implications.

バックアップを取得します。

# バックアップ開始
$ time borg create --progress /backup::201710161545 /path_to_backupdata
Enter passphrase for key /backup:


real    17m6.435s
user    9m23.344s
sys 1m6.268s

約17分かかりました。 

$ ll /backup/
total 2588
-rw------- 1 root root     700 Oct 16 15:40 config
drwx------ 3 root root    4096 Oct 16 15:40 data
-rw------- 1 root root     199 Oct 16 16:03 hints.39
-rw------- 1 root root 2621498 Oct 16 16:03 index.39
-rw------- 1 root root     190 Oct 16 16:03 integrity.39
-rw------- 1 root root      16 Oct 16 16:02 nonce
-rw------- 1 root root      73 Oct 16 15:40 README
$ du -sh /backup/
18G /backup/

バックアップの容量は18GBになっています。
尚、borgはデフォルトでは圧縮しません。
-Cオプションを付けアルゴリズム(lz4, zlib, lzma)を指定することで圧縮してくれます。
次にテストファイル(空ファイル)を追加して、差分バックアップを実行します。

# time borg create --progress /backup::201710161545-1 /path_to_backupdata
Enter passphrase for key /backup:

real    0m17.042s
user    0m12.098s
sys 0m1.089s

約17秒で終わりました。

2017/10/20追記

バックアップ検証その3 「リモートリポジトリの作成」

前提条件

192.168.101.101:バックアップサーバ想定
192.168.101.102:バックアップクライアント想定

ユーザ名とIPアドレスを指定することでリモートサーバにリポジトリが作れるらしい。
またスクリプト化する際は、パスレーズは空にすると良さげです。
バックアップサーバにバックアップ集約したい時に使えそうです。

$ borg init --encryption=repokey root@192.168.101.101:/backup
Enter new passphrase:
Enter same passphrase again:
Do you want your passphrase to be displayed for verification? [yN]:

By default repositories initialized with this version will produce security
errors if written to with an older version (up to and including Borg 1.0.8).

If you want to use these older versions, you can disable the check by running:
borg upgrade --disable-tam 'ssh://root@192.168.101.101/backup'

See https://borgbackup.readthedocs.io/en/stable/changes.html#pre-1-0-9-manifest-spoofing-vulnerability for details about the security implications.
$

バックアップ検証その4 「暗号化キーの作成」

暗号化キーを作成することもできます。
--encryptionkeyfileを指定してみます。
sshでパスレーズなしで接続できる環境で実施しました。

$ borg init --encryption=keyfile root@192.168.101.101:/backup2

Enter new passphrase:
Enter same passphrase again:
Do you want your passphrase to be displayed for verification? [yN]:

By default repositories initialized with this version will produce security
errors if written to with an older version (up to and including Borg 1.0.8).

If you want to use these older versions, you can disable the check by running:
borg upgrade --disable-tam 'ssh://root@192.168.101.101/backup2'

See https://borgbackup.readthedocs.io/en/stable/changes.html#pre-1-0-9-manifest-spoofing-vulnerability for details about the security implications.
$

# ホームディレクトリに鍵が作成されます。この鍵がないとアクセスできません。
$ ll .config/borg/keys/192_168_101_101__backup2
-rw------- 1 root livesen 553 Oct 20 14:24 .config/borg/keys/192_168_101_101__backup2

# バックアップ取得
$ borg create root@192.168.101.101:/backup2::{hostname}-{user}-{now:%Y-%m-%dT%H:%M:%S.%f} /var/tmp/
Enter passphrase for key /root/.config/borg/keys/192_168_101_101__backup2:
# バックアップ先でborg listを実行すると鍵がないと言われる。成功
$ borg list /backup2
No key file for repository /backup2 found in /root/.config/borg/keys.

# バックアップ先に鍵を追加するとborg listを表示できる
$ borg list /backup2
Enter passphrase for key /root/.config/borg/keys/192_168_101_101__backup2:
localhost.vagrant-root-2017-10-20T16:18:56.896347 Fri, 2017-10-20 16:19:04 [8acaf63776474c2810e82ca16ab181879693e5dd292e677d5d4cb35282efaf18]

2017/10/25追記

バックアップ検証その5 「世代管理」

pruneコマンドで世代管理が可能です。
http://borgbackup.readthedocs.io/en/stable/usage/prune.html

# 例)2017/10/26のアーカイブが複数ある場合
$ borg list /backup3
localhost.vagrant-2017-10-25T15:56   Wed, 2017-10-25 15:56:14 [a95236834513e8f832b53601dee3942e29af0ac8cffa0fb120b99d010c545448]
localhost.vagrant-2017-10-26T13:53   Thu, 2017-10-26 13:53:02 [bdf7f37130c5a32c786a726759045bad63c51a275db0d0862bf5b3f5d476f89f]
localhost.vagrant-2017-10-26T13:54   Thu, 2017-10-26 13:54:29 [bceb725ceacbf52de5e7c28cf3bfc727991aa25c9ce7f304a645cb2915cad716]
localhost.vagrant-2017-10-26T14:01   Thu, 2017-10-26 14:01:09 [3d8f0b2928e4d85c7ad833a5071887c8d23331d1785c77e1857f20482c4c8cb0]
localhost.vagrant-2017-10-26T14:02   Thu, 2017-10-26 14:02:06 [6d4fe3c085d66f7f29e9d468a41b5e07894215faa148f78dd5d24c08870995e3]

日ごとに世代管理して7日間残したいとします。
--keep-dailyオプションで指定します。

$ borg prune -v --list --keep-daily=7 root@192.168.101.101:/backup3/
Warning: The repository at location ssh://root@192.168.101.101/backup3 was previously located at ssh://192.168.101.101/backup3
Do you want to continue? [yN] y
Keeping archive: localhost.vagrant-2017-10-26T14:02   Thu, 2017-10-26 14:02:06 [6d4fe3c085d66f7f29e9d468a41b5e07894215faa148f78dd5d24c08870995e3]
Pruning archive: localhost.vagrant-2017-10-26T14:01   Thu, 2017-10-26 14:01:09 [3d8f0b2928e4d85c7ad833a5071887c8d23331d1785c77e1857f20482c4c8cb0] (1/3)
Pruning archive: localhost.vagrant-2017-10-26T13:54   Thu, 2017-10-26 13:54:29 [bceb725ceacbf52de5e7c28cf3bfc727991aa25c9ce7f304a645cb2915cad716] (2/3)
Pruning archive: localhost.vagrant-2017-10-26T13:53   Thu, 2017-10-26 13:53:02 [bdf7f37130c5a32c786a726759045bad63c51a275db0d0862bf5b3f5d476f89f] (3/3)
Keeping archive: localhost.vagrant-2017-10-25T15:56   Wed, 2017-10-25 15:56:14 [a95236834513e8f832b53601dee3942e29af0ac8cffa0fb120b99d010c545448]
$ 

日ごとに1つに集約されていることを確認

# borg list /backup3
localhost.vagrant-2017-10-25T15:56   Wed, 2017-10-25 15:56:14 [a95236834513e8f832b53601dee3942e29af0ac8cffa0fb120b99d010c545448]
localhost.vagrant-2017-10-26T14:02   Thu, 2017-10-26 14:02:06 [6d4fe3c085d66f7f29e9d468a41b5e07894215faa148f78dd5d24c08870995e3]

次に1日だけ残すようにしてみます。

$ borg prune -v --list --keep-daily=1 root@192.168.101.101:/backup3/
Keeping archive: localhost.vagrant-2017-10-26T14:02   Thu, 2017-10-26 14:02:06 [6d4fe3c085d66f7f29e9d468a41b5e07894215faa148f78dd5d24c08870995e3]
Pruning archive: localhost.vagrant-2017-10-25T15:56   Wed, 2017-10-25 15:56:14 [a95236834513e8f832b53601dee3942e29af0ac8cffa0fb120b99d010c545448] (1/1)

1日分のバックアップのみ残りました。

$ borg list /backup3
localhost.vagrant-2017-10-26T14:02   Thu, 2017-10-26 14:02:06 [6d4fe3c085d66f7f29e9d468a41b5e07894215faa148f78dd5d24c08870995e3]

まとめ

シンプルでいいですね。
除外するファイルや圧縮率もを細かく指定できたり、差分出したりといろんなオプションが用意されています。
お試しあれ。

18
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
20