日期:2014-05-16  浏览次数:20763 次

文件同步,rsync本机linux实践,成功
最近需要用rsync实现文件同步。

复制rsync实例配置文档到/etc下
sudo cp /usr/share/doc/rsync/examples/rsyncd.conf /etc
sudo vim /etc/rsyncd.conf
 # sample rsyncd.conf configuration file
  2 
  3 # GLOBAL OPTIONS
  4 
  5 #motd file=/etc/motd
  6 #log file=/var/log/rsyncd
  7 # for pid file, do not use /var/run/rsync.pid if
  8 # you are going to run rsync out of the init.d script.
  9 pid file=/var/run/rsyncd.pid
 10 #syslog facility=daemon
 11 #socket options=
 12 
 13 # MODULE OPTIONS
 14 
 15 [ftp]
 16 
 17     comment = public archive
 18     path = /home/user/adms/materials/
 19     use chroot = yes
 20 #   max connections=10
 21     lock file = /var/lock/rsyncd
 22 # the default for read only is yes...
 23     read only = yes
 24     list = yes
 25     uid = nobody
 26     gid = nogroup
 27 #   exclude =
 28 #   exclude from =
 29 #   include =
 30 #   include from =
 31 #   auth users =
 32 #   secrets file = /etc/rsyncd.secrets
 33     strict modes = yes
 34 #   hosts allow =
 35 #   hosts deny =
 36     ignore errors = no
 37     ignore nonreadable = yes
 38     transfer logging = no
 39 #   log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.
 40     timeout = 600
 41     refuse options = checksum dry-run

可以说除了path,其他参数基本没改变

sudo vim /etc/rsyncd.secrets

写入
 #用户名:密码
rsyncuser:123456

更改使用权限
chmod 600 /etc/reync.secrets

启动服务
/etc/init.d/rsync start

查看873端口是否启动
sudo lsof -i:873

查看rsync服务是否启动
ps aux | grep rsync


同步测试
rsync -vzrtopg --progress --delete rsyncuser@192.168.2.254::ftp /home/rsync/temp/

其中rsyncuser是rsync服务器端提供的用户名,保证有写入权限
/home/rsync/temp/是rsync客户端存储文件的地址
192.168.2.254是rsync服务端ip地址
ftp是rsync服务端配置的模块名称


设置定时同步

使用linux的cron服务:
将以上同步命令写入脚本比如materialrsync.sh,保存在文件系统中,比如/home/huan/materialrsync/
执行sudo cronteb -e写入任务计划,如
1 * * * * /home/huan/materialrsync/materialrsync.sh
表示每分钟执行一次
具体应根据实际需要设置任务计划。