实例需求:
3台主机,host1和host3之间文件的单向同步和双向同步问题。
1.同步脚本:
#!/bin/bash HOST1="172.31.29.221" USER1="w1" PASSWORD1="Server123" HOST2="172.31.28.61" USER2="w2" PASSWORD2="Server123" HOST3="172.31.19.244" USER3="w3" # 如果 rsync 守护进程需要此密码文件 RSYNC_OPTS="--password-file=/etc/rsyncd/w.secrets" #host3向host1单向同步/www/wwwroot/test.com/文件夹, #同时子目录public/不同步 REMOTE_SOURCE="$USER3@$HOST3::rsync_test" LOCAL_DIR="/www/wwwroot/test.com/" rsync -avz --delete --exclude="public/" --exclude="runtime/" \ $RSYNC_OPTS "$REMOTE_SOURCE" "$LOCAL_DIR" # 可选:记录日志 echo "$(date): Sync completed from $HOST3 to local server." \ >> /var/log/sync_from_host3.log #host1和host3的/www/wwwroot/test.com/public/uploads/ #文件夹双向同步 SOURCE_DIR="$USER3@$HOST3::rsync_test_uploads" DES_DIR="/www/wwwroot/test.com/public/uploads/" rsync $RSYNC_OPTS -avz "$SOURCE_DIR" "$DES_DIR" rsync $RSYNC_OPTS -avz "$DES_DIR" "$SOURCE_DIR" # 可选:记录日志 echo "$(date): Sync completed." >> /var/log/sync_to_host3.log #host3向host1单向同步/www/wwwroot/test.com/public/文件夹, #同时子目录uploads/不同步 REM_SOURCE="$USER3@$HOST3::rsync_test_public" LOL_DIR="/www/wwwroot/test.com/public/" rsync -avz --delete --exclude="uploads/" \ $RSYNC_OPTS "$REM_SOURCE" "$LOL_DIR" # 可选:记录日志 echo "$(date): Sync completed from $HOST3 to local server." \ >> /var/log/sync_from_host3.log
2.host3的rsyncd.conf配置文件:
# sample rsyncd.conf configuration file # GLOBAL OPTIONS #motd file=/etc/motd #log file=/var/log/rsyncd # for pid file, do not use /var/run/rsync.pid if # you are going to run rsync out of the init.d script. # The init.d script does its own pid file handling, # so omit the "pid file" line completely in that case. # pid file=/var/run/rsyncd.pid #syslog facility=daemon #socket options= # MODULE OPTIONS address = 172.31.19.244 port = 873 uid = user1_myrsync gid = myrsync use chroot = no max connections=10 timeout = 600 # motd file=/etc/rsyncd/rsyncd.motd pid file=/var/run/rsyncd.pid lock file = /var/lock/rsyncd log file=/var/log/rsync.log log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes. dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz [rsync_test] comment = abc.com phpcode rsync path = /www/wwwroot/test.com exclude = public/*** runtime/*** lock file = /var/lock/rsyncd # the default for read only is yes. .. read only = true write only = false list = yes auth users = user1_myrsync w1 w2 w3 w4 secrets file = /etc/rsyncd/rsyncd.secrets strict modes = no hosts allow = 172.31.19.244 172.31.28.61 172.31.29.221 172.31.16.193 172.31.21.140 # hosts deny = ignore errors = no ignore nonreadable = yes transfer logging = no # log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes. timeout = 600 refuse options = checksum dry-run [rsync_test_uploads] comment = public archive is ok by yyyyyyy path = /www/wwwroot/test.com/public/uploads/ lock file = /var/lock/rsyncd # the default for read only is yes... read only = no write only = no list = yes auth users = user1_myrsync w1 w2 w3 w4 secrets file = /etc/rsyncd/rsyncd.secrets strict modes = no hosts allow = 172.31.19.244 172.31.28.61 172.31.29.221 172.31.16.193 172.31.21.140 # hosts deny = ignore errors = no ignore nonreadable = yes transfer logging = no # log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes. timeout = 600 refuse options = checksum dry-run [rsync_test_public] comment = test.com phpcode rsync path = /www/wwwroot/test.com/public/ exclude = uploads/*** lock file = /var/lock/rsyncd # the default for read only is yes. .. read only = true write only = false list = yes auth users = user1_myrsync w1 w2 w3 w4 secrets file = /etc/rsyncd/rsyncd.secrets strict modes = no hosts allow = 172.31.19.244 172.31.28.61 172.31.29.221 172.31.16.193 172.31.21.140 # hosts deny = ignore errors = no ignore nonreadable = yes transfer logging = no # log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes. timeout = 600 refuse options = checksum dry-run
建议将rsyncd.conf中的
uid = user1_myrsync
gid = myrsync
改为
uid = root
gid = root