可实现全量及增量、本地或远程的数据同步备份的工具
官网:rsync(1) man page
相当于cp(本地复制)、scp(远程复制)、rm(删除)命令的合体但比这三个命令更优秀
既能够备份数据内容,也能够备份属性信息
常用参数:avz或az
-v 详细模式输出,传输时的进度等信息
-z 传输时进行压缩以提高传输效率。--compress-level=NUM可按级别压缩
-a 归档模式,表示以递归方式传输文件,并保持文件说有属性,等于rtopgDI
-r 对子目录以递归传递,即目录下的所有目录都同样传输,注意是小写r
-t 保持稳健时间信息
-o 保持文件属主信息
-p 保持文件权限
-g 保持文件属组信息
-P 显示同步的过程及传输的进度等信息
-D 保持设备文件信息
-l 保留软链接
-e 使用指定的shell程序替代传输用的rsh管道,如ssh
--exclude=PATTERN 指定排除不需要传输的文件模式(和tar参数一样)
--exclude-from=file 指定排除某个目录中的某个文件(和tar参数一样)
--bwlimit=RATE 限制传输时的带宽(避免全部占用完带宽)
--partial 断点续传
--delete 让目标目录和源目录数据保持一致(不一致会删除)
rsync [OPTION...] 源路径 目的路径
拉: rsync [OPTION...] [远端用户@]主机名或IP:源路径 目标路径
推: rsync [OPTION...] 源路径 [远端用户@]主机名或IP:目标路径
PS1:前面的是原路径,后面的是目标路径,推和拉的区别仅仅是远端地址所在的位置
PS2:远端用户名可省略,省略代表用本机当前用户去连接
rsync -avz /etc/hosts /tmp
rsync -avz /etc/hosts root@192.168.1.66:/tmp/
rsync -avz root@192.168.1.66:/etc/hosts /tmp/
rsync -avz -e 'ssh -p 22' /etc/hosts root@192.168.1.66:/tmp/
rsync -avz --exclude=a.txt /backup/ root@192.168.1.66:/tmp/
rsync -avz --exclude-from=paichu.txt /backup/ root@192.168.1.66:/tmp/
--exclude参数使用相对路径,可以排除单个和多个文件
也可以用--exclude-from=paichu.txt,排除这个文件中指定的文件
rsync -avz --delete /backup/ root@192.168.1.66:/tmp/
--delete会把服务器上有而本地没有的数据全部删除,非常危险,慎用慎用
sync -avz --bwlimit=100 /backup/ root@192.168.1.66:/tmp/
搭建服务端启动守护进程(873端口),然后在客户端推或拉数据进行备份,比普通远程模式好的地方在于,免交互的备份数据,配合定时任务食用更加.
语法
拉: rsync [OPTION...] [USER@]HOST::SRC DEST
推: rsync [OPTION...] SRC [USER@]HOST::DEST
PS:推拉的区别同普通模式,用的更多的是推模式,因为拉的模式客户端数量很大时,会非常消耗服务端资源,而推模式请求由客户端发起,不占用服务端资源
rpm -qa rsync
在线安装
yum install -y rsync
离线安装
rpm -ivh rsync-3.1.2-10.el7.x86_64.rpm
先检查是否有安装,如果有则安装
2.添加rsync服务的用户
useradd -s /sbin/nologin -M rsync
id rsync
cat >/etc/rsync/rsyncd.conf <<"EOF"
##rsyncd.conf start##
uid = root
gid = root
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
fake super = yes
list = false
hosts allow = 192.168.1.66 192.168.1.95 172.50.56.20
#hosts deny = 0.0.0.0/32
auth users = rsync
secrets file = /etc/rsync/rsync.password
[backup]
comment = "backup dir by noah"
path = /backup/
read only = false
EOF
关于配置文件各行的说明见文档最后
3.创建认证文件并修改权限
echo 'rsync:123456' >/etc/rsync.password
/etc/rsync.password
chmod -R 600 /etc/rsync.password
ls -l /etc/rsync.password
此处的用户和密码,非系统中的用户和密码
6) 启动rsync服务加入开机启动
#启动rsync服务,systemctl start rsyncd 启动也可
/usr/bin/rsync --daemon
netstat -nltp|grep 873
#设置开机启动服务
echo "/usr/bin/rsync --daemon" >>/etc/rc.local
tail -1 /etc/rc.local
rpm -qa rsync
在线安装
yum install -y rsync
离线安装
rpm -ivh rsync-3.1.2-10.el7.x86_64.rpm
echo '123456' >/etc/rsync.password
chmod -R 600 /etc/rsync.password
#只需要密码不需要用户名
启动rsync客户端。
#启动rsync服务,systemctl start rsyncd 启动也可
/usr/bin/rsync --daemon
#设置开机启动服务
echo "/usr/bin/rsync --daemon" >>/etc/rc.local
3) 同步文件:源文件--目标文件 [推送到到服务端rsyncd.conf配置文件路径目录]
rsync -avz --delete --password-file=/etc/rsync.password /data/backup/mysql rsync@172.50.56.21::backup/
提示:在客户端执行命令。ip后面跟的backup是服务器配置文件中的模块名,不是备份路径
uid = rsync #用户,远端的客户机使用rsync用户来访问共享目录
gid = rsync #用户组
use chroot = no #安全相关
max connections = 200 #最大连接数
timeout = 300 #超时时间
pid file = /var/run/rsyncd.pid #进程对应的进程号文件
lock file = /var/run/rsync.lock #锁文件,防止文件不一致
log file = /var/log/rsyncd.log #日志文件
ignore errors #忽略错误
list = false #不允许列表(ls)
hosts allow = 192.168.1.0/24 #白名单,允许的网段
#hosts deny = 0.0.0.0/32 #黑名单,拒绝的网段
auth users = rsync #链接的虚拟用户,非系统用户
secrets file = /etc/rsync..password #虚拟用户的账号密码文件
[backup] #模块名称,可以有多个,称为多模块
comment = "backup dir by noah" 模块描述信息
path = /backup/ #服务器提供的共享目录
read only = false #可写
注意不要在配置文件中某一行后面用#号写注释,识别不了的,注释用的#号必须写在行首
2) 黑白名单三种情况说明:
注:报错1:rsync: chgrp "." (in backup) failed: Permission denied (13)
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
关闭SELinux:
1、临时关闭(不用重启机器):
setenforce 0 ##设置SELinux 成为permissive模式
##setenforce 1 设置SELinux 成为enforcing模式
2、修改配置文件需要重启机器:
修改/etc/selinux/config 文件
将SELINUX=enforcing改为SELINUX=disabled
重启机器即可