`

通过rsync实现同步

阅读更多
引自:http://blog.chinaunix.net/uid-1877180-id-303386.html
nohup rsync -zrtopg --delete --links root@172.27.0.16::root --exclude=*.iso --exclude /proc --exclude /dev --exclude /sys --exclude /tmp --exclude /boot --password-file=/etc/rsync.client.pass /backup/rsync/ &

1,rsync简介
Rsync是一个快速和异常灵活的文件复制工具,它能够通过remote shell (rsh,ssh)或remote rsync daemon 来实现本地到或从远程主机的复制。它提供了大量选项来控制行为的方方面面,能够弹性定义复制文件的属性,通过delta-transfer 算法,只发送源和目标不同的文件来降低网络数据传输量。rsync作为增强的copy命令广泛的用于镜像和备份。
rsync的额外特性:
1、支持复制文件保存文件的软硬链接,属主,属组,权限等。
2、类似于GNU tar的exclude ,exclude from选项。
3、 CVS排除模式,忽略CVS要求的相同文件。
4、能够便用任何远程shell,如rcp、ssh等方式来传输文。
5、不要求超级用户特 权。
6、流水式文件传输最小化传输延时。
7、支持匿名或验证的rsync。
在没有建立rsync服务器的情况下,我们可以利用 rsync命令直接进行快速的差异备份:
2,命令格式:
#rsync [option] 源路径 目标路径
其中:
[option]:
a:使用 archive模式,等于-rlptgoD,即保持原有的文件权限
z:表示传输时压缩数据
v:显示到屏幕中
e:使用远程 shell程序(可以使用rsh或ssh)
--delete:精确保存副本,源主机删除的文件,目标主机也会同步删除
--include=PATTERN: 不排除符合PATTERN的文件或目录
--exclude=PATTERN:排除所有符合PATTERN的文件或目录
--password- file:指定用于rsync服务器的用户验证密码
源路径和目标路径可以使用如下格式:
rsync://[USER@]Host[:Port]/Path     <--rsync服务器路径
[USER@]Host::Path                         <--rsync服务器的另一种表示形式
[USER@]Host:Path                          <--远程路径
LocalPath                                       <--本地路径
※ 需要注意的是,来源或目的路径最少要有一个是本地路径,如果忽略本地路径,则只会列出远端的文件列表。
例子:
#rsync -ave ssh test:/home/ftp/pub/ /home/ftp/pub/
把源路径中远端test机器上的/home/ftp/pub/目录中的内容,通过rsync同步到本地的/home/ftp/pub/目录下。
◎ 小心源路径结尾时候的/号,后缀/通知rsync复制该目录的内容,但不复制目录本身。
例如:
#rsync -ave ssh test:/home/ftp/pub /home/ftp/
则会把pub目录整个同步到本地/home/ftp/路径中
#rsync -azv --delete rsync://linuxing@192.168.1.100/blog /var/www/html/
通过linuxing登陆到192.168.1.100中,同步rsync服务器的blog项到本地的/var/www/html/,并删除本地上 源路径中不存在的文件或目录。
※千万要注意--delete参数,在使用此参数的时候,建议用绝对路径指定本地目录,防止清空当前目录。

3,服务器端设置:
修改配置文件
vi /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no             # 不使用chroot
max connections = 4
log file = /var/log/rsyncd.log   
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
[test]                          #rsync区段
path = /var/www/html/test       #需要同步的目录
comment =  test folder          #注释
ignore errors                   #忽略错误
read only = yes                 #只读
list = no                       #不能列表
auth users = webrsync           #连接rsync服务的用户名
secrets file = /etc/rsyncd.secrets    #指定存放帐号密码的位置
创建帐号密码文件:
vi /etc/rsync.pass
webrsync:myrsynpass
保 存后,需要保证用户是root,权限是600,否则会出现验证错误。
chown root.root /etc/rsync.pass
chmod 600 /etc/rsync.pass
启动服务:
rsync --daemon
设成开机启动的两种方式:
a,修改inetd.conf下的rsync
编辑/etc/services,加入rsync   873/tcp,指定rsync的服务端口是873
修改/etc /xinetd.conf下的rsync
sed -i -e "/disable/{ s/yes/no/ }" /etc/xinetd.d/rsync
service xinetd.d restart
b,将命令写到启动文件中
echo "rsync --daemon" >> /etc/rc.local
4,客设端设置:
rsync -vzrtopg --progress --delete webrsync@192.168.21.1::test /tmp/
password:
receiving file list ... done
下面这个命令行中-vzrtopg里的v是verbose,z是压缩,r是recursive,topg都是保持文件原有属性如 属主、时间的参数。--progress是指显示出详细的进度情况,--delete是指如
果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致。后面的webrsync@ip中,webrsync是指定密码文件中的用 户名,之后的::test这一inburst是模块名
,也就是在/etc/rsyncd.conf中自定义的名称。最后的/tmp是备份到本地的目录名。
在客户端通过指过--password-file,可以不用输入密码,这样在脚本只不有输入密码,这求在客户端也创建密码文件:
vi /etc/rsync.pass,只输入要验证用户的密码,chmod 600 /etc/rsync.pass
然后重新运行命令
rsync -vzrtopg --progress --delete webrsync@192.168.21.1::test /tmp/ --password-file=/etc/rsync.pass 
5,设成定时运行
crontab -e
0 2 * * *
rsync -vzrtopg --progress --delete webrsync@192.168.21.1::test /tmp/ --password-file=/etc/rsync.pass
或者编写脚本:
rsync.sh
#!/bin/sh
DATE=`date +%w`
rsync -vzrtopg --progress --delete webrsync@192.168.21.1::test /tmp/ --password-file=/etc/rsync.pass > /var/log/rsync.$DATE
chmod +x rsync.sh
添加到cron里运行。
6,其它使用方法:
比如我的客户端想要复制服务器端的两个或更多个文件或目录,可通过下面的方式:
rsync -av host:file1 :file2 host:file{3,4} /dest/
rsync -av host::modname/file{1,2} host::modname/file3 /dest/
rsync -av host::modname/file1 ::modname/file{3,4}
这里如我的客户端要从服务器端同步两个文件夹,test和 web
在服务器端的rsyncd.conf
[test]                         
path = /var/www/html/test #注意这里是test不是test/,它将在目标目录下新建文件夹test而不是复制test下的所有文件    
comment =  test folder         
ignore errors                  
read only = yes                
list = no                      
auth users = webrsync          
secrets file = /etc/rsyncd.secrets
[web]                       
path = /var/www/html/web   #同上   
comment =  web folder         
ignore errors                  
read only = yes                
list = no                      
auth users = webrsync          
secrets file = /etc/rsyncd.secrets
在客户端运行:
rsync -vzrtopg --progress --delete webrsync@192.168.21.1::test webrsync@192.168.21.1:web /tmp --password-file=/etc/rsync.pass
会在客户端的 tmp目录下新建两个文件夹test和web,将两个文件夹下的内容同步过来。
7,参考脚本:
1、每隔七天将数据往中心服务器做增量备份
#!/bin/sh
# This script does personal backups to a rsync backup server. You will end up
# with a 7 day rotating incremental backup. The incrementals will go
# into subdirectories named after the day of the week, and the current
# full backup goes into a directory called "current"
# tridge@linuxcare.com
# directory to backup
BDIR=/home/$USER
# excludes file - this contains a wildcard pattern per line of files to exclude
EXCLUDES=$HOME/cron/excludes
# the name of the backup machine
BSERVER=owl
# your password on the backup server
export RSYNC_PASSWORD=XXXXXX

########################################################################
BACKUPDIR=`date +%A`
OPTS="--force --ignore-errors --delete-excluded --exclude-from=$EXCLUDES
      --delete --backup --backup-dir=/$BACKUPDIR -a"
export PATH=$PATH:/bin:/usr/bin:/usr/local/bin
# the following line clears the last weeks incremental directory
[ -d $HOME/emptydir ] || mkdir $HOME/emptydir
rsync --delete -a $HOME/emptydir/ $BSERVER::$USER/$BACKUPDIR/
rmdir $HOME/emptydir
# now the actual transfer
rsync $OPTS $BDIR $BSERVER::$USER/current
2、备份至一个空闲的硬盘
#!/bin/sh
export PATH=/usr/local/bin:/usr/bin:/bin
LIST="rootfs usr data data2"
for d in $LIST; do
    mount /backup/$d
    rsync -ax --exclude fstab --delete /$d/ /backup/$d/
    umount /backup/$d
done
DAY=`date "+%A"`
   
rsync -a --delete /usr/local/apache /data2/backups/$DAY
rsync -a --delete /data/solid /data2/backups/$DAY
3、对vger.rutgers.edu的cvs树进行镜像
#!/bin/bash
cd /var/www/cvs/vger/
PATH=/usr/local/bin:/usr/freeware/bin:/usr/bin:/bin
RUN=`lps x | grep rsync | grep -v grep | wc -l`
if [ "$RUN" -gt 0 ]; then
    echo already running
    exit 1
fi
rsync -az vger.rutgers.edu::cvs/CVSROOT/ChangeLog $HOME/ChangeLog
sum1=`sum $HOME/ChangeLog`
sum2=`sum /var/www/cvs/vger/CVSROOT/ChangeLog`
if [ "$sum1" = "$sum2" ]; then
    echo nothing to do
    exit 0
fi
rsync -az --delete --force vger.rutgers.edu::cvs/ /var/www/cvs/vger/
exit 0
4、利用find的一种巧妙方式
rsync -avR remote:'`find /home -name "*.[ch]"`' /tmp/
可以用这种方法列出需要备份的文件列表——这种方法似乎比较少人用到。
五、参考资料:
1、http://rsync.samba.org/
2、rsync examples
3、rsync FAQ
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics