rsync,remote synchronize顾名思意就知道它是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限、时间、软硬链接等附加信息。
默认情况ubuntu安装了rsync服务,但在/etc下没有配置文件,一般情况可以copy示例文件到/etc下
- 安装sync,xinetd
1 |
$ sudo apt-get install rsync xinetd |
- 拷贝示例配置文件到/etc目录下
1 |
$ sudo cp /usr/share/doc/rsync/examples/rsyncd.conf /etc |
查看内容,可以看到如下内容
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 41 42 |
# sample rsyncd.conf configuration file # GLOBAL OPTIONS #motd file=/etc/motd #log file=/var/log/rsyncd # for pid file, do not use /var/run/rsync.pid if # you are going to run rsync out of the init.d script. # pid file=/var/run/rsyncd.pid #syslog facility=daemon #socket options= # MODULE OPTIONS [ftp] comment = public archive path = /var/www/pub use chroot = yes # max connections=10 lock file = /var/lock/rsyncd # the default for read only is yes... read only = yes list = yes uid = nobody gid = nogroup # exclude = # exclude from = # include = # include from = # auth users = # secrets file = /etc/rsyncd.secrets strict modes = yes # hosts allow = # hosts deny = ignore errors = no ignore nonreadable = yes transfer logging = no # log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes. timeout = 600 refuse options = checksum dry-run dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz |
- 修改同步的目录,用户名,密码,日志信息
1 |
$ sudo vim /etc/rsyncd.conf |
1.修改
1 |
path = /var/www/pub |
为需要同步的目录。
如果路径中存在空格,则要分两种情况处理,如果空格在路径中间,如"/nfs/Public/Shared Videos",则直接写
1 |
path = /nfs/Public/Shared Videos |
如果空格在路径的最后面,如"/nfs/Public/Shared Videos ",则需要如下形式设置,注意最后面的"\ ",是一个反斜杠加空格,否则最后的空格会被忽略。
1 |
path = /nfs/Public/Shared Videos\ |
2.设置可以登录的用户名,密码,修改
1 2 |
# auth users = # secrets file = /etc/rsyncd.secrets |
为
1 2 |
auth users = user secrets file = /etc/rsyncd.secrets |
配置用户名和密码
1 2 3 |
$ sudo vim /etc/rsyncd.secrets user:password |
赋予权限 rsyncd.secrets的权限必须为600
1 |
$ sudo chmod 600 /etc/rsyncd.secrets |
3.开启日志
1 |
#log file=/var/log/rsyncd |
为
1 |
log file=/var/log/rsyncd |
4.如果提示
1 |
rsync: change_dir "/" (in ftp) failed: Permission denied (13) |
则调整
1 2 |
uid = nobody gid = nogroup |
为
1 2 |
uid = root gid = root |
5.对于严格要求一致性的重要的文件,去掉"refuse options"中的"checksum",这样会导致同步变慢,但是会比较安全(已经有报告说当同步时候不校验MD5会出现文件大小一致但是MD5不正确的情况),这个需要客户端在同步的时候使用 "-c" 作为参数。
1 |
refuse options = dry-run |
- 编辑/etc/default/rsync 启动rsync作为使用xinetd的守护进程
1 |
$ sudo vim /etc/default/rsync |
修改
1 |
RSYNC_ENABLE=inetd |
创建 /etc/xinetd.d/rsync 通过xinetd使rsync开始工作
1 2 3 4 5 6 7 8 9 10 11 12 |
$ sudo vim /etc/xinetd.d/rsync service rsync { disable = no socket_type = stream wait = no user = root server = /usr/bin/rsync server_args = --daemon log_on_failure += USERID } |
启动/重启 xinetd
1 |
$ sudo /etc/init.d/xinetd restart |
- 测试
运行下面的命令检查,确认rsync配置成功。
1 2 3 4 |
$ sudo rsync user@www.mobibrw.com::ftp Password: drwxr-xr-x 4096 2006/12/13 09:41:59 . drwxr-xr-x 4096 2006/11/23 18:00:03 folders |
- 从服务器同步文件
1 2 3 4 5 |
$ sudo rsync -cvazu --progress user@www.mobibrw.com::ftp /rsync Password: receiving incremental file list ./ ................................... |
c同步完成后校验文件MD5(慢,但是可靠)
v详细提示
a以archive模式操作,复制目录、符号连接
z压缩
u只进行更新,防止本地新文件被重写,注意两者机器的时钟的同步
--progress指显示进度
注意,如果需要为多个目录做独立的配置,可以参考如下配置(配置中设置了两个独立的同步目录"ftp"跟"movie")
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# sample rsyncd.conf configuration file # GLOBAL OPTIONS #motd file=/etc/motd log file=/var/log/rsyncd # for pid file, do not use /var/run/rsync.pid if # you are going to run rsync out of the init.d script. # pid file=/var/run/rsyncd.pid #syslog facility=daemon #socket options= # MODULE OPTIONS [ftp] comment = public archive path=/nfs/MyCloud use chroot = yes # max connections=10 lock file = /var/lock/rsyncd # the default for read only is yes... read only = yes list = yes uid = root gid = root # exclude = # exclude from =# hosts allow = # hosts deny = # include = # include from = auth users = user secrets file = /etc/rsyncd.secrets strict modes = yes # hosts allow = # hosts deny = ignore errors = no ignore nonreadable = yes transfer logging = no # log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes. timeout = 600 refuse options = dry-run dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz [movie] comment = public archive path= /nfs/Public/Shared Videos use chroot = yes # max connections=10 lock file = /var/lock/rsyncd # the default for read only is yes... read only = yes list = yes uid = root gid = root # exclude = # exclude from = # include = # include from = auth users = user secrets file = /etc/rsyncd.secrets strict modes = yes # hosts allow = # hosts deny = ignore errors = no ignore nonreadable = yes transfer logging = no # log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes. timeout = 600 refuse options = dry-run dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz |
- 通过计划任务定时同步
1.创建脚本文件,假定文件在/home目录
1 2 3 4 |
$ vim rsync_backup.sh #!/bin/sh rsync -auvz --password-file=/home/rsyncd.secrets user@www.mobibrw.com::ftp /rsync |
2.赋予执行权限
1 |
$ chmod +x rsync_backup.sh |
3.创建密码文件,自动填写密码
1 2 3 |
$ vim /home/rsyncd.secrects password |
4.创建定时任务,每半小时自动检查备份一次
- 编辑计划任务文件,执行日志输出到 “/var/log/cron_rsync_backup.log”
1 2 3 |
$ vim cron_rsync_backup */30 * * * * /home/rsync_backup.sh > /var/log/cron_rsync_backup.log 2>&1 |
- 增加计划任务
1 |
$ crontab /home/cron_rsync_backup |
- 重启计划任务,使之生效
1 |
$ /etc/init.d/cron restart |
- 检查是否已经成功增加计划
1 |
$ crontab -l |