ubuntu 12.04配置rsync服务

rsync,remote synchronize顾名思意就知道它是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限、时间、软硬链接等附加信息。

默认情况ubuntu安装了rsync服务,但在/etc下没有配置文件,一般情况可以copy示例文件到/etc下

  • 安装sync,xinetd
$ sudo apt-get install rsync xinetd
  • 拷贝示例配置文件到/etc目录下
$ sudo cp /usr/share/doc/rsync/examples/rsyncd.conf /etc

查看内容,可以看到如下内容

# 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
  • 修改同步的目录,用户名,密码,日志信息
$ sudo vim /etc/rsyncd.conf

1.修改

path = /var/www/pub

为需要同步的目录。
如果路径中存在空格,则要分两种情况处理,如果空格在路径中间,如"/nfs/Public/Shared Videos",则直接写

path = /nfs/Public/Shared Videos

如果空格在路径的最后面,如"/nfs/Public/Shared Videos ",则需要如下形式设置,注意最后面的"\ ",是一个反斜杠加空格,否则最后的空格会被忽略。

path = /nfs/Public/Shared Videos\

2.设置可以登录的用户名,密码,修改

# auth users =
# secrets file = /etc/rsyncd.secrets

auth users = user
secrets file = /etc/rsyncd.secrets

配置用户名和密码

$ sudo vim /etc/rsyncd.secrets 

  user:password

赋予权限 rsyncd.secrets的权限必须为600

$ sudo chmod 600 /etc/rsyncd.secrets

3.开启日志

#log file=/var/log/rsyncd

log file=/var/log/rsyncd

4.如果提示

rsync: change_dir "/" (in ftp) failed: Permission denied (13)

则调整

uid = nobody
gid = nogroup

uid = root
gid = root

5.对于严格要求一致性的重要的文件,去掉"refuse options"中的"checksum",这样会导致同步变慢,但是会比较安全(已经有报告说当同步时候不校验MD5会出现文件大小一致但是MD5不正确的情况),这个需要客户端在同步的时候使用 "-c" 作为参数。

 refuse options = dry-run
  • 编辑/etc/default/rsync 启动rsync作为使用xinetd的守护进程
$ sudo vim /etc/default/rsync

修改

RSYNC_ENABLE=inetd

创建 /etc/xinetd.d/rsync 通过xinetd使rsync开始工作

$ 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

$ sudo /etc/init.d/xinetd restart
  • 测试

运行下面的命令检查,确认rsync配置成功。

$ 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
  • 从服务器同步文件
$ 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"

# 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目录

$ vim rsync_backup.sh

#!/bin/sh 
rsync -auvz --password-file=/home/rsyncd.secrets user@www.mobibrw.com::ftp /rsync

2.赋予执行权限

$ chmod +x rsync_backup.sh

3.创建密码文件,自动填写密码

$ vim /home/rsyncd.secrects

password

4.创建定时任务,每半小时自动检查备份一次

  • 编辑计划任务文件,执行日志输出到 “/var/log/cron_rsync_backup.log”
$ vim cron_rsync_backup

*/30 * * * *  /home/rsync_backup.sh > /var/log/cron_rsync_backup.log 2>&1
  •  增加计划任务
$ crontab /home/cron_rsync_backup
  •  重启计划任务,使之生效
$ /etc/init.d/cron restart
  • 检查是否已经成功增加计划
$ crontab -l

发布者

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注