使用 Git 備份 Linux 上的網頁檔案
BUP 並不單純是 Git, 而是一款基於 Git 的軟體. 一般情況下, 我使用 rsync 來備份我的檔案, 而且迄今為止一直工作的很好. 唯一的不足就是無法把檔案恢複到某個特定的時間點. 因此, 我開始尋找替代品, 結果發現了 BUP, 一款基於 git 的軟體, 它將資料存放區在一個倉庫中, 並且有將資料恢複到特定時間點的選項.
要使用 BUP, 你先要初始化一個空的倉庫, 然後備份所有檔案. 當 BUP 完成一次備份是, 它會建立一個還原點, 你可以過後還原到這裡. 它還會建立所有檔案的索引, 包括檔案的屬性和驗校和. 當要進行下一個備份時, BUP 會對比檔案的屬性和驗校和, 只儲存發生變化的資料. 這樣可以節省很多空間.
安裝 BUP (在 CentOS 6 & 7 上測試通過)
首先確保你已經安裝了 RPMFORGE 和 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
對於 debian/Ubuntu 使用者, 你可以使用 "apt-get build-dep bup". 要獲得更多的資訊, 可以查看 https://github.com/bup/bup
在 CentOS 7 上, 當你運行 "make test" 時可能會出錯, 但你可以繼續運行 "make install".
第一步時初始化一個空的倉庫, 就像 git 一樣.
- [techarena51@vps ~]$ bup init
預設情況下, bup 會把倉庫儲存在 "~/.bup" 中, 但你可以通過設定環境變數 "export BUP_DIR=/mnt/user/bup" 來改變設定.
然後, 建立所有檔案的索引. 這個索引, 就像之前講過的那樣, 儲存了一系列檔案和它們的屬性及 git 目標 id (sha1 雜湊表). (屬性包括了軟連結, 許可權和不可改變位元組)
- bup index /path/to/file
- bup save -n nameofbackup /path/to/file
-
- #Example
- [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.
- bloom: adding 1 file (7 objects).
"BUP save" 會把所有內容分塊, 然後把它們作為對象儲存. "-n" 選項指定備份名.
你可以查看備份列表和已備份檔案.
- [techarena51@vps ~]$ bup ls
- local-etc techarena51 test
- #Check for a list of backups available for my site
- [techarena51@vps ~]$ bup ls techarena51
- 2014-09-24-0644162014-09-24-071814 latest
- #Check for the files available in these backups
- [techarena51@vps ~]$ bup ls techarena51/2014-09-24-064416/var/www/html
- apc.php techarena51.com wp-config-sample.php wp-load.php
在同一個伺服器上備份檔案從來不是一個好的選擇. BUP 允許你遠程備份網頁檔案, 但你必須保證你的 SSH 金鑰和 BUP 都已經安裝在遠程伺服器上.
- 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.
- bloom: adding 1 file (7 objects).
- Receiving index from server:1268/1268,done.
- bloom: adding 1 file (7 objects).
恢複備份
登入遠程伺服器並輸入下面的命令
- [techarena51@vps ~]$bup restore -C ./backup techarena51/latest
-
- #Restore an older version of the entire working dir elsewhere
- [techarena51@vps ~]$bup restore -C /tmp/bup-out/testrepo/2013-09-29-195827
- #Restore one individual file from an old backup
- [techarena51@vps ~]$bup restore -C /tmp/bup-out/testrepo/2013-09-29-201328/root/testbup/binfile1.bin
唯一的缺點是你不能把檔案恢複到另一個伺服器, 你必須通過 SCP 或者 rsync 手動複製檔案.
通過整合的 網頁伺服器查看備份.
- bup web
- #specific port
- bup web :8181
你可以使用 shell 指令碼來運行 bup, 並建立一個每日啟動並執行定時任務.
- #!/bin/bash
-
- bup index /var/www/html
- bup save -r user@remote-vps.com:-n techarena51 /var/www/html
BUP 並不完美, 但它的確能夠很好地完成任務. 我當然非常願意看到這個項目的進一步開發, 希望以後能夠增加遠程恢複的功能.
你也許喜歡閱讀使用inotify-tools即時檔案同步:
RHCE系列之即時同步----Rsync+Inotify-Tools
inotify-tools+rsync即時同步檔案安裝和配置
Linux git命令參數及用法詳解
Fedora通過Http Proxy下載Git
在Ubuntu Server上安裝Git
伺服器端Git倉庫的建立(Ubuntu)
Linux下Git簡單使用教程(以Android為例)
Git權威指南 PDF高清中文版
Git 的詳細介紹:請點這裡
Git 的:請點這裡
本文永久更新連結地址: