環境:CentOS release 6.8 (Final),內核 2.6.32-642.el6.x86_64,已配置 EPEL 源(https://mirrors.aliyun.com/repo/epel-6.repo),SELinux 已 Disabled,iptables 未運行。
[root@rsync ~]# rpm -qa rsync rsync-3.0.6-12.el6.x86_64
若未安裝:yum install -y rsync
用來管理本地目錄,禁止登入 shell:
[root@rsync ~]# useradd -s /sbin/nologin -M rsync [root@rsync ~]# id rsync uid=501(rsync) gid=501(rsync) groups=501(rsync)
此檔案預設不存在,需自行建立:
cat >> /etc/rsyncd.conf <<EOF #rsync_config_______start #created by syaving 2016-9-22 uid = rsync gid = rsync use chroot = no max connections = 200 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [backup] path = /backup ignore errors read only = false list = false hosts allow=172.16.1.0/24 #hosts deny=0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password #rsync_config_______end EOF
依 auth users 配置遠程連接賬戶,並依 secrets file 產生密碼檔:
[root@rsync ~]# echo "rsync_backup:rsync" > /etc/rsync.password [root@rsync ~]# cat /etc/rsync.password rsync_backup:rsync
[root@rsync ~]# chmod 600 /etc/rsync.password [root@rsync ~]# ls -ld /etc/rsync.password -rw-------. 1 root root 20 Sep 22 19:27 /etc/rsync.password
[root@rsync ~]# mkdir /backup -p [root@rsync ~]# chown -R rsync.rsync /backup [root@rsync ~]# ll -ld /backup/ drwxr-xr-x. 12 rsync rsync 4096 Sep 23 19:17 /backup/
注意:如果沒有 /backup 目錄,就會出現 chdir failed。
[root@rsync ~]# rsync --daemon [root@rsync ~]# ps -ef | grep rsync | grep -v grep failed to create pid file /var/run/rsyncd.pid: File exists root 25117 1 0 Sep27 ? 00:00:00 rsync --daemon [root@rsync ~]# lsof -i :873 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsync 25117 root 4u IPv4 35555 0t0 TCP *:rsync (LISTEN) rsync 25117 root 5u IPv6 35556 0t0 TCP *:rsync (LISTEN)
[root@rsync ~]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local [root@rsync ~]# tail -1 /etc/rc.local /usr/bin/rsync --daemon
[root@rsync ~]# rpm -qa rsync rsync-3.0.6-12.el6.x86_64
客戶端只需密碼本身:
[root@rsync ~]# echo "rsync" > /etc/rsync.password [root@rsync ~]# cat /etc/rsync.password rsync
[root@rsync ~]# chmod 600 /etc/rsync.password [root@rsync ~]# ls -ld /etc/rsync.password -rw-------. 1 root root 20 Sep 22 19:27 /etc/rsync.password
[root@rsync ~]# mkdir /backup -p [root@rsync ~]# ll -ld /backup/
推送:
rsync -avz --delete /tmp/ rsync_backup@10.0.0.8::rsync \
--password-file=/etc/rsync.password
# --delete 請謹慎使用
rsync -avz /tmp/ rsync://rsync_backup@10.0.0.8/backup/ \
--password-file=/etc/rsync.password
拉取:
rsync -avz rsync_backup@10.0.0.8::rsync /tmp/ \
--password-file=/etc/rsync.password
rsync -avz rsync://rsync_backup@10.0.0.8/backup/ /tmp/ \
--password-file=/etc/rsync.password
提示:上述的 backup 是模塊名,不是路徑。
原文出自「宋某人c」博客:https://syaving.blog.51cto.com/5614476/1862916