Linux Crontab + rsync 实现远程备份
公司图片服务器上的资源比较多了,考虑到容灾问题,特把资源进行跨机房,跨网段远程文件备份。在网上找到了 rsync 这款软件,它能帮我解决问题。现在把我在局域网的配置方法进行了整理,分享如下:
主机:192.168.190.199 备机:192.168.190.208 配置操作步骤: 备机 1.服务器是否安装rsync
rpm -qa|grep –i rsync
若有显示,说明已安装
2.安装rsync软件
有两种安装方式:
a.利用 yum 安装
yum install rsync xinetd
查看是否自己配置了yum (如果没有,问 google) b.源码安装 下载源码自己编译 http://rsync.samba.org/ftp/rsync/rsync-3.0.9.tar.gz
cd rsync-3.0.9
./configure
make && make install
3.配置rsyncd.conf 安装完 rsync 服务,这个文件默认是没有的需要自己手动创建一个。
vi /etc/rsyncd.conf
#主要全局参数
uid = root
gid = root
use chroot = yes #为了安全考虑,让rsync运行于chroot环境
log file = /var/log/rsyncd.log #rsync的日志文件
pid file = /var/run/rsyncd.pid #rsync的pid文件
hosts deny = * #除了允许的之外,默认禁止访问
[picdata]
comment = backup for picServer
path = /home/Gzh/rsyncBak
read only = no
auth users = Gzh
secrets file = /etc/rsync.pass
hosts allow = 192.168.190.199,127.0.0.1
:wq!
4.配置密码文件 rsync.pass
vi /etc/rsync.pass
Gzh:123456
格式:用户名:密码(用户名必须系统真是存在的用户,密码最好不要个系统密码一致)
5.修改 rsync.pass 和rsyncd.conf 权限
这个两个文件必须是 600 不然服务不读
chmod 600 /etc/rsync.pass
chmod 600 /etc/rsyncd.conf
6.开启防火墙tcp 873端口
vi /etc/sysconfig/iptables
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
:wq
7.启动rsync 服务
启动服务有两种方式:
a.使用rsync的–daemon选项启动,rsync服务默认使用873号端口
启动:rsync –daemon
关闭:pkill rsync
lsof –i:873 (检查服务是否启动)
b.xinetd守护
修改/etc/xinetd.d/rsync文件,把
disable = yes
改成
disable = no
然后重启xinetd即可,
/etc/init.d/xinetd restart
服务
service xinetd start #启动
service xinetd stop #停止
service xinetd restart #重新启动
如果你的xinetd开机并没自动启动的话,需要执行
chkconfig xinetd on
到此rsync 服务已经配置完成
主机:
1.配置密码文件rsync.pass
vi /home/Gzh/shell/rsync.pass
123456
:wq
这个里面只配置密码
修改文件的权限 600
2.执行命令
/usr/bin/rsync -vzrtopg --password-file=/home/Gzh/shell/rsync.pass /home/Gzh/shell Gzh@192.168.190.208::picData
执行成功
3.备份脚本
#!/bin/sh
#command
rsync=/usr/bin/rsync
echo=/bin/echo
#backup server info
remote_host=Gzh@192.168.190.208
remote_path=picData
#backup
backup_path="/home/Gzh/shell"
#passFile
passfile_path="/home/Gzh/shell/rsync.pass"
for path in $backup_path;
do
date=`date "+%D %H:%M:%S"`
$echo "--- Start Backup $path $date ----"
$echo "$rsync -vzrtopg --password-file=$passfile_path $path $remote_host::$remote_path"
$rsync -zrtopg --password-file=$passfile_path $path $remote_host::$remote_path
date=`date "+%D %H:%M:%S"`
$echo "--- End Backup $path $date ----"
done
这样就可以实现远程备份了。