使用 Git 備份 Linux 上的網頁檔案

來源:互聯網
上載者:User

使用 Git 備份 Linux 上的網頁檔案

BUP 並不單純是 Git, 而是一款基於 Git 的軟體. 一般情況下, 我使用 rsync 來備份我的檔案, 而且迄今為止一直工作的很好. 唯一的不足就是無法把檔案恢複到某個特定的時間點. 因此, 我開始尋找替代品, 結果發現了 BUP, 一款基於 git 的軟體, 它將資料存放區在一個倉庫中, 並且有將資料恢複到特定時間點的選項.

要使用 BUP, 你先要初始化一個空的倉庫, 然後備份所有檔案. 當 BUP 完成一次備份是, 它會建立一個還原點, 你可以過後還原到這裡. 它還會建立所有檔案的索引, 包括檔案的屬性和驗校和. 當要進行下一個備份時, BUP 會對比檔案的屬性和驗校和, 只儲存發生變化的資料. 這樣可以節省很多空間.

安裝 BUP (在 CentOS 6 & 7 上測試通過)

首先確保你已經安裝了 RPMFORGE 和 EPEL 倉庫

  1. [techarena51@vps ~]$ sudo yum groupinstall "Development Tools"
  2. [techarena51@vps ~]$ sudo yum install python python-devel
  3. [techarena51@vps ~]$ sudo yum install fuse-python pyxattr pylibacl
  4. [techarena51@vps ~]$ sudo yum install perl-Time-HiRes
  5. [techarena51@vps ~]$ git clone git://github.com/bup/bup
  6. [techarena51@vps ~]$ cd bup
  7. [techarena51@vps ~]$ make
  8. [techarena51@vps ~]$ make test
  9. [techarena51@vps ~]$ sudo make install

對於 debian/Ubuntu 使用者, 你可以使用 "apt-get build-dep bup". 要獲得更多的資訊, 可以查看 https://github.com/bup/bup

在 CentOS 7 上, 當你運行 "make test" 時可能會出錯, 但你可以繼續運行 "make install".

第一步時初始化一個空的倉庫, 就像 git 一樣.

  1. [techarena51@vps ~]$ bup init

預設情況下, bup 會把倉庫儲存在 "~/.bup" 中, 但你可以通過設定環境變數 "export BUP_DIR=/mnt/user/bup" 來改變設定.

然後, 建立所有檔案的索引. 這個索引, 就像之前講過的那樣, 儲存了一系列檔案和它們的屬性及 git 目標 id (sha1 雜湊表). (屬性包括了軟連結, 許可權和不可改變位元組)

  1. bup index /path/to/file
  2. bup save -n nameofbackup /path/to/file
  3.  
  4. #Example
  5. [techarena51@vps ~]$ bup index /var/www/html
  6. Indexing:7973,done(4398 paths/s).
  7. bup: merging indexes (7980/7980),done.
  8.  
  9. [techarena51@vps ~]$ bup save -n techarena51 /var/www/html
  10.  
  11. Reading index:28,done.
  12. Saving:100.00%(4/4k,28/28 files),done.
  13. bloom: adding 1 file (7 objects).
  14. Receiving index from server:1268/1268,done.
  15. bloom: adding 1 file (7 objects).

"BUP save" 會把所有內容分塊, 然後把它們作為對象儲存. "-n" 選項指定備份名.

你可以查看備份列表和已備份檔案.

  1. [techarena51@vps ~]$ bup ls
  2. local-etc techarena51 test
  3. #Check for a list of backups available for my site
  4. [techarena51@vps ~]$ bup ls techarena51
  5. 2014-09-24-0644162014-09-24-071814 latest
  6. #Check for the files available in these backups
  7. [techarena51@vps ~]$ bup ls techarena51/2014-09-24-064416/var/www/html
  8. apc.php techarena51.com wp-config-sample.php wp-load.php

在同一個伺服器上備份檔案從來不是一個好的選擇. BUP 允許你遠程備份網頁檔案, 但你必須保證你的 SSH 金鑰和 BUP 都已經安裝在遠程伺服器上.

  1. bup index path/to/dir
  2. bup save-r remote-vps.com -n backupname path/to/dir
例子: 備份 "/var/www/html" 檔案夾
  1. [techarena51@vps ~]$bup index /var/www/html
  2. [techarena51@vps ~]$ bup save -r user@remotelinuxvps.com:-n techarena51 /var/www/html
  3. Reading index:28,done.
  4. Saving:100.00%(4/4k,28/28 files),done.
  5. bloom: adding 1 file (7 objects).
  6. Receiving index from server:1268/1268,done.
  7. bloom: adding 1 file (7 objects).
恢複備份

登入遠程伺服器並輸入下面的命令

  1. [techarena51@vps ~]$bup restore -C ./backup techarena51/latest
  2.  
  3. #Restore an older version of the entire working dir elsewhere
  4. [techarena51@vps ~]$bup restore -C /tmp/bup-out/testrepo/2013-09-29-195827
  5. #Restore one individual file from an old backup
  6. [techarena51@vps ~]$bup restore -C /tmp/bup-out/testrepo/2013-09-29-201328/root/testbup/binfile1.bin

唯一的缺點是你不能把檔案恢複到另一個伺服器, 你必須通過 SCP 或者 rsync 手動複製檔案.

通過整合的 網頁伺服器查看備份.

  1. bup web
  2. #specific port
  3. bup web :8181

你可以使用 shell 指令碼來運行 bup, 並建立一個每日啟動並執行定時任務.

  1. #!/bin/bash
  2.  
  3. bup index /var/www/html
  4. 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 的:請點這裡

本文永久更新連結地址:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.