您的位置:首页 > 运维架构 > Linux

Linux下的数据备份工具rsync

2018-02-23 15:32 781 查看
rsync(remote sync): 远程同步,可以本地同步数据,不会覆盖以前的数据,而是先判断已经存在的数据和新数据的差异,只有数据不同时才会把不相同的部分覆盖。
# yum install rsync.x86_64 -y
本地同步
[root@apenglinux-001 ~]# rsync -av /etc/passwd /tmp/password
sending incremental file list
passwd

sent 959 bytes  received 31 bytes  1980.00 bytes/sec
total size is 885  speedup is 0.89
远程同步(同步到另一台机器,另一台机器也需要安装rsnyc)
[root@apenglinux-001 ~]# rsync -av /etc/passwd 192.168.221.20:/tmp/password
root@192.168.221.20's password:
sending incremental file list
passwd

sent 959 bytes  received 31 bytes  220.00 bytes/sec
total size is 885  speedup is 0.89
rsync的命令格式

rsync [option]... src dest
rsync [option]... src [user@]host:dest
rsync [option]... [user@]host:src dest
rsync [option]... [user@]host::src dest
rsync [option]... src [user@]host::dest
rsync常用选项
-a 这是归档模式,表示以递归方式传输文件,并保持所有属性,它等同于-rlptgoD。-a选项后面可以跟一个--no-option,表示关闭-rlptgoD中的某一个。如-a--no-l=-rptgoD
-r 表示递归模式处理子目录
-v 表示打印一些信息,比如文件列表,文件数量等
-l 表示保留软链接,不拷贝软链接对应的源文件
-L 表示要拷贝源文件中软链接对应的源文件
-p 表示保持文件权限
-o 表示保持文件属主信息
-g 表示保持文件属组信息
-D 表示保持设备文件信息
-t 表示保持文件时间信息
--delete 表示删除dest中src没有的文件
--exclude=pattern 表示排除不需要传输的文件
-P 表示在同步的过程中可以看到同步的过程状态,比-v更加详细
-u 表示把dest中比src还新的文件排除,不会覆盖
-z 将会在传输过程中压缩
通过ssh方式同步
[root@apenglinux-001 ~]# rsync -av /etc/passwd -e "ssh -p 22" root@192.168.221.20:/tmp/password
root@192.168.221.20's password:
sending incremental file list
passwd
sent 959 bytes  received 31 bytes  282.86 bytes/sec
total size is 885  speedup is 0.89
通过服务同步(c/s模式)

server:192.168.221.10
# yum install rsync -y
# vim /etc/rsyncd.conf
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsync.pid
address=192.168.221.10
[test]
path=/root/rsync
use chroot=true
maxconnections=4
read only=no
list=true
uid=root
gid=root
auth users=test
secrets file=/etc/rsyncd.passwd
hosts allow=192.168.221.20
# rsync --daemon --config=/etc/rsyncd.conf
# mkdir /root/rsync
# chmod 777 /root/rsync
# vim /etc/rsyncd.passwd //在里面写上"test:apeng"
# chmod 600 /etc/rsyncd.passwd
# systemctl stop firewalld
client: 192.168.221.20
# yum install rsync -y
# systemctl stop firewalld
# rsync -avP /etc/fstab test@192.168.221.10::test //输入用户名对应的密码
#
#
# vim /etc/pass_file  //里面写上"apeng"
# chmod 600 /etc/pass_file
# rsync -avP /etc/passwd --password-file=/etc/pass_file  test@192.168.221.10::test //可以省去输入密码的步骤
在服务端不指定用户(注释掉下面两行,就可以不用再输入密码了)
#auth users=test
#secrets file=/etc/rsyncd.passwd
注意:如果修改了/etc/rsyncd.conf,不用重启服务。但如果修改了端口号,要让其生效,则要杀死原有的rsync进程,再重启服务
# yum install psmisc.x86_64 -y
# killall rsync
# rsync --daemon --config=/etc/rsyncd.conf
# netstat -tlnp|grep rsync
端口号变了,则同步时如下(需指定--port)

# rsync -avP /etc/issue --port=8730 --password-file=/etc/pass_file test@192.168.221.10::test
/etc/rsyncd.conf中,如果list=true,则在客户端可列出模块号
[root@apenglinux-002 ~]# rsync --port=8730 192.168.221.10::
test
/etc/rsyncd.conf中,如果 use chroot=true,不能备份指向外部的符号链接所指向的目录文件,加上-L会报错。use chroot=false,加上-L,可以备份软链接所指向的目录文件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  rsync 数据备份