Rsnapshot备份工具

Rsnapshot是用perl采用perl语言,利用rsync和ssh功能来实现本地或远程的备份工具,它具有增量备份功能。



1、安装
在RHEL/CentOS系统环境需要EPEL源支持
RHEL/CentOS 7 64 Bit

## RHEL/CentOS 7 64-Bit ##
# wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-6.noarch.rpm
# rpm -ivh epel-release-7-6.noarch.rpm
RHEL/CentOS 6 32-64 Bit

## RHEL/CentOS 6 32-Bit ##
# wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
# rpm -ivh epel-release-6-8.noarch.rpm

## RHEL/CentOS 6 64-Bit ##
# wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# rpm -ivh epel-release-6-8.noarch.rpm
RHEL/CentOS 5 32-64 Bit

## RHEL/CentOS 5 32-Bit ##
# wget http://download.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
# rpm -ivh epel-release-5-4.noarch.rpm

## RHEL/CentOS 5 64-Bit ##
# wget http://download.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
# rpm -ivh epel-release-5-4.noarch.rpm

# yum install rsnapshot

Debian/Ubuntu/环境直接使用一下命令
# apt-get install rsnapshot

2、rsnapshot需要ssh密钥认证支持,所以需提前完成服务器间的密钥认证。

3、调整配置文件

# vi /etc/rsnapshot.conf
#修改默认备份目录
snapshot_root            /data/backup/
#去掉cmd_ssh前的注释,已使用ssh传输
cmd_ssh         /usr/bin/ssh

#########################################
#           BACKUP INTERVALS            #
# Must be unique and in ascending order #
# i.e. hourly, daily, weekly, etc.      #
#########################################
#备份周期和份数
interval        hourly  6
interval        daily   7
interval        weekly  4
interval        monthly 3

#ssh 端口
ssh_args        -p 22

#备份过滤
exclude_file    /data/backup/tecmint.exclude

#本机的备份,localhost位于定义的备份目录之内
backup      /tecmint/       localhost/
backup      /etc/           localhost/

#远程备份
backup       root@example.com:/home/        /data/backup/

# vi /data/backup/tecmint.exclude
#目录内的文件目录级别
+ /boot
+ /data
+ /tecmint
+ /etc
+ /home
+ /opt
+ /root
+ /usr
- /usr/*
- /var/cache
+ /var
- /*


#测试配置文件
# rsnapshot configtest

测试小时备份配置
# rsnapshot -t hourly
Sample Output

echo 2028 > /var/run/rsnapshot.pid 
mkdir -m 0700 -p /data/backup/ 
mkdir -m 0755 -p /data/backup/hourly.0/ 
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded /home \
    /backup/hourly.0/localhost/ 
mkdir -m 0755 -p /backup/hourly.0/ 
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded /etc \
    /backup/hourly.0/localhost/ 
mkdir -m 0755 -p /data/backup/hourly.0/ 
/usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded \
    /usr/local /data/backup/hourly.0/localhost/ 
touch /data/backup/hourly.0/
Note: The above command tells rsnapshot to create an “hourly” backup. It actually prints out the commands that it will perform when we execute it really.

进行备份
# rsnapshot hourly

备份结果
# cd /data/backup
# ls -l

total 4
drwxr-xr-x 3 root root 4096 Oct 28 09:11 hourly.0

计划任务

0     */4    * * *    root    /usr/bin/rsnapshot hourly
30     3     * * *    root    /usr/bin/rsnapshot daily
0      3     * * 1    root    /usr/bin/rsnapshot weekly
30     2     1 * *    root    /usr/bin/rsnapshot monthly
Let me explain exactly, what above cron rules does:

Runs every 4 hours and creates an hourly directory under /backup directory.
Runs daily at 3:30am and create a daily directory under /backup directory.
Runs weekly on every Monday at 3:00am and create a weekly directory under /backup directory.
Runs every monthly at 2:30am and create a monthly directory under /backup directory.
To better understand on how cron rules works, I suggest you read our article that describes.


#备份报告
# cp /usr/share/doc/rsnapshot-1.3.1/utils/rsnapreport.pl /usr/local/bin
# chmod +x /usr/local/bin/rsnapreport.pl

#需要修改配置文件如下形式
vi /etc/rsnapshot.conf

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


#可以重新定义备份并发送报告,记得定义mailx发送命令,不然可能发不出去哦
0     */4    * * *    root    /usr/bin/rsnapshot hourly 2>&1  | /usr/local/bin/rsnapreport.pl | mail -s "Hourly Backup" yourname@email.com
30     3     * * *    root    /usr/bin/rsnapshot daily 2>&1  | /usr/local/bin/rsnapreport.pl | mail -s "Daily Backup" yourname@email.com
0      3     * * 1    root    /usr/bin/rsnapshot weekly 2>&1  | /usr/local/bin/rsnapreport.pl | mail -s "Weekly Backup" yourname@email.com
30     2     1 * *    root    /usr/bin/rsnapshot monthly 2>&1  | /usr/local/bin/rsnapreport.pl | mail -s "Montly Backup" yourname@email.com

#报告样式如下
SOURCE           TOTAL FILES    FILES TRANS TOTAL MB    MB TRANS   LIST GEN TIME  FILE XFER TIME
--------------------------------------------------------------------------------------------------------
localhost/          185734     11853     2889.45    6179.18    40.661 second   0.000 seconds

发表评论

电子邮件地址不会被公开。 必填项已用*标注