
BUP 並不只是 Git,而是一套以 Git 為基礎的備份工具。我平常用 rsync 備份,一直運作良好,唯一的遺憾是無法把檔案還原到某個特定時間點。所以我開始找替代方案,最後選了 BUP:它把資料存進一個倉庫,並提供「回到某個時間點」的還原選項。
運作方式是:先初始化一個空倉庫,然後備份所有檔案。每次備份完成,BUP 會建立一個還原點,之後你可以回到那裡。它同時會為所有檔案建立索引,記錄檔案屬性與校驗和。下次備份時,BUP 比對屬性與校驗和,只保存真正變動的資料,因此非常省空間。
先確認已啟用 Repoforge 與 EPEL 倉庫:
[techarena51@vps ~]$ sudo yum groupinstall "Development Tools"
[techarena51@vps ~]$ sudo yum install python python-devel
[techarena51@vps ~]$ sudo yum install fuse-python pyxattr pylibacl
[techarena51@vps ~]$ sudo yum install perl-Time-HiRes
[techarena51@vps ~]$ git clone git://github.com/bup/bup
[techarena51@vps ~]$ cd bup
[techarena51@vps ~]$ make
[techarena51@vps ~]$ make test
[techarena51@vps ~]$ sudo make install
CentOS 7 上 make test 可能報錯,但不影響後續的 make install。
[techarena51@vps ~]$ bup init
預設倉庫放在 ~/.bup,可用環境變數改掉:
export BUP_DIR=/mnt/user/bup
接著建立索引。這個索引記錄每個檔案及其屬性與 Git 物件 id(sha1 雜湊),屬性包含軟連結、權限與不可變位元組。
bup index /path/to/file
bup save -n nameofbackup /path/to/file
# 實例
[techarena51@vps ~]$ bup index /var/www/html
Indexing: 7973, done (4398 paths/s).
bup: merging indexes (7980/7980), done.
[techarena51@vps ~]$ bup save -n techarena51 /var/www/html
Reading index: 28, done.
Saving: 100.00% (4/4k, 28/28 files), done.
bloom: adding 1 file (7 objects).
Receiving index from server: 1268/1268, done.
bup save 會把內容切塊後存成物件,-n 用來指定備份名稱。
[techarena51@vps ~]$ bup ls
local-etc techarena51 test
# 列出某個備份的所有還原點
[techarena51@vps ~]$ bup ls techarena51
2014-09-24-064416 2014-09-24-071814 latest
# 檢視該備份中的檔案
[techarena51@vps ~]$ bup ls techarena51/2014-09-24-064416/var/www/html
apc.php techarena51.com wp-config-sample.php wp-load.php
在同一台伺服器上備份從來不是好主意。BUP 支援遠端備份,但前提是遠端已安裝 BUP 且 SSH 金鑰已配置完成。
bup index path/to/dir
bup save -r remote-vps.com -n backupname path/to/dir
實例:備份 /var/www/html
[techarena51@vps ~]$ bup index /var/www/html
[techarena51@vps ~]$ bup save -r user@remotelinuxvps.com: -n techarena51 /var/www/html
Reading index: 28, done.
Saving: 100.00% (4/4k, 28/28 files), done.
登入遠端主機後執行:
[techarena51@vps ~]$ bup restore -C ./backup techarena51/latest
# 還原舊版的整個工作目錄到別處
[techarena51@vps ~]$ bup restore -C /tmp/bup-out /testrepo/2013-09-29-195827
# 只還原舊備份中的單一檔案
[techarena51@vps ~]$ bup restore -C /tmp/bup-out /testrepo/2013-09-29-201328/root/testbup/binfile1.bin
唯一的缺陷是無法把檔案直接還原到另一台伺服器,得再用 scp 或 rsync 手動複製過去。
bup web
bup web :8181 # 指定連接埠
最後,把流程寫成 shell 腳本並掛上 crontab,就能每天自動備份:
#!/bin/bash
bup index /var/www/html
bup save -r user@remote-vps.com: -n techarena51 /var/www/html
BUP 並不完美,但確實把備份這件事做得很好。希望這個專案後續能加入遠端還原的功能。