系统:centos5.4
软件:rsync
环境:server1: apache服务器 IP:192.168.1.10
server2: 备份服务器 IP:192.168.3.140
目的:通过rsync自动同步server1的web文件(路径/usr/local/apache/htdocs)到server2的/tmp/rsync_bak下
一 安装配置rsync
1.1首先在在server2上通过yum安装及配置rsync服务端
- [root@centos5 ~]# yum install rsync -y
1.2 创建rsync配置文件
- [root@centos5 ~]# mkdir /etc/rsyncd
- [root@centos5 ~]# cd /etc/rsyncd
- [root@centos5 rsyncd]# touch rsyncd.conf rsyncd.motd rsyncd.secrets
上面3个文件分别是rsyncd.conf主配置文件,rsyncd提示文件,rsyncd用户密码配置文件
1.3 配置rsync相关的3个配置文件
- [root@centos5 rsyncd]# vi /etc/rsyncd.conf
- motd file = /etc/rsyncd.motd
- read only = no
- list = yes
- uid = root
- gid = root
- Use chroot =no
- #hosts allow = 192.168.0.61
- #hosts deny =192.168.128.0/24
- max connections = 5
- log file = /var/log/rsyncd.log
- pid file = /var/run/rsyncd.pid
- lock file = /var/run/rsyncd.lock
- #define dirctory for rsync
- [test]
- path =/tmp/rsync_bak/
- #此路径为本机(rsync服务器端)存放同步过来文件的目录
- secrets file =/etc/rsyncd.secrets
- auth users = test
- read only = no
- #如果有多台服务器要同步备份可以设置多个,例如
- #[test1]
- #path =/tmp/rsync_bak1/
- #secrets file =/etc/rsyncd1.secrets
- #auth users = test1
- #read only = no
- [root@centos5 rsyncd]# vi /etc/rsyncd.motd
- Welcome to use the test rsync services!
- [root@centos5 rsyncd]#vi /etc/rsyncd.secrets
- #密码文件权限为600,此步必须设置
- #用户名和密码格式:用户名:密码
- test:test
- [root@centos5 rsyncd]#chmod 600 /etc/rsyncd/rsyncd.*
1.4 运行rsync
- [root@centos5 rsyncd]#rsync --daemon /etc/rsyncd/rsyncd.conf
- [root@centos5 rsyncd]# ps -ef |grep rsyncd|grep -v grep
- root 10396 1 0 14:30 ? 00:00:00 /usr/bin/rsync --daemon /etc/rsyncd/rsyncd.conf
2.1 在server1上安装rsyncd客户端
- [root@centos5 ~]# yum install rsync -y
2.2 客户端上只需要配置密码文件
- [root@centos5 ~]# mkdir /etc/
- [root@centos5 ~]# vi /etc/rsyncd/rsyncd.secrets
- test
- #此处的密码必须匹配服务器端设置的密码,并且此文件权限也需要修改成600
- [root@centos5 ~]#chmod 600 /etc/rsyncd/rsyncd.secrets
二 测试rsync同步
在server1上执行下面命令
- [root@test1 htdocs]# /usr/bin/rsync -vzrtopg --progress --delete /usr/local/apache2/htdocs/ --password-file=/etc/rsyncd.secrets test@192.168.3.140::test
- Welcome to use the test rsync services!
- sending incremental file list
- ./
- a
- 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=1041/1043)
- b
- 0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=1040/1043)
- sent 311129 bytes received 2154 bytes 626566.00 bytes/sec
- total size is 145816024 speedup is 465.45
三 设置定时同步
在server1上做如下操作
- [root@test1 ~]# vi rsyncd_crond
- 0 0 5 * * /usr/bin/rsync -vzrtopg --progress --delete /usr/local/apache2/htdocs/ --password-file=/etc/rsyncd.secrets test@192.168.3.140::test
- [root@test1 ~]# crontab rsyncd_crond
- [root@test1 ~]# crontab -l
- 0 0 5 * * /usr/bin/rsync -vzrtopg --progress --delete /usr/local/apache2/htdocs/ --password-file=/etc/rsyncd.secrets test@192.168.3.140::test
如果要修改同步时间也可以修改
- [root@test1 ~]# crontab -e 进入后修改