使用 GIT 備份 linux 上的網頁文件

jopen 10年前發布 | 22K 次閱讀 Git 版本控制系統

使用 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).</pre>

"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-064416 2014-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</pre>

在同一個服務器上備份文件從來不是一個好的選擇. 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</pre>

唯一的缺點是你不能把文件恢復到另一個服務器, 你必須通過 SCP 或者 rsync 手動復制文件.

通過集成的 web 服務器查看備份.

bup web

specific port

bup web :8181</pre>

你可以使用 shell 腳本來運行 bup, 并建立一個每日運行的定時任務.

#!/bin/bash

bup index /var/www/html bup save -r user@remote-vps.com: -n techarena51 /var/www/html </pre>

BUP 并不完美, 但它的確能夠很好地完成任務. 我當然非常愿意看到這個項目的進一步開發, 希望以后能夠增加遠程恢復的功能.

你也許喜歡閱讀這篇——使用inotify-tools實時文件同步.


via: http://techarena51.com/index.php/using-git-backup-website-files-on-linux/

作者:Leo G 譯者:wangjiezhe 校對:Caroline

本文由 LCTT 原創翻譯,Linux中國 榮譽推出

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!