特点
可以镜像保存整个目录树和文件系统。
可以很容易做到保持原来文件的权限、时间、软硬链接等等。
无须特殊权限即可安装。
快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。rsync 在传输数据的过程中可以实行压缩及解压缩操作,
rsync 的传输方式至少可以透过三种方式来运作:
在本机上直接运作,用法就与 cp 几乎一模一样,例如:
rsync -av /etc /tmp (将 /etc/ 的数据备份到 /tmp/etc 内)透过 rsh 或 ssh 的信道在 server / client 之间进行数据传输,例如:
rsync -av -e ssh user@rsh.server:/etc /tmp (将 rsh.server 的 /etc 备份到本地主机的 /tmp 内)直接透过 rsync 提供的服务 (daemon) 来传输,此时 rsync 主机需要启动 873 port:
1. 你必须要在 server 端启动 rsync , 看 /etc/xinetd.d/rsync 即可;
2. 你必须编辑 /etc/rsyncd.conf 配置文件;
3. 你必须设定好 client 端联机的密码数据;
4. 在 client 端可以利用:rsync -av user@hostname::/dir/path /local/path
介绍第二种方式比较常用 基于ssh信道加密并进行镜像传输
服务器端设置
[root@www ~]# yum -y install rsync
[root@www ~]# vim /etc/xinetd.d/rsync
service rsync
{
disable = no
bind = 127.0.0.1
only_from =127.0.0.1/8
no_access =127.0.0{100,200}
instances =UNLIMITED
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
service rsync
{
disable = no
bind = 192.168.129.152
only_from = 192.168.128.0/23
access_times = 01:00-9:00 20:00-23:59
instances = 10
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
[root@www xinetd.d]# /etc/init.d/xinetd restart
[root@www xinetd.d]# grep -i 'disable' /etc/xinetd.d/*
/etc/xinetd.d/rsync: disable = no
[root@www ~]# rsync [-avrlptgoD] [-e ssh] [user@host:/dir] [/local/path]选项与参数: -v :观察模式,可以列出更多的信息,包括镜像时的档案档名等; -q :与 -v 相反,安静模式,略过正常信息,仅显示错误讯息; -r :递归复制!可以针对『目录』来处理!很重要! -u :仅更新 (update),若目标档案较新,则保留新档案不会覆盖; -l :复制链接文件的属性,而非链接的目标源文件内容; -p :复制时,连同属性 (permission) 也保存不变! -g :保存源文件的拥有群组; -o :保存源文件的拥有人; -D :保存源文件的装置属性 (device) -t :保存源文件的时间参数; -I :忽略更新时间 (mtime) 的属性,档案比对上会比较快速; -z :在数据传输时,加上压缩的参数! -e :使用的信道协议,例如使用 ssh 通道,则 -e ssh -a :相当于 -rlptgoD ,所以这个 -a 是最常用的参数了! 更多说明请参考 man rsync 的解说!
客户端操作
远程备份
[Jade@localhost:~/Documents/]% rsync -av /etc root@192.168.129.152:/tmp
由于ssh的端口改变 需要指定ssh端口
[Jade@localhost:~/Documents/]% rsync -av -e ’ssh -p 4444’ /etc root@192.168.129.152:/tmp
本地备份
[Jade@localhost:~/Documents/]% rsync -av /etc /tmp
....(前面省略)....sent 21979554 bytes received 25934 bytes 4000997.82 bytes/sec total size is 21877999 speedup is 0.99 [root@www ~]# ll -d /tmp/etc /etc drwxr-xr-x. 106 root root 12288 Jul 26 16:10 /etc drwxr-xr-x. 106 root root 12288 Jul 26 16:10 /tmp/etc <==瞧!两个目录一样!
转载于:https://blog.51cto.com/wanjiadi/1874214