目录
一、rsync概述
Rsync(Remote Sync)是Linux系统下的数据镜像备份工具。该工具可以实现远程同步、不同主机之间的同步,也能实现全量备份和增量备份,保持数据链接和权限,并采用优化的同步算法,传输前对数据进行压缩,故该工具非常适合架构集中式备份或异地备份。也支持本地复制或与ssh、rsync同步。
官网地址:https://rsync.samba.org/
优点:
-
scp无法备份大量数据,而rsync备份、统计、比较一起进行。
-
可以备份整个目录树和文件系统,并保持文件原来的权限、时间、软硬链接。
-
安装较容易,无需特殊权限。
-
同步快速,首次同步完全备份,再次同步增量备份。
-
可以使用scp和ssh等方式传输备份文件
-
支持匿名传输
-
选择性保持:符号链接、硬链接、文件属性、权限、时间等
-
传输速度快:压缩再传输、解压再使用,减少带宽。
[root@bogon ~]# rsync --version
rsync version 3.1.2 protocol version 31
Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
append, ACLs, xattrs, iconv, symtimes, prealloc
rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you
are welcome to redistribute it under certain conditions. See the GNU
General Public Licence for details.
rsync 版本 3.1.2 协议版本 31
版权所有 (C) 1996-2015 Andrew Tridgell、Wayne Davison 等人。
网站: http://rsync.samba.org/
能力:
64 位文件、64 位 inum、64 位时间戳、64 位长整数、
socketpairs, 硬链接, 符号链接, IPv6, batchfiles, inplace,
append、ACLs、xattrs、iconv、symtimes、prealloc
rsync 绝对不提供保修。 这是免费软件,而您
欢迎在特定条件下重新分发它。 参见 GNU
通用公共许可证 (General Public Licence) 了解详情。
备份分类:
-
完全备份:所有文件进行备份
-
差异备份:备份自上次完全备份以来所有的修改
-
增量备份:备份自上次备份依赖所作的修改(备份最小)
-
二、rsync运行原理
rsync采用C/S模式,即点到点的传输。通过xinetd服务监听873端口,再让xinetd服务下的rsync服务作出响应。
xinetd服务配置文件 : /etc/xinetd
rsync的配置文件:/etc/rsyncd.conf
源主机:需要同步数据的服务器 就记住数据在哪儿,哪儿就是源主机
目标主机:存放服务器同步数据的主机
数据同步方式:push 和 pull
-
推push:主动同步,把数据发送给目标主机。服务器开销大,适合后端服务器较少的情况。【服务器备份推给rsync客户端存放,主动模式】
目的主机配置为 rsync 服务端,源主机周期性的使用 rsync 命令把要同步的目录推过去。
-
拉pull:所有客户端主机去服务器上面拉数据,导致数据传输缓慢。【rsync客户端去服务器上拉数据,存放到客户端上,被动模式】
源主机配置为 rsync 服务端,目的主机周期性的使用 rsync 命令把要同步的目录拉过来。
参数 | 作用 |
---|---|
-a | –archive archive mode权限保存模式【递归、保持属性】 |
-r | 递归处理 |
-p | 保留文件原有属性 |
-t | 保留文件原有时间 |
-g | 保留属组 |
-o | 保留档案所有者 |
-D | 保留device咨询 |
-l | 复制所有的连接 |
-z | 压缩传输 |
-H | 保留硬链接文件 |
-A | 保留文件的ACL属性 |
-P | –progress |
-–version | 输出rsync版本信息 |
-v | 显示输出过程 |
-u | 仅进行更新 |
--port | 指定端口号,默认873 |
--delete | 删除那些目标位置有的文件而备份源没有的文件,最大程度的保持一致。 |
--password-file | 指定密码文件 |
--bwlimit | 限制I/O带宽 |
--filter | 需要过滤的文件 |
--exclude | 需要过滤的文件 |
--progress | 显示备份过程 |
--avz | 常用:保留权限、显示同步过程、压缩传输 |
三、rsync部署
实验步骤:
一、环境准备
服务端和客户端关不防火墙,关闭selinux
[root@client ~]# systemctl stop firewalld.service
[root@client ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@client ~]# getenforce
Disabled
[root@client ~]# setenforce 0
setenforce: SELinux is disabled
[root@server ~]# systemctl stop firewalld.service
[root@server ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@server ~]# getenforce
Disabled
[root@server ~]# setenforce 0
setenforce: SELinux is disabled
客户端安装rsync服务和xinetd服务
服务器安装rsync服务和xinetd服务
将rsync运行在daemon(守护进程)模式
[root@server ~]# yum install xinetd rsync -y
[root@client ~]# yum install xinetd rsync -y
# rsync运行在daemon模式
[root@server ~]# rsync --daemon
[root@client ~]# rsync --daemon
# 查看是否监听873端口号
[root@server ~]# netstat -anptu|grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 11303/rsync
tcp6 0 0 :::873 :::* LISTEN 11303/rsync
[root@client ~]# netstat -anpltu|grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 3742/rsync
tcp6 0 0 :::873 :::* LISTEN 3742/rsync
二、备份测试
服务器创建新目录,备份一份数据到新目录里,用于与客户端进行备份测试
# 由于rsync备份是会保持目录及其文件的权限、时间、软硬连接不变的,那要求备份在ssh传输过程中一路顺畅,不会因权限不足而备份失败。
# 这里创建test用户用于测试
[root@server ~]# useradd test && echo 123456 | passwd --stdin test
更改用户 test 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@client ~]# useradd test && echo 123456 | passwd --stdin test
更改用户 test 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@server ~]# mkdir /data
[root@server /]# ls -lh
drwxr-xr-x 2 root root 6 6月 19 21:01 data
[root@server /]# chown -R test:test /data
[root@server /]# ls -lh
drwxr-xr-x 2 test test 6 6月 19 21:01 data
[root@server /]# chmod g+s data
[root@server /]# setfacl -m u:test:rwx data
[root@server /]# getfacl data
# file: data
# owner: test
# group: test
# flags: -s-
user::rwx
user:test:rwx
group::r-x
mask::rwx
other::r-x
[root@server /]# cp -r /boot/* data
[root@server /]# ls -lh
drwxrwsr-x+ 5 test test 4.0K 6月 19 21:05 data
[root@client ~]# mkdir -p /data/backup
[root@client /]# ls data
backup
[root@client /]# chown test:test /data/backup
[root@client /]# ll -d /data/backup
drwxr-xr-x 2 test test 6 6月 19 21:11 /data/backup
[root@server /]# rsync -avz /data/ test@192.168.235.140:/data/backup
The authenticity of host '192.168.235.140 (192.168.235.140)' can't be established.
ECDSA key fingerprint is SHA256:mcPIFK1593usLRK8zsfuNNsBn39Jt2MC2L/GP1fhA1I.
ECDSA key fingerprint is MD5:00:85:24:3b:89:ba:ab:7e:35:49:f7:21:d2:0e:c7:f7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.235.140' (ECDSA) to the list of known hosts.
test@192.168.235.140's password:
sending incremental file list
./。。。。。。。。中间备份部分就省略了
sent 337,502,690 bytes received 120,275 bytes 13,240,116.27 bytes/sec
total size is 901,248,587 speedup is 2.67
[root@client /]# du -sh /data/backup
883M /data/backup
创建/rsyncd.conf配置文件
- 服务器:server 192.168.235.139 (需要进行数据备份的主机)
- 客户端:client 192.168.115.235140 (备份存储的主机)
1、客户端配置备份目录
------关闭selinux
------关闭防火墙
# 在客户端上编写rsync配置文件,创建一个存放备份的同步目录
[root@client ~]# vim /etc/rsyncd.conf
port=873
address = 192.168.235.140
uid = root
gid = root
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
motd file = /etc/rsyncd.motd
hosts allow = 192.168.235.0/24
[data]
path = /data/backup
comment = bakcup data
read only = false
list = yes
auth users = rsyncuser
secrets file = /etc/rsync.passwd
创建认证文件
# 创建密码文件,格式 用户名:密码
# 设置密码文件权限为600或者700
[root@client ~]# echo "rsyncuser:123456" > /etc/rsync.passwd
设置文件权限
[root@client ~]# chmod 600 /etc/rsync.passwd
[root@client ~]# chmod 600 /etc/rsyncd.conf
启用xinetd和rsync
[root@client /]# systemctl start xinetd
[root@client /]# cd
[root@client ~]# systemctl enable xinetd
[root@client ~]# ps aux | grep xinetd
root 1060 0.0 0.0 25044 588 ? Ss 08:35 0:00 /usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid
root 2151 0.0 0.0 112824 988 pts/0 S+ 08:49 0:00 grep --color=auto xinetd
关闭了防火墙就省略这一步,没有就运行这一步
[root@client ~]# firewall-cmd --permanent --zone=public --add-port=873/tcp
FirewallD is not running(防火墙没有运行)
puth推数据
同步过程都是服务端主动推送数据给目标主机
# rsync用户一般是第一个普通用户,即UID为1001,故备份的所有者和所属组都为1001
# 服务端推送数据到共享目录位置
#rsync 选项 需要备份的目录 rsync用户名@存放备份的服务器IP::共享模块名 --password-file=密码文件
# rsync -avz --delete /data/ rsyncuser@192.168.115.112::data(备份之后会自动删除源主机没有的数据)
[root@server data]# rsync -avz /data/ rsyncuser@192.168.235.140::data
Welcome to Backup Server
Password:
sending incremental file list
./
System.map-3.10.0-1160.el7.x86_64
config-3.10.0-1160.el7.x86_64
..........................................................................省略
如果不想输密码的话可在服务器上vim /etc/rsync.passwd
[root@server data]# vim /etc/rsync.passwd
[root@server data]# cat /etc/rsync.passwd
1
[root@server data]# chmod 600 /etc/rsync.passwd
[root@server data]# rsync -avz --delete /data rsyncuser@192.168.235.140::data --password-file=/etc/rsync.passwd
Welcome to Backup Server
sending incremental file list
data/
data/System.map-3.10.0-1160.el7.x86_64
data/config-3.10.0-1160.el7.x86_64
data/initramfs-0-rescue-22964ad04229458db602dfe97305f81d.img
data/initramfs-3.10.0-1160.el7.x86_64.img
data/lock -> ../run/lock
data/mail -> spool/mail
data/run -> ../run
data/symvers-3.10.0-1160.el7.x86_64.gz
pull拉取数据
目标主机主动拉取数据进行同步
先在服务器上配置背负目录
配置服务器 /etc/rsyncd.conf
[root@server /]# vim /etc/rsyncd.conf
port=873
address = 192.168.235.139
uid = root
gid = root
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
motd file = /etc/rsyncd.motd
hosts allow = 192.168.235.0/24
[data]
path = /data
comment = master data
read only = false
list = yes
auth users = rsyncuser
secrets file = /etc/rsync.passwd
~
创建认证文件
[root@server /]# vim /etc/rsync.passwd
[root@server /]# cat /etc/rsync.passwd
rsyncuser:1
设置文件权限
[root@server /]# chmod 600 /etc/rsync.passwd
[root@server /]# chmod 600 /etc/rsyncd.conf
启用xinetd和rsyncd服务
[root@server /]# systemctl start xinetd
[root@server /]# rsync --daemon --config=/etc/rsyncd.conf
[root@server /]# netstat -anpttu |grep 873
tcp 0 0 192.168.235.139:873 0.0.0.0:* LISTEN 5990/rsync
# 看到以上结果,说明服务端的rsync服务已经配置完成。
注意:如果没有关闭防火墙将执行这一步
# 开放873端口号,允许通过873端口号拉取数据
[root@server ~]# firewall-cmd --permanent --zone=public --add-port=873/tcp
success
[root@server ~]# firewall-cmd --reload
success
目标主机上拉取数据
[root@client data]# rsync -avz rsyncuser@192.168.235.139::data /data/backup
Password:
receiving incremental file list
./
backup/
backup/test/
backup/test/.bash_logout
backup/test/.bash_profile
backup/test/.bashrc
backup/z/
backup/z/.bash_logout
backup/z/.bash_profile
backup/z/.bashrc
sent 157 bytes received 1,188 bytes 244.55 bytes/sec
total size is 884 speedup is 0.66
如果不想输入密码,也是相同的方法,vim /etc/rsync.passwd
[root@client data]# vim /etc/rsync.passwd
[root@client data]# cat /etc/rsync.passwd
1
以上就是puth推数据和pull拉数据
rsync+sersync 实现数据实时同步
同步过程:
①同步服务器上开启sersync记录指定路径的文件系统变化情况。
②源服务器上使用rsync命令把变化的数据同步到目标服务器上。
③源服务器上配置sersync服务,目标服务器安装rsync服务。
开始部署sersync守护进程
# 解压并重命名为sersync
[root@server opt]# ll
总用量 712
-rw-r--r-- 1 root root 727290 3月 8 2017 sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@server opt]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@server opt]# ll
总用量 712
drwxr-xr-x 2 root root 41 10月 26 2011 GNU-Linux-x86
-rw-r--r-- 1 root root 727290 3月 8 2017 sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@server opt]# mv GNU-Linux-x86/ sersync
[root@server opt]# ll
总用量 712
drwxr-xr-x 2 root root 41 10月 26 2011 sersync
-rw-r--r-- 1 root root 727290 3月 8 2017 sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@server opt]# cd sersync/
[root@server sersync]# ls
confxml.xml sersync2
[root@server sersync]# vim confxml.xml #下面是要修改的部分
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
# 定义监控路径、同步的目标主机、共享模块
<sersync>
<localpath watch="/data"> #监控源主机目标路径
<remote ip="192.168.235.140" name="data"/> 这里是目标主机ip,服务端的配置文件名
</localpath>
<rsync>
<commonParams params="-artuz"/>
# 开启用户认证,并设置账号和密码
<auth start="true" users="rsyncuser" passwordfile="/etc/rsync.passwd"/>
[root@server ~]# /opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d run as a daemon
option: -r rsync all the local files to the remote servers before the sersync work
option: -o config xml name: /opt/sersync/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
will ignore the inotify createFile event
daemon start,sersync run behind the console
use rsync password-file :
user is rsyncuser
passwordfile is /etc/rsync.passwd
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data && rsync -artuz -R --delete ./ rsyncuser@192.168.235.140::data --password-file=/etc/rsync.passwd >/dev/null 2>&1
run the sersync:
watch path is: /data
启用sersync服务
# 开启sersync守护进程同步数据,即实时同步。
# -r参数,先做一次完全同步,再做差异同步。
[root@server ~]# /opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d run as a daemon
option: -r rsync all the local files to the remote servers before the sersync work
option: -o config xml name: /opt/sersync/confxml.xml
daemon thread num: 10
parse xml config file
注意:配置认证密码文件一定要写对,(服务端和客户端一定要分清)
设置rsync+sersync开机自启
# 设置开机自启
[root@server ~]# vim /etc/rc.d/rc.local
/opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml
# 如sersync正常运行,通过ps命令可以看到进程已经开启。
[root@server ~]# ps aux | grep sersync2 | grep -v 'grep'| wc -l