简洁的想法

 找回密码
 注册
搜索
查看: 528|回复: 1

Ubuntu下使用rsnapshot做远程备份

[复制链接]
发表于 2011-11-17 09:00:34 | 显示全部楼层 |阅读模式
我的系统有很多东西需要备份,而我比较懒,不想自己动脑筋去写shell执行rsync,就用了rsnapshot,在Ubuntu下安装rsnapshot很简单:
  1. $ sudo apt-get install rsnapshot
复制代码
剩下的就是配置rsnapshot了。

我有两台Ubuntu,一台叫backup.server用作备份,一台叫online.server,这台需要备份。
首先在两台机器上都建立备份的用户rbackup
  1. $sudo useradd -d /home/rbackup
复制代码
我要用backup.server去备份online.server,所以backup.server上的用户要登陆到online.server上去执行rsync命令。在backup.server上:
  1. $ cd .ssh
  2. $ ssh-keygen -t rsa
复制代码
生成id_rsa和id_rsa.pub,注意这里不要输入密码,因为我们需要无密码去连接对方。然后把id_rsa.pub中的内容放到online.server上rbackup用户的信任列表中去,即拷贝id_rsa.pub的内容到online.server上rbackup用户的authorized_keys,如果没有这个文件,则创建一个。现在我们在backup.server上应该可以不提示密码就ssh到online.server了。

出于安全性考虑,没有密码的ssh被认为是极其不安全的,所以我们要限制一下online.server上从ssh登陆上来的rbackup用户的权限,这里通过修改online.server上rbackup的authorized_keys文件如下:
  1. from="backup.server",command="/home/rbackup/validate-rsync.sh" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA2x0QYNVJtF8h8oSlKFzpnPJPFHoi4IYl0Y60eYMb.... == rbackup@backup.server
复制代码
这里告诉了online.server上的rbackup用户,当从ssh上登陆上来时,执行shell validate-rsync.sh这个脚本。好了,我们的这个validate-rsync.sh具体如下:
  1. #!/bin/sh  
  2. case "$SSH_ORIGINAL_COMMAND" in  
  3.       *\&*)  
  4.         echo "Rejected 1"  
  5.         ;;  
  6.       *\;*)  
  7.         echo "Rejected 2"  
  8.         ;;  
  9.       rsync*)  
  10.         $SSH_ORIGINAL_COMMAND  
  11.         ;;  
  12.       *true*)  
  13.         echo $SSH_ORIGINAL_COMMAND  
  14.         ;;  
  15.       *)  
  16.         echo "Rejected 3"  
  17.         ;;  
  18. esac
复制代码
  1. $ chmod +x validate-rsync.sh
复制代码
这时候如果你从backup.server上执行 ssh online.server应该直接返回"Reject 3",而不再能够登陆上去了。

另外,由于我要备份很多文件是需要root权限的,所以需要对rsync作一下包装,在online.server上执行:
  1. $ sudo vi /usr/local/bin/rsync_wrapper.sh
复制代码
然后输入以下脚本:
  1. #!/bin/sh  
  2. /usr/bin/sudo /usr/bin/rsync "$@";
复制代码
  1. $ sudo chmod +x /usr/local/bin/rsync_wrapper.sh
复制代码
为了能够让rbackup这个用户可以执行这条命令,我们还需要把rbackup加入到sudoer中去,在onlien.server上执行:
  1. $ sudo visudo
复制代码
加入下面这行
  1. rbackup ALL=NOPASSWD: /usr/bin/rsync
复制代码
这句话告诉系统,rbackup这个用户可以用root的权限来执行rsyn命令。

好了,这些做完以后,我们就可以开始配置rsnapshot了,在backup.server上执行
  1. $ sudo vi /etc/rsnapshot.conf
