使用rsnapshot进行增量远程备份

Linux/Windows服务器、系统、网络、运营等过程中的一些疑难杂症和经验分享
回复
头像
光头强
帖子: 20
注册时间: 2021年 2月 20日 21:34 星期六

使用rsnapshot进行增量远程备份

帖子 光头强 »

安装 rsnapshot

代码: 全选

apt install rsnapshot

配置rsnapshot

rsnapshot的配置位于/etc/rsnapshot.conf中。 建议在开始更改之前先对其进行备份:

代码: 全选

mv /etc/rsnapshot.conf /etc/rsnapsnap.conf.default

如果有必要的话,为每一个单独的备份项目设置一个独立的配置文件:

代码: 全选

cp /etc/rsnapshot.conf.default /etc/rsnapshot.conf.server1

重要说明:rsnapshot.conf在元素之间需要TAB。 因此,“ cmd / usr / bin / ssh”是“ cmd <TAB> / usr / bin / ssh”。

代码: 全选

cmd_ssh /usr/bin/ssh

添加以下备份间隔:

代码: 全选

interval hourly 6
interval daily 7
interval weekly 4
interval monthly 3

我正在使用存储在/root/.ssh/backup中的无密码ssh密钥。 我还使用其他ssh端口。 因此,请进行以下更改:

ssh_args -p 8989 -i ~/.ssh/backup

这两个命令用于报告(请参见下文):

代码: 全选

rsync_long_args --stats --delete --numeric-ids --relative --delete-excluded
verbose 4

现在,告诉rsnapshot将备份保存在哪里:

代码: 全选

snapshot_root /backups/server1.lowend.party/

最后,添加备份定义:

backup root@server1.lowend.party:/data/ .

会将文件保留在/backups/server1.lowend.party/hourly.0 中。

想在备份中排除/ data / cache:

代码: 全选

exclude_file /etc/rsnapshot.server1.exclude

接着输入:

代码: 全选

- /data/cache/*

没有使用默认的/etc/rsnapshot.conf名称,所以需要对所有rsnapshot命令使用-c参数。 开始测试配置:

代码: 全选

root@backup:/etc# rsnapshot -c /etc/rsnapshot.conf.server1 configtest
Syntax OK

Now we can run a simulation:

root@backup:/etc# rsnapshot -c /etc/rsnapshot.conf.server1 -t hourly
echo 9633 > /var/run/rsnapshot.pid
mkdir -m 0755 -p /backups/hourly.0/
/usr/bin/rsync -a --stats --delete --numeric-ids --relative --delete-excluded \
--exclude-from=/etc/rsnapshot.server1.exclude --rsh=/usr/bin/ssh -p 8989 \
-i ~/.ssh/backup root@server1.lowend.party:/data/ \
/backups/hourly.0/server1.lowend.party/
touch /backups/hourly.0/

One more thing to do. I like to use rsnapshot’s reporting tool, so let’s enable it:

cp /usr/share/doc/rsnapshot/examples/utils/rsnapreport.pl /usr/local/bin
chmod 755 /usr/local/bin/rsnapreport.pl

We’re good to go!
Running rsnapshot

On server1, I have 547MB in /data, and 30MB in /data/cache which will be excluded:

root@server1:~# du -sm /data
547 /data
root@server1:~# du -sm /data/cache
30 /data/cache

Let’s run our first rsnapshot backup:

root@backup:/backups/server1.lowend.party# rsnapshot -c /etc/rsnapshot.conf.server1 hourly
Setting locale to POSIX "C"
echo 10012 > /var/run/rsnapshot.pid
mkdir -m 0755 -p /backups/server1.lowend.party/hourly.0/
/usr/bin/rsync -av --stats --delete --numeric-ids --relative \
--delete-excluded --exclude-from=/etc/rsnapshot.server1.exclude \
--rsh=/usr/bin/ssh -p 8989 -i ~/.ssh/backup \
root@server1.lowend.party:/data/ \
/backups/server1.lowend.party/hourly.0/.
receiving incremental file list
data/
<snipped>
data/cache/

Number of files: 10,982 (reg: 10,980, dir: 2)
Number of created files: 10,982 (reg: 10,980, dir: 2)
Number of deleted files: 0
Number of regular files transferred: 10,980
Total file size: 518,702,282 bytes
Total transferred file size: 518,702,282 bytes
Literal data: 518,702,282 bytes
Matched data: 0 bytes
File list size: 611,123
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 208,691
Total bytes received: 519,874,481

sent 208,691 bytes received 519,874,481 bytes 80,012,795.69 bytes/sec
total size is 518,702,282 speedup is 1.00
touch /backups/server1.lowend.party/hourly.0/
rm -f /var/run/rsnapshot.pid
/usr/bin/logger -p user.info -t rsnapshot[10012] /usr/bin/rsnapshot -c \
/etc/rsnapshot.conf.server1 hourly: completed successfully

Now I can also run that using the rsnapshotreport.pl script we setup. If I do, the output will look like this (the TOTAL MB is a little different because I ran these at different times):

rsnapshot -c /etc/rsnapshot.conf.server1 hourly | /usr/local/bin/rsnapshotreport.pl

SOURCE TOTAL FILES FILES TRANS TOTAL MB MB TRANS LIST GEN TIME FILE XFER TIME

server1.lowend.party:/data/ 11982 1 564.81 46.10 0.001 seconds 0.000 seconds

Now if I continue running hourly backups, I see new directories being created in /backups/server1.lowend.party:

drwxr-xr-x 3 root root 4096 Jul 12 16:03 hourly.0
drwxr-xr-x 3 root root 4096 Jul 12 16:01 hourly.1
drwxr-xr-x 3 root root 4096 Jul 12 15:58 hourly.2

Interestingly, hourly.0 is 500-odd MB, will the rest are only 1MB. Why? Because hourly.1, hourly.2, etc. are simply hard links back to hourly.0. This is a huge space savings.

If I nuke some files on server1’s /data and run another couple backups, you’ll see this:

root@backup:/backups/server1.lowend.party# du -sm *
526 hourly.0
39 hourly.1
1 hourly.2
1 hourly.3

rsnapshot is retaining data in hourly.1 because it’s needed to reconstruct the backups for that hour.
Automating rsnapshot

Setting up automated backups is as easy as putting jobs in cron. For example:

MAILTO=you@somewhere.com
0 * * * * root /usr/bin/rsnapshot -c /etc/rsnapshot.conf.server1 hourly 2>&1 | /usr/local/bin/rsnapreport.pl
0 3 * * * root /usr/bin/rsnapshot -c /etc/rsnapshot.conf.server1 daily 2>&1 | /usr/local/bin/rsnapreport.pl
0 3 * * 1 root /usr/bin/rsnapshot -c /etc/rsnapshot.conf.server1 weekly 2>&1 | /usr/local/bin/rsnapreport.pl
30 2 1 * * root /usr/bin/rsnapshot -c /etc/rsnapshot.conf.server1 monthly 2>&1 | /usr/local/bin/rsnapreport.pl

Now

回复