目录
rsync is an open source utility that provides fast incremental file transfer. rsync is freely available under the GNU General Public License version 2 and is currently being maintained by Wayne Davison.
过程 4.1. rsync
安装rsync
在AS3 第二张CD上找到rsync-2.5.6-20.i386.rpm
[root@linuxas3 root]# cd /mnt [root@linuxas3 mnt]# mount cdrom [root@linuxas3 mnt]# cd cdrom/RedHat/RPMS [root@linuxas3 RPMS]# rpm -ivh rsync-2.5.6-20.i386.rpm
配置/etc/rsyncd.conf
在rh9,as3系统上rsync安装后,并没有创建rsyncd.conf文档,要自己创建rsyncd.conf文档
[root@linuxas3 root]# vi /etc/rsyncd.conf uid=nobody gid=nobody max connections=5 use chroot=no log file=/var/log/rsyncd.log pid file=/var/run/rsyncd.pid lock file=/var/run/rsyncd.lock #auth users=root secrets file=/etc/rsyncd.passwd [postfix] path=/var/mail comment = backup mail ignore errors read only = yes list = no auth users = postfix [netkiller] path=/home/netkiller/web comment = backup 9812.net ignore errors read only = yes list = no auth users = netkiller [pgsqldb] path=/var/lib/pgsql comment = backup postgresql database ignore errors read only = yes list = no
选项说明
uid = nobody gid = nobody use chroot = no # 不使用chroot max connections = 4 # 最大连接数为4 pid file = /var/run/rsyncd.pid #进程ID文件 lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log # 日志记录文件 secrets file = /etc/rsyncd.pwd # 认证文件名,主要保存用户密码,权限建议设为600,所有者root [module] # 这里是认证的模块名,在client端需要指定 path = /var/mail # 需要做镜像的目录 comment = backup xxxx # 注释 ignore errors # 可以忽略一些无关的IO错误 read only = yes # 只读 list = no # 不允许列文件 auth users = postfix # 认证的用户名,如果没有这行,则表明是匿名 [other] path = /path/to... comment = xxxxx
密码文件
在server端生成一个密码文件/etc/rsyncd.pwd
[root@linuxas3 root]# echo postfix:xxx >>/etc/rsyncd.pwd [root@linuxas3 root]# echo netkiller:xxx >>/etc/rsyncd.pwd [root@linuxas3 root]# chmod 600 /etc/rsyncd.pwd
启动rsync daemon
[root@linuxas3 root]# rsync --daemon
添加到启动文件
echo "rsync --daemon" >> /etc/rc.d/rc.local [ OK ]
cat /etc/rc.d/rc.local 确认一下
测试
[root@linux docbook]# rsync rsync://netkiller.8800.org/netkiller [root@linux tmp]# rsync rsync://netkiller@netkiller.8800.org/netkiller Password: [chen@linux temp]$ rsync -vzrtopg --progress --delete postfix@netkiller.8800.org::postfix /tmp Password:
过程 4.2. installation setp by setp
installation
$ sudo apt-get install rsync
enable
$ sudo vim /etc/default/rsync RSYNC_ENABLE=true
config /etc/rsyncd.conf
$ sudo vim /etc/rsyncd.conf uid=nobody gid=nobody max connections=5 use chroot=no pid file=/var/run/rsyncd.pid lock file=/var/run/rsyncd.lock log file=/var/log/rsyncd.log #auth users=root secrets file=/etc/rsyncd.secrets [neo] path=/home/neo/www comment = backup neo ignore errors read only = yes list = no auth users = neo [netkiller] path=/home/netkiller/public_html comment = backup netkiller ignore errors read only = yes list = no auth users = netkiller [mirror] path=/var/www/netkiller.8800.org/html/ comment = mirror netkiller.8800.org exclude = .svn ignore errors read only = yes list = yes [music] path=/var/music comment = backup music database ignore errors read only = yes list = no [pgsqldb] path=/var/lib/pgsql comment = backup postgresql database ignore errors read only = yes list = no auth users = neo,netkiller
/etc/rsyncd.secrets
$ sudo vim /etc/rsyncd.secrets neo:123456 netkiller:123456
$ sudo chmod 600 /etc/rsyncd.secrets
start
$ sudo /etc/init.d/rsync start
test
$ rsync -vzrtopg --progress --delete neo@localhost::neo /tmp/test1/ $ rsync -vzrtopg --progress --delete localhost::music /tmp/test2/
firewall
$ sudo ufw allow rsync
yum install xinetd
配置 /etc/xinetd.d/rsync
vim /etc/xinetd.d/rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
# allows crc checksumming etc.
service rsync
{
disable = yes
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
disable = yes 改为 disable = no
# vim /etc/rsyncd.conf
chkconfig xinetd on /etc/init.d/xinetd restart
# Minimal configuration file for rsync daemon # See rsync(1) and rsyncd.conf(5) man pages for help # This line is required by the /etc/init.d/rsyncd script pid file = /var/run/rsyncd.pid port = 873 address = 192.168.1.171 #uid = nobody #gid = nobody uid = root gid = root use chroot = yes read only = yes #limit access to private LANs hosts allow=192.168.1.0/255.255.255.0 10.0.1.0/255.255.255.0 hosts deny=* max connections = 5 motd file = /etc/rsyncd/rsyncd.motd #This will give you a separate log file #log file = /var/log/rsync.log #This will log every file transferred - up to 85,000+ per user, per sync #transfer logging = yes log format = %t %a %m %f %b syslog facility = local3 timeout = 300 [home] path = /home list=yes ignore errors auth users = linux secrets file = /etc/rsyncd/rsyncd.secrets comment = linuxsir home exclude = beinan/ samba/ [beinan] path = /opt list=no ignore errors comment = optdir auth users = beinan secrets file = /etc/rsyncd/rsyncd.secrets [www] path = /www/ ignore errors read only = true list = false hosts allow = 172.16.1.1 hosts deny = 0.0.0.0/32 auth users = backup secrets file = /etc/backserver.pas [web_user1] path = /home/web_user1/ ignore errors read only = true list = false hosts allow = 202.99.11.121 hosts deny = 0.0.0.0/32 uid = web_user1 gid = web_user1 auth users = backup secrets file = /etc/backserver.pas [pub] comment = Random things available for download path = /path/to/my/public/share read only = yes list = yes uid = nobody gid = nobody auth users = pub secrets file = /etc/rsyncd.secrets
transfer file from src to dest directory
neo@netkiller:/tmp$ mkdir rsync neo@netkiller:/tmp$ cd rsync/ neo@netkiller:/tmp/rsync$ ls neo@netkiller:/tmp/rsync$ mkdir src dest neo@netkiller:/tmp/rsync$ echo file1 > src/file1 neo@netkiller:/tmp/rsync$ echo file2 > src/file2 neo@netkiller:/tmp/rsync$ echo file3 > src/file3
skipping directory
neo@netkiller:/tmp/rsync$ mkdir src/dir1 neo@netkiller:/tmp/rsync$ mkdir src/dir2 neo@netkiller:/tmp/rsync$ rsync src/* dest/ skipping directory src/dir1 skipping directory src/dir2
recurse into directories
neo@netkiller:/tmp/rsync$ rsync -r src/* dest/ neo@netkiller:/tmp/rsync$ ls dest/ dir1 dir2 file1 file2 file3
backup
neo@netkiller:/tmp/rsync$ rsync -r --backup --suffix=.2008-11-21 src/* dest/ neo@netkiller:/tmp/rsync$ ls dest/ dir1 dir2 file1 file1.2008-11-21 file2 file2.2008-11-21 file3 file3.2008-11-21 neo@netkiller:/tmp/rsync$
backup-dir
neo@netkiller:/tmp/rsync$ rsync -r --backup --suffix=.2008-11-21 --backup-dir mybackup src/* dest/ neo@netkiller:/tmp/rsync$ ls dest/ dir1 dir2 file1 file1.2008-11-21 file2 file2.2008-11-21 file3 file3.2008-11-21 mybackup neo@netkiller:/tmp/rsync$ ls dest/mybackup/ file1.2008-11-21 file2.2008-11-21 file3.2008-11-21
rsync -r --backup --suffix=.2008-11-21 --backup-dir ../mybackup src/* dest/ neo@netkiller:/tmp/rsync$ ls dest mybackup src neo@netkiller:/tmp/rsync$ ls src/ dir1 dir2 file1 file2 file3
update
neo@netkiller:/tmp/rsync$ rm -rf dest/* neo@netkiller:/tmp/rsync$ rsync -r -u src/* dest/ neo@netkiller:/tmp/rsync$ echo netkiller>>src/file2 neo@netkiller:/tmp/rsync$ rsync -v -r -u src/* dest/ building file list ... done file2 sent 166 bytes received 42 bytes 416.00 bytes/sec total size is 38 speedup is 0.18
update by time and size
neo@netkiller:/tmp/rsync$ echo Hi>src/dir1/file1.1 neo@netkiller:/tmp/rsync$ rsync -v -r -u src/* dest/ building file list ... done dir1/file1.1 sent 166 bytes received 42 bytes 416.00 bytes/sec total size is 41 speedup is 0.20
--archive
rsync -a src/ dest/
--compress
rsync -a -z src/ dest/
--delete
src
svn@netkiller:~$ ls src/ dir1 dir2 file1 file2 file3
dest
neo@netkiller:~$ rsync -v -u -a --delete -e ssh svnroot@127.0.0.1:/home/svnroot/src /tmp/dest svnroot@127.0.0.1's password: receiving file list ... done created directory /tmp/dest src/ src/file1 src/file2 src/file3 src/dir1/ src/dir2/ sent 104 bytes received 309 bytes 118.00 bytes/sec total size is 0 speedup is 0.00
src
svn@netkiller:~$ rm -rf src/file2 svn@netkiller:~$ rm -rf src/dir2
dest
neo@netkiller:~$ rsync -v -u -a --delete -e ssh svnroot@127.0.0.1:/home/svnroot/src /tmp/dest svnroot@127.0.0.1's password: receiving file list ... done deleting src/dir2/ deleting src/file2 src/ sent 26 bytes received 144 bytes 68.00 bytes/sec total size is 0 speedup is 0.00
http://samba.anu.edu.au/rsync/examples.html
$ rsync -v -u -a --delete --rsh=ssh --stats localfile username@hostname:/home/username/
for example:
I want to copy local workspace of eclipse directory to another computer.
$ rsync -v -u -a --delete --rsh=ssh --stats workspace neo@192.168.245.131:/home/neo/
rsync使用方法
rsync rsync://认证用户@主机/模块
rsync -vzrtopg --progress --delete 认证用户@主机::模块 /mirror目录
例 4.1. examples
用rsync删除目标目录
mkdir /root/blank
rsync --delete-before -a -H -v --progress --stats /root/blank/ ./cache/
例 4.2. backup to a central backup server with 7 day incremental
#!/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
例 4.3. backup to a spare disk
I do local backups on several of my machines using rsync. I have an
extra disk installed that can hold all the contents of the main
disk. I then have a nightly cron job that backs up the main disk to
the backup. This is the script I use on one of those machines.
#!/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
The first part does the backup on the spare disk. The second part
backs up the critical parts to daily directories. I also backup the
critical parts using a rsync over ssh to a remote machine.
例 4.4. mirroring vger CVS tree
The vger.rutgers.edu cvs tree is mirrored onto cvs.samba.org via
anonymous rsync using the following script.
#!/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
Note in particular the initial rsync of the ChangeLog to determine if
anything has changed. This could be omitted but it would mean that the
rsyncd on vger would have to build a complete listing of the cvs area
at each run. As most of the time nothing will have changed I wanted to
save the time on vger by only doing a full rsync if the ChangeLog has
changed. This helped quite a lot because vger is low on memory and
generally quite heavily loaded, so doing a listing on such a large
tree every hour would have been excessive.
例 4.5. automated backup at home
I use rsync to backup my wifes home directory across a modem link each
night. The cron job looks like this
#!/bin/sh
cd ~susan
{
echo
date
dest=~/backup/`date +%A`
mkdir $dest.new
find . -xdev -type f \( -mtime 0 -or -mtime 1 \) -exec cp -aPv "{}"
$dest.new \;
cnt=`find $dest.new -type f | wc -l`
if [ $cnt -gt 0 ]; then
rm -rf $dest
mv $dest.new $dest
fi
rm -rf $dest.new
rsync -Cavze ssh . samba:backup
} >> ~/backup/backup.log 2>&1
note that most of this script isn't anything to do with rsync, it just
creates a daily backup of Susans work in a ~susan/backup/ directory so
she can retrieve any version from the last week. The last line does
the rsync of her directory across the modem link to the host
samba. Note that I am using the -C option which allows me to add
entries to .cvsignore for stuff that doesn't need to be backed up.
例 4.6. Fancy footwork with remote file lists
One little known feature of rsync is the fact that when run over a remote shell (such as rsh or ssh) you can give any shell command as the remote file list. The shell command is expanded by your remote shell before rsync is called. For example, see if you can work out what this does: rsync -avR remote:'`find /home -name "*.[ch]"`' /tmp/ note that that is backquotes enclosed by quotes (some browsers don't show that correctly).
#!/usr/bin/perl
my $path = "/data"; #本地目录
my $ip="172.16.xxx.xxx"; #远程目录
my $maxchild=5; #同时并发的个数
open FILE,"ls $path|";
while()
{
chomp;
my $filename = $_;
my $i = 1;
while($i<=1){
my $un = `ps -ef |grep rsync|grep -v grep |grep avl|wc -l`;
$i =$i+1;
if( $un < $maxchild){
system("rsync -avl --size-only $path/$_ $ip:$path &") ;
}else{
sleep 5;
$i = 1;
}
}
}
命令行选项 -v, --verbose 增强可读性 -q, --quiet 忽略非错误信息 --no-motd 忽略daemon模式的MOTD信息 (see manpage caveat) -c, --checksum 基于checksum校验,而非mod-time和size -a, --archive 归档模式,和-rlptgoD (不要使用 -H,-A,-X)参数相同 --no-OPTION 关闭一些显式的OPTION (比如 --no-D) -r, --recursive 递归目录 -R, --relative 使用相对路径 --no-implied-dirs 不发送制定目录的属性,避免在目标使用--relative删除连接重新传输文件, -b, --backup 备份 (查看 --suffix & --backup-dir) --backup-dir=DIR 基于DIR来创建备份的目录结构 --suffix=SUFFIX 制定备份的后最 (default ~ w/o --backup-dir) -u, --update 跳过接受端较新的文件 --inplace 直接在文件上更新(SEE MAN PAGE) --append 直接追加文件 --append-verify 和--append很像, 只是用文件的较旧的那部份做checksum -d, --dirs 仅传输文件而非递归 -l, --links 将软链接当作软链接来拷贝 -L, --copy-links 拷贝链接对应的文件或者目录而非链接本身 --copy-unsafe-links 仅不安全的链接才被拷贝 --safe-links 忽略那些链接到目录树外的链接 -k, --copy-dirlinks 将链接翻译成其链接的目录 -K, --keep-dirlinks 将目录链接在接收端转换成目录 -H, --hard-links 保留硬链接 -p, --perms 保留权限 -E, --executability 保留文件的可执行性 --chmod=CHMOD 改变文件或者目录的权限 -A, --acls 保留ACLs (implies --perms) -X, --xattrs 保留扩展属性 -o, --owner 保留所有者(super-user only) -g, --group 保留组 --devices 保留设备文件 (super-user only) --specials 保留特殊文件 -D 和 --devices --specials一样 -t, --times 保留修改时间 -O, --omit-dir-times 在--times选项里忽略目录的时间 --super 允许接受方执行那个一些超级用户的活动 --fake-super 通过xattrs来存储和回复权限 -S, --sparse 高效处理稀疏文件 -n, --dry-run 只运行,不做改变 -W, --whole-file 拷贝整个文件(without delta-xfer algorithm) -x, --one-file-system 不跨越文件系统 -B, --block-size=SIZE 强制指定checksum的块大小 -e, --rsh=COMMAND 指定要使用的远程shell --rsync-path=PROGRAM 指定远程要运行的rsync路径 --existing 如果接收端不存在文件就不创建 --ignore-existing 如果接收端存在就不更新文件 --remove-source-files 发送端删除已经同步的文件 (non-dirs) --del an alias for --delete-during --delete 删除接收端上在发送端不存在的文件 --delete-before 接收端在发送前删除,而不是发送过程中 --delete-during 接收端在发送过程中删除 --delete-delay 在发送过程中寻找文件,在发送完成后删除 --delete-after 接收端在发送完成后删除 --delete-excluded 在接收端删除排除的文件 --ignore-errors 即使有I/O错误也删除 --force 即使目录不是空的也删除 --max-delete=NUM 最多删除文件的数目 --max-size=SIZE 最大传输文件的大小 --min-size=SIZE 最小传输文件的大小 --partial 保持部分传输的文件 --partial-dir=DIR 制定部分传输文件的存放目录 --delay-updates 先传输,最后再更新,保持原子性 -m, --prune-empty-dirs 接收端删除空目录 --numeric-ids 接收端不要将uid/gid映射为用户名和组名 --timeout=SECONDS 设置I/O超时时间,s为单位 --contimeout=SECONDS 设置链接服务端的时间 -I, --ignore-times 即使mtime和size都相同也不跳过 --size-only 只要大小相同就跳过 --modify-window=NUM 比对时间时制定精确范围,范围内都认为时间相同 -T, --temp-dir=DIR 指定创建临时文件的目录 -y, --fuzzy 文件在接收端不存在的情况下,在当前目录下寻找一个基础文件,以加快传输 --compare-dest=DIR 接收端除了和发送端对比还和这里指定的目录对比,适合备份上次备份改变的文件 --copy-dest=DIR 和--compare-dest类似,只是接收端会用本地拷贝来复制那些未改变的文件 --link-dest=DIR 和--compare-dest类似,只是接收端会建立那些未改变文件的硬链接 -z, --compress 传输过程中压缩 --compress-level=NUM 指定压缩等级 --skip-compress=LIST 不压缩指定后缀的文件 -C, --cvs-exclude 以CSV的方式自动忽略文件 -f, --filter=RULE 新增一个file-filtering规则 -F same as --filter='dir-merge /.rsync-filter' repeated: --filter='- .rsync-filter' --exclude=PATTERN 排除规则PATTERN --exclude-from=FILE 从文件中读取排除规则 --include=PATTERN 不要排除指定规则的文件 --include-from=FILE 从文件中读取包含的规则 --files-from=FILE 从文件中读取文件列表 -0, --from0 all *-from/filter files are delimited by 0s -s, --protect-args 参数不许要空格分割; only wildcard special-chars --address=ADDRESS 绑定监听的地址 --port=PORT 制定端口号 --sockopts=OPTIONS 制定TCP选项 --blocking-io 在远程shell中使用blocking I/O --stats 给出文件统计信息 -8, --8-bit-output 输出时不对高位字符转义 -h, --human-readable 以易于阅读的方式打印数字 --progress 显示传输进度 -P same as --partial --progress -i, --itemize-changes 打印更新的总结信息 --out-format=FORMAT 以特定的格式打印更新信息 --log-file=FILE 日志文件 --log-file-format=FMT 日志文件格式 --password-file=FILE 密码文件 --list-only 仅列出文件 --bwlimit=KBPS 限制带宽; KBytes per second --write-batch=FILE 将批量更新写入文件 --only-write-batch=FILE 和 --write-batch类似 but w/o updating destination --read-batch=FILE 从文件中读取批量更新任务 --protocol=NUM 使用旧版本的协议 --iconv=CONVERT_SPEC 要求文件名字符转义 -4, --ipv4 prefer IPv4 -6, --ipv6 prefer IPv6 --version 打印帮助信息 (-h) --help 打印这个帮组信息 (-h 仅在单独使用时与 --help 同意)
模拟运行,显示日志,但不做复制操作。
rsync -anvzP /www/* root@172.16.0.1/www
速度限制,限制为 100k Bytes/s
rsync -auvzP--bwlimit=100 /www/* root@172.16.0.1/www
rsync -auzv --rsh=ssh root@202.130.101.33:/www/example.com/* /backup/example.com/ # --rsh=ssh 可以省略 rsync -auzv root@202.130.101.33:/www/example.com/* /backup/example.com/
如果需要特别参数,可以这样写,这里指定连接SSH的端口为20
rsync -auzv --rsh='ssh -p20' root@202.130.101.34:/www/example.com/* /backup/example.com/