系統: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 [email protected]::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 [email protected]::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 [email protected]::test
如果要修改同步時間也可以修改
- [root@test1 ~]# crontab -e 進入後修改