复制代码
去掉那些注释掉的cmd_xxx,当然前提是你需要有这些命令。比较重要的是下面几点:

  • interval部分,按照实际情况配置是需要按小时备份,还是按天,按星期等等
  • rsync_long_args --rsync-path=rsync_wrapper.sh --delete --numeric-ids --relative --delete-excluded,
    注意这里加入了--rsync-path=rsync_wrapper.sh,这里告诉rsnapshot,我们在online.server上运行的rsync是这个包装过的版本,即有sudo权限的。
  • 最后,就是那些backup points了,比如说:
    1.     backup rbackup@online.server:/etc/ online.server/ exclude=mtab
    2.     backup rbackup@online.server:/home/ online.server/ exclude=dummy
    复制代码
    具体可以参考rsnapshot的文档。



现在测试一下吧:
  1. $ rsnapshot hourly
复制代码
这时候如果一切正常的话,在你的目录里就会有hourly.0/online.server这样一个目录,包含了那些你希望备份的东西。
好了,最后就是我们要让它定时执行,也就是编辑我们的cron,
  1. $ crontab -e
  2. 30 19 * * * rsnapshot daily
复制代码
这里我设定的是每天19:30分执行rsnapshot进行备份。你也可以编写一个脚本封装一下rsnapshot,让它执行完之后再把备份的log发到你的邮箱里去,就象下面这样:
  1. #!/bin/sh  
  2. rm /home/rbackup/log/*  
  3. rsnapshot daily  
  4. cat /home/rbackup/log/rsnapshot | mail -s "rsnapshot backup log" "abc@foo.com"
复制代码
转自: http://cristal.iteye.com/blog/485674
 楼主| 发表于 2011-11-17 09:05:20 | 显示全部楼层
After testing a lot of file backup utilities for Windows, I found the best solution (for my needs) to be rsnapshot, an optional tool that can be installed within Cygwin, a replacement for Windows’ standard command line (cmd.exe).

Cygwin adds a full set of Unix/Linux tools the system, most of them command-line ones, of which rsnapshot is one. As such, it can be quite difficult to configure if you have no experience with this kind of environment. But once you make it work, it’s pure awesomeness, as rsnapshot:

a) Backups files as they are, without encasing them into proprietary file formats, so you can browse the backup with Windows Explorer as usual. You’ll get folders named hourly.0\, hourly.1\, hourly.2\, … daily.0\, daily.1\, …, weekly.0\, …, monthly.0\ …; all of them fully browseable; all of them containing your entire set of files at that moment in time.

b) Saves disk space by using Windows NTFS-formatted disks built-in “hardlinks” feature, which allows a single physical file to have multiple different names on multiple different folders without taking more space. So, if a file were backed up as \hourly.0\videos\hugemovie.avi, \hourly.1\videos\hugemovie.avi, \hourly.2\…, it would still take space only Hardlinks have an additional advantage, in that they’re resistant do certain kinds of stupidity. Specifically, the only way to delete such a file is by deleting all of its names.

c) Has full network support, both local and remote, including the ability to backup from remote servers to your external drive if you so wished. Think, for instance, regular backups of your website.

That said, there are a few downsides:

d) If for some reason you edit a backed up file, the change will apply everywhere that file is hardlinked from, since all “copies” are actually the same file. So it’s important to be careful.

e) Renamed files aren’t detected as such, so they get backed up again, taking that much space twice (or thrice, or…). There are workaround for this, such as also renaming/moving the previously backed up version within the most recent backup folder (usually \hourly.0\…) whenever you rename/move the original so as to make both names equal. So, depending on how you manage your files, this can be a once in a blue moon procedure, or a multiple times a day annoyance.

f) Configuring the scheduling of backups can be a confusing procedure. Using Windows’ own Task Scheduler, for instance, isn’t a straightforward procedure; and enabling Cygwin’s own built in scheduling tool, ‘cron’, is a small challenge in itself. Besides, once scheduling works, it’ll still require lots of tweaking until you’re satisfied with the way it’s working.

All in all, though, I find the ability to have instant access to my backed up files via Windows Explorer so much of an advantage that rsnapshot’s disadvantages pale in comparison. Try it if you can. It’s worth the effort.

转自: http://www.giveawayoftheday.com/ ... ecovery-11-compact/
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|Archiver|简洁的想法

GMT+1, 2021-2-28 15:09

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表