手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

使用 Git 备份 Linux 上的网页文件

[日期:2014-10-13] 来源:Linux中国  作者: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 手动复制文件.

通过集成的 web 服务器查看备份.

  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 http://www.linuxidc.com/Linux/2014-06/103806.htm

inotify-tools+rsync实时同步文件安装和配置 http://www.linuxidc.com/Linux/2012-06/63624.htm

Linux git命令参数及用法详解 http://www.linuxidc.com/Linux/2012-01/51205.htm

Fedora通过Http Proxy下载Git http://www.linuxidc.com/Linux/2009-12/23170.htm

在Ubuntu Server上安装Git http://www.linuxidc.com/Linux/2009-06/20421.htm

服务器端Git仓库的创建(Ubuntu) http://www.linuxidc.com/Linux/2011-02/32542.htm

Linux下Git简单使用教程(以Android为例) http://www.linuxidc.com/Linux/2010-11/29883.htm

Git权威指南 PDF高清中文版 http://www.linuxidc.com/Linux/2013-10/91053.htm

Git 的详细介绍请点这里
Git 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2014-10/107977.htm

linux
相关资讯       Git 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款