Rsync常用的一款同步文件工具。配置也很方便。
yum install sync -y或者apt-get install sync
- 服务安装好之后,/etc下没有配置文件,一般情况可以copy示例文件到/etc下
#cp /usr/share/doc/rsync/examples/rsyncd.conf /etc
vim /etc/rsyncd.conf
uid = common
gid = common
use chroot = no
max connections = 10
strict modes = yes
hosts allow = 192.168.2.12 #可以空格,允许多个
hosts deny = *
port = 5699 #默认开启端口
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[hmt]
path = /data/www/html/
comment = hello
ignore errors
read only = no
write only = no
hosts allow = 192.168.2.12
list = false
auth users = hmt
secrets file = /etc/rsync.password
- 建立/etc/rsyncd/rsyncd.secrets文件
#vim /etc/rsync.password
#cat /etc/rsyncd/rsyncd.secrets
hmt:123456
- 为了密码的安全性,我们必须把权限设为600
chown root:root /etc/rsyncd/rsyncd.secrets
chmod 600 /etc/rsync.password
chmod 600 /etc/rsyncd/rsyncd.secrets
vim /etc/rsyncd/rsyncd.motd
启动rsync:
systemctl start rsyncd.service
lsof -i:5699
lsof -i:5699
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 750 root 4u IPv4 22661487 0t0 TCP *:5699 (LISTEN)
rsync 750 root 5u IPv6 22661488 0t0 TCP *:5699 (LISTEN)
至此服务器端安装完毕;
客户端安装配置:
yum -y install rsync
客户端需要vim /etc/rsync.password
cat /etc/rsync.password
123456
运行:
rsync -avz –port 5699 –password-file=/etc/rsync.password hmt@192.168.2.12::hmt /data/www/html
Password: 这里要输入rsync的密码,是服务器端提供的,在前面的例子中,我们用的是 asdf,输入的密码并不显示出来;输好后就回车; 注: 这个命令的意思就是说,用rsync 用户登录到服务器上,把/data/www/html 数据,同步到本地目录/data/www/html/上。当然本地的目录是可以你自己定义的,比如 dave也是可以的;当你在客户端上,当前操作的目录下没有/tmp/test/这个目录时,系统会自动为你创建一个;当存在/tmp/test/这个目录中,你要注意它的写权限。
参数说明:
-a 参数,相当于-rlptgoD,
-r 是递归 -l 是链接文件,意思是拷贝链接文件;
-p 表示保持文件原有权限;
-t 保持文件原有时间;
-g 保持文件原有用户组;
-o 保持文件原有属主;
-D 相当于块设备文件;
-z 传输时压缩;
-P 传输进度;
-v 传输时的进度等信息,和-P有点关系
*
rsync -avzP –delete rsync@192.168.2.150::hometools /tmp/test/
–delete 选项,表示服务器上的数据要与客户端完全一致,如果 /tmp/test/目录中有服务器上不存在的文件,则删除。最终目的是让/tmp/test/目录上的数据完全与服务器上保持一致;用的时候要小心点,最好不要把已经有重要数所据的目录,当做本地更新目录,否则会把你的数据全部删除;
计划任务同步:
*/5 * * * * rsync -zvaP rsync@192.168.2.150::hometools /tmp/test/ –password-file=/etc/rsyncd/rsyncd.secrets
*/5 * * * * rsync -avz –port 5699 –password-file=/etc/rsync.password hmt@192.168.2.12::hmt /data/www/html