Rsync配置与使用

Date:2013-04-12 Author:阿债 Category:Linux Tag:rsync

服务端

安装rsync并创建配置目录和文件

1
2
3
4
5
6
7
#!/bin/bash
yum install rsync
mkdir -p /etc/rsync
touch /etc/rsync/rsyncd.conf /etc/rsync/rsyncd.secrets /etc/rsync/rsyncd.motd
echo "root:changeme" > /etc/rsync/rsyncd.secrets
echo 'Welcome !' > /etc/rsync/rsyncd.motd
chmod 600 /etc/rsync/rsyncd.secrets

/etc/rsync/rsyncd.conf文件内容如下,一定要设置为root用户

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
pid file = /var/run/rsyncd.pid
port = 873
address = 192.168.1.23
uid = root
gid = root

use chroot = yes
read only = no

#limit access to private LANs
#设置可访问的主机:如果多个ip则用空格隔开:192.168.0.3 192.168.0.4 192.168.0.5或者设置区间 192.168.0.3/5
hosts allow = 192.168.1.0/24
hosts deny = *

max connections = 5
motd file = /etc/rsync/rsyncd.motd

#This will give you a separate log file
log file = /var/log/rsync.log

#This will log every file transferred - up to 85,000+ per user, per sync
transfer logging = yes

log format = %t %a %m %f %b
syslog facility = local3
timeout = 300


[projects]
#要同步服务器的目录路径
path = /home/ryan/projects
list = yes
ignore errors
#真实的系统用户,多个用户用逗号隔开
auth users = root
#密码文件
secrets file = /etc/rsync/rsyncd.secrets
comment = projects
#不同步的目录或文件,用空格隔开
exclude = .svn/ *.rar

启动服务

1
rsync --config=/etc/rsync/rsyncd.conf --daemon

客户端

安装rsync并创建传输脚本

1
2
3
4
5
6
#!/bin/bash
mkdir -p /home/ryan/bin
touch /home/ryan/bin/rsync_projects.sh
chmod +x /home/ryan/bin/rsync_projects.sh
echo "changeme" > /home/ryan/bin/rsync.pass
chmod 600 /home/ryan/bin/rsync.pass

/home/ryan/bin/rsync_projects.sh的内容如下,为了在定时任务中使用,要用完整路径

1
2
3
4
#!/bin/bash
    #!/bin/bash
/usr/bin/rsync -vzrtopg --progress /home/ryan/projects/* \
    root@192.168.1.50::projects --password-file=/home/ryan/bin/rsync.pass