Ubuntu14.04下搭建Rsync服务并设置定时文件同步

Rsync服务用于两台Linux主机之间同步文件,其中客户端会根据配置自动同步服务端指定路径下的文件,以达到备份文件的目的。Rsync服务采用的是增量更新算法,当一个文件改动部分区域时不会传输整个文件,因此同步效率较高,以下是简单的安装步骤。

1、安装rsync软件包

apt-get install rsync

2、修改rsyncd.conf配置文件
查看/etc/init.d/rsync中配置文件的位置,一般为:/etc/rsyncd.conf
vi /etc/rsyncd.conf
写入:
motd file = /etc/rsyncd.motd
secrets file = /etc/rsyncd.scrt
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log

port = 873
use chroot = yes
hosts allow=120.26.224.51
max connections = 5

[software]
path = /home/deploy/software
comment = This is the web app dir.
uid = root
gid = root
read only = false
auth users = root
transfer logging = yes
exclude = dir1/

3、修改/etc/rsyncd.motd配置文件,写入服务器欢迎信息
vi /etc/rsyncd.motd
Welcome to my rsync server!

4、修改/etc/rsyncd.scrt密码配置文件
vi /etc/rsyncd.scrt
user1:password1
user2:password2

更改密码文件权限为600
chmod 600 /etc/rsyncd.scrt

5、修改/etc/default/rsync配置,设置启用
RSYNC_ENABLE=true

6、服务器重启
service rsync restart

7、客户端测试连接
rsync  --list-only [email protected]::software

8、同步数据
rsync  -avzp  --delete  [email protected]::software dir

9、同步数据不用输入密码
创建密码文件
vi rsyncd.scrt
写入用户密码:password
修改权限为600
chmod 600 rsyncd.scrt
同步
rsync  -avzp  --delete --password-file=rsyncd.scrt [email protected]::software dir

10、使用crontab设置定时同步
将同步命令写入shell脚本
vi rsync.sh

每天凌晨3点开始同步
crontab -e
00 3 * * * /data/aliyun/rsync.sh >> /data/aliyun/log.log


注意:脚本里所有路径必须写绝对路径,默认当前目录为该用户家目录

猜你喜欢

转载自blog.csdn.net/u010227646/article/details/80014241