一,rsync
rsync是一款开源的,快速的,多功能的,可实现全量及增量的本地或远程数据同步备份的工具。
1,数据同步原理
Centos6,rsync3.x比对方法,一边比对差异,一边对差异部分进行备份
2,rsync的工作原理
(1)单个主机本地之间的数据传输(类似cp)
(2)借助rcp,ssh等通道来传输数据(类似scp)
(3)以守护进程(socket)的方式传输数据
3,本地传输命令
命令格式: rsync 参数 源 目的
-v | 显示详细信息 |
---|---|
-z | 传输时压缩 |
-a | 表示以递归方式传输文件 |
(1)把系统的hosts文件同步到/opt目录
[root@localhost ~]# rsync /etc/hosts /opt
[root@localhost ~]# cat /opt/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
(2)把opt目录拷贝到/mnt下
[root@localhost ~]# rsync -avz /opt /mnt/
sending incremental file list
opt/
opt/hosts
sent 173 bytes received 39 bytes 424.00 bytes/sec
total size is 158 speedup is 0.75
[root@localhost ~]# cp -ap /opt /mnt
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# ls
opt
(3)删除
[root@localhost ~]# rsync -avz --delete /old/ /tmp/
sending incremental file list
deleting vmware-root_6521-1723667692/
deleting vmware-root_6519-1690112741/
deleting vmware-root_6518-726305083/
deleting vmware-root_6515-1681789792/
sent 42 bytes received 979 bytes 2,042.00 bytes/sec
total size is 0 speedup is 0.00
注:相当于/tmp与空文件/old同步
(4)带宽限制
[root@localhost ~]# rsync -avz dbfile 192.168.3.129:/backup
没有带宽限制
[root@localhost ~]# rsync -avz --bwlimit=100 defile 192.168.3.129:/backup
--bwlimit限制带宽
(5)本地备份
[root@localhost ~]# cd /opt/
[root@localhost opt]# mkdir test
[root@localhost opt]# touch test/test1
[root@localhost opt]# chmod -R 700 test(授权)
1)仅把/opt目录下的内容同步
[root@localhost opt]# rsync -avz /opt/ /tmp/
sending incremental file list
./
hosts
test/
test/test1
sent 262 bytes received 65 bytes 654.00 bytes/sec
total size is 158 speedup is 0.48
2)把/opt本身及内部内容都同步
[root@localhost opt]# rsync -avz /opt /tmp/
sending incremental file list
opt/
opt/hosts
opt/test/
opt/test/test1
sent 271 bytes received 66 bytes 674.00 bytes/sec
total size is 158 speedup is 0.47
4,通过shell在不同主机间传输
(1)将当前内容推送到远程主机
[root@localhost ~]# rsync -avzP -e 'ssh -p 22' /etc/ root@192.168.3.48:/tmp/
root@192.168.3.48's password:
sending incremental file list
./
.pwd.lock
0 100% 0.00kB/s 0:00:00 (xfr#1, ir-chk=1184/1186)
.updated
163 100% 0.00kB/s 0:00:00 (xfr#2, ir-chk=1183/1186)
……
(2)将远程主机拉取到当前主机
[root@localhost ~]# rsync -avzP -e 'ssh -p 22' root@192.168.3.48:/opt/ /tmp/
root@192.168.3.48's password:
receiving incremental file list
./
sent 27 bytes received 37 bytes 6.74 bytes/sec
total size is 0 speedup is 0.00
5,通过socket方式传输
(1)创建两个Centos7的虚拟机,并关闭防火墙
[root@localhost ~]# systemctl stop firewalld(关闭防火墙)
[root@localhost ~]# systemctl disable firewalld(禁用防火墙)
[root@localhost ~]# setenforce 0(关闭selinux防火墙)
[root@localhost ~]# getenforce(查看selinux防火墙状态是否关闭)
[root@localhost ~]# vi /etc/sysconfig/selinux(修改selinux防火墙配置文件)
对A服务器
(2)判断rsync是否安装
[root@localhost ~]# rpm -qa rsync
rsync-3.1.2-4.el7.x86_64
(3)修改rsyncd.cof的配置文件
[root@localhost ~]# cat /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
uid = root
gid = root
use chroot = no
max connections = 200
pid file = /var/run/rsyncd.pid
exclude = lost+found/
# transfer logging = yes
timeout = 300
# ignore nonreadable = yes
# dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
# [ftp]
# path = /home/ftp
# comment = ftp export area
[backup]
path = /backup/
ignore errors
read only = false
hosts allow = 192.168.3.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
(4)创建共享目录及添加rsync用户
[root@localhost ~]# useradd -M -s /sbin/nologin rsync
[root@localhost ~]# id rsync
[root@localhost ~]# mkdir /backup
注:创建的用户名和目录名要和/etc/rsyncd.conf里的保持一致
(5)启动rsync服务
[root@localhost ~]# rsync --daemon(启动)
[root@localhost ~]# netstat -antup | grep rsync(查看是否启动)
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 7259/rsync
tcp6 0 0 :::873 :::* LISTEN 7259/rsync
(6)修改
[root@localhost ~]# chown -R rsync:rsync /backup/
[root@localhost ~]# chmod -R 755 /backup/
(7)创建rsync虚拟账户和密码
[root@localhost ~]# echo "rsync_backup:123456" > /etc/rsync.password
(8)修改账户和密码的权限
[root@localhost ~]# chmod 600 /etc/rsync.password
(9)加入开机启动
[root@localhost ~]# vi /etc/rc.local
客户端
(1)安装rsync,并启动
[root@localhost ~]# yum install rsync
[root@localhost ~]# netstat -antup | grep rsync
(2)创建rsync虚拟账户和密码
[root@localhost ~]#echo "123456" > /etc/rsync.password
(3)修改账户和密码的权限
[root@localhost ~]# chmod 600 /etc/rsync.password
(4)创建和服务器A对应的目录名
[root@localhost ~]# mkdir /backup
(5)在/backup目录里输入内容测试
[root@localhost backup]# touch test
[root@localhost backup]# rsync -avz /backup/ rsync_backup@192.168.3.129::backup --password-file=/etc/rsync.password
sending incremental file list
./
test
sent 106 bytes received 46 bytes 304.00 bytes/sec
total size is 0 speedup is 0.00