rsync是一款开源的,快速的,多功能的,可实现全量及增量的本地或远程数据同步备份的优秀工具。html
全量:将所有数据,进行传输覆盖
增量:只传输差别部分的数据redis
Rsync经过其独特的“快速检查”算法,实现增量传输数据算法
[root@backup ~]#man rsync Rsync finds files that need to be transferred using a “quick check” algorithm (by default) that looks for files that have changed in size or in last-modified time. Any changes in the other preserved attributes (as requested by options) are made on the destination file directly when the quick check indicates that the file’s data does not need to be updated.
在同步备份数据时,默认状况下,Rsync经过其独特的“quick check”算法,它仅同步大小或者最后修改时间发生变化的文件或目录,固然也可根据权限,属主等属性的变化同步,但须要指定相应的参数,甚至能够实现只同步一个文件里有变化的内容部分,因此,能够实现快速的同步备份数据。shell
centos 5 rsync 2.x 先比对再同步,速度较慢
centos 6 rsync 3.x 一边比对,一边对差别部分进行同步centos
[root@mico ~]# 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
相似于 cp 命令 -- 实现本地备份传输数据
相似于scp 命令 -- 远程备份传输数据
相似于 rm 命令 -- 实现无差别同步备份
相似于 ls 命令 -- 本地文件信息查看安全
rsync 命令属于1 v 4 的命令服务器
[root@mico practices]# cp -a /root/practices/source/tmp_file.txt tmp/ cp:是否覆盖"tmp/tmp_file.txt"? [root@mico practices]# rm -rf tmp/tmp_file.txt [root@mico practices]# cp -a /root/practices/source/tmp_file.txt tmp/ [root@mico practices]# ls tmp/ tmp_file.txt [root@mico practices]# rm tmp/tmp_file.txt rm:是否删除普通空文件 "tmp/tmp_file.txt"? [root@mico practices]# rsync /root/practices/source/tmp_file.txt tmp/ [root@mico practices]# ls tmp/ tmp_file.txt [root@mico practices]# ls tmp/tmp_file.txt tmp/tmp_file.txt
检查对端服务器目标位置上是否有该文件app
root@vicodona practices]# ls tmp/tmp_file.txt ls: cannot access tmp/tmp_file.txt: No such file or directory
从本地拷贝到远端服务器上ssh
[root@mico practices]# ls tmp/tmp_file.txt tmp/tmp_file.txt [root@mico practices]# scp -rp /root/practices/source/tmp_file.txt 47.107.108.121:/root/practices/tmp/ The authenticity of host '47.107.108.121 (47.107.108.121)' can't be established. ECDSA key fingerprint is SHA256:XRAgcbGsezufwlo5zaYbN4gO/8tS7pHAcNtfc7T1URA. ECDSA key fingerprint is MD5:e0:07:d1:80:ae:1f:8c:58:46:69:15:d6:46:0e:76:4f. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '47.107.108.121' (ECDSA) to the list of known hosts. root@47.107.108.121's password: tmp_file.txt 100% 0 0.0KB/s 00:00 [root@mico practices]#
检查远端服务器上的结果socket
[root@vicodona practices]# ls tmp/tmp_file.txt tmp/tmp_file.txt
[root@mico practices]# rsync -rp /root/practices/source/tmp_file.txt 47.107.108.121:/root/practices/tmp/ root@47.107.108.121's password: [root@mico practices]#
检查远端服务器上的结果
[root@vicodona practices]# ls tmp/tmp_file.txt tmp/tmp_file.txt
[root@mico practices]# ls source/tmp_file.txt tmp/tmp_file.txt [root@mico practices]# rm -rf source/tmp_file.txt [root@mico practices]# ll source/tmp_file.txt ls: 没法访问source/tmp_file.txt: 没有那个文件或目录 [root@mico practices]#
建立一个空目录,使用空目录进行无差别同步
[root@mico practices]# ll tmp/ 总用量 0 -rw-r--r-- 1 root root 0 7月 12 15:17 tmp_file.txt [root@mico practices]# mkdir null [root@mico practices]# rsync --delete null/ tmp/ rsync: --delete does not work without --recursive (-r) or --dirs (-d). rsync error: syntax or usage error (code 1) at main.c(1567) [client=3.1.2] [root@mico practices]# rsync -a --delete null/ tmp/ [root@mico practices]# ll tmp/ 总用量 0
使用 rsync 能够实现与ls相似的功能
[root@mico practices]# ls -l source/tmp_file.txt -rw-r--r-- 1 root root 0 7月 12 15:44 source/tmp_file.txt [root@mico practices]# rsync source/tmp_file.txt -rw-r--r-- 0 2019/07/12 15:44:57 tmp_file.txt [root@mico practices]#
#tar zcvf backup_1.tar.gz /opt/data -exclude=mico 说明:在打包/opt/data时就排除了mico命名的目录和文件。
# 将备份/home 目录自 2008-01-29 以来修改过的文件 # tar -N 2008-01-29 -zcvf /backups/inc-backup_$(date +%F).tar.gz /home # 将备份 /home 目录昨天以来修改过的文件 # tar -N $(date -d yesterday "+%F") -zcvf /backups/inc-backup_$(date +%F).tar.gz /home # 添加文件到已经打包的文件 # tar -rf all.tar *.gif 说明:这条命令是将全部.gif的文件增长到all.tar的包里面去。-r是表示增长文件的意思。
两台服务器之间数据同步(定时任务cron+rsync)
同步网站内部人员数据信息(定时任务最小周期为1分钟)
两台服务器之间数据同步(实时任务inotify/sersync/lrsyncd+rsync)
同步网站用户人员数据信息
SYNOPSIS 本地数据同步方式 Local: rsync [OPTION...] SRC... [DEST] 远程数据同步方式 Access via remote shell: Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST:DEST 守护进程方式同步数据 Access via rsync daemon: Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST] rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST::DEST rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
Local: rsync [OPTION...] SRC... [DEST]
参数 | 含义 |
---|---|
rsync | 数据同步命令 |
[OPTION...] | rsync命令参数信息 |
SRC | 要同不得数据信息(文件或目录) |
[DEST] | 将数据传输到什么位置 |
实例演示命令
[root@backup ~]# rsync /etc/hosts /tmp/ [root@backup ~]# ls /tmp/hosts /tmp/hosts
Access via remote shell: Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
说明:须要进行交互传输数据。若是想实现免交互传输数据,须要借助ssh+key方式实现
pull: 拉: | |
---|---|
[USER@] : | 以什么用户身份传输数据信息 |
HOST: | 远程主机信息(IP地址信息 主机名称信息) |
SRC: | 远端要恏过来的数据信息 |
[dest] | 恏到本地什么位置 |
push:推: | |
SRC: | 本地要怼过去的数据信息 |
DEST | 怼到远端什么位置 |
从远端拉文件到当前目录
[root@vicodona tmp]# touch 1.txt [root@vicodona tmp]# ll total 0 -rw-r--r-- 1 root root 0 Jul 13 16:00 1.txt [root@vicodona tmp]# pwd /root/practices/tmp ---------------------------------------- [root@mico tmp]# rsync 47.107.108.121:/root/practices/tmp/1.txt . root@47.107.108.121's password: [root@mico tmp]# ll 总用量 0 -rw-r--r-- 1 root root 0 7月 13 16:03 1.txt
将本地的tmp目录推到远端服务器上
[root@vicodona practices]# ls tmp ls: cannot access tmp: No such file or directory ---------------- [root@mico practices]# rsync -r /root/practices/tmp 47.107.108.121:/root/practices root@47.107.108.121's password: ---------------------------- [root@vicodona practices]# ll total 4 drwxr-xr-x 2 root root 4096 Jul 13 16:11 tmp
若是仅是推送目录下的文件并不包括目录自己,使用/tmp/
/tmp --表示将tmp目录自己及目录下的内容进行传输
/tmp/ --表示只传输tmp目录下面的内容信息
Access via rsync daemon: Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST] rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST::DEST rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
- vicodona 服务器做为rsync服务端
- 以rsync客户端做为参照物,将数据推到rsync服务器上
第一步:检查软件是否存在
[root@vicodona ~]# rpm -qa|grep rsync rsync-3.1.2-4.el7.x86_64
第二步:进行软件服务配置
[root@vicodona ~]# cat /etc/rsyncd.conf # /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man page for more options. # configuration example: # uid = nobody # gid = nobody # use chroot = yes # max connections = 4 # pid file = /var/run/rsyncd.pid # exclude = lost+found/ # transfer logging = yes # timeout = 900 # ignore nonreadable = yes # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # [ftp] # path = /home/ftp # comment = ftp export area uid = 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 read only = false list = false hosts allow = 47.107.108.121/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password fake super = yes [backup] comment = "backu dir by vicodona" path = /backup
第三步:建立rsync用户
[root@vicodona ~]# id rsync id: rsync: no such user [root@vicodona ~]# useradd -s /sbin/nologin -M rsync
第四步:建立数据备份存储目录,目录修改属主
[root@vicodona ~]# mkdir /backup/ [root@vicodona ~]# chown -R rsync.rsync /backup/
第五步:建立认证用户密码文件
[root@vicodona ~]# echo "rsync_backup:vicodona123">>/etc/rsync.password [root@vicodona ~]# chmod 600 /etc/rsync
第六步:启动rsync服务
[root@vicodona ~]# rsync --daemon
查看启动的服务
[root@vicodona ~]# ps -ef|grep rsync root 7860 1 0 08:58 ? 00:00:00 rsync --daemon root 7863 7841 0 08:58 pts/19 00:00:00 grep --color=auto rsync [root@vicodona ~]# netstat -lntup |grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 7860/rsync tcp6 0 0 :::873 :::* LISTEN 7860/rsync
第一步:查看软件是否存在
[root@mico ~]# rpm -qa|grep rsync rsync-3.1.2-4.el7.x86_64
第二步:建立认证文件
客户端认证文件只须要有密码便可
[root@mico ~]# echo "vicodona123">>/etc/rsync.passwd [root@mico ~]# chmod 600 /etc/rsync.passwd
第三步:实现数据传输
交互式
[root@mico practices]# rsync -azvP /root/practices/tmp/1.txt rsync_backup@47.107.108.121::backup Password: sending incremental file list 1.txt 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/1) sent 88 bytes received 43 bytes 29.11 bytes/sec total size is 0 speedup is 0.00
非交互式
[root@mico practices]# rsync -azvP /root/practices/tmp/1.txt rsync_backup@47.107.108.121::backup --password-file=/etc/rsync.passwd sending incremental file list 1.txt 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/1) sent 88 bytes received 43 bytes 262.00 bytes/sec total size is 0 speedup is 0.00
目录参数 | 参数说明 |
---|---|
-v, --verbose | 详细模式输出 |
-q, --quiet | 精简输出模式 |
-c, --checksum | 打开校验开关,强制对文件传输进行校验 |
-a, --archive | 归档模式,表示以递归方式传输文件,并保持全部文件属性,等于-rlptgoD |
-r, --recursive | 对子目录以递归模式处理 |
-R, --relative | 使用相对路径信息 |
-b, --backup | 建立备份,也就是对于目的已经存在有一样的文件名时,将老的文件从新命名为~filename。可使用--suffix选项来指定不一样的备份件前缀。 |
--backup-dir | 将备份文件(如~filename)存放在在目录下。 |
-suffix=SUFFIX | 定义备份文件前缀 |
-u, --update | 仅仅进行更新,也就是跳过全部已经存在于DST,而且文件时间晚于要备份的文件。(不覆盖更新的文件) |
-l, --links | 保留软链结 |
-L, --copy-links | 想对待常规文件同样处理软链结 |
--copy-unsafe-links | 仅仅拷贝指向SRC路径目录树之外的链结 |
--safe-links | 忽略指向SRC路径目录树之外的链结 |
-H, --hard-links | 保留硬链结 |
-p, --perms | 保持文件权限 |
-o, --owner | 保持文件属主信息 |
-g, --group | 保持文件属组信息 |
-D, --devices | 保持设备文件信息 |
-t, --times | 保持文件时间信息 |
-S, --sparse | 对稀疏文件进行特殊处理以节省DST的空间 |
-n, --dry-run | 现实哪些文件将被传输 |
-W, --whole-file | 拷贝文件,不进行增量检测 |
-x, --one-file-system | 不要跨越文件系统边界 |
-B, --block-size=SIZE | 检验算法使用的块尺寸,默认是700字节 |
-e, --rsh=COMMAND | 指定使用rsh、ssh方式进行数据同步 |
--rsync-path=PATH | 指定远程服务器上的rsync命令所在路径信息 |
-C, --cvs-exclude | 使用和CVS同样的方法自动忽略文件,用来排除那些不但愿传输的文件 |
--existing | 仅仅更新那些已经存在于DST的文件,而不备份那些新建立的文件 |
--delete | 删除那些DST中SRC没有的文件 |
--delete-excluded | 一样删除接收端那些被该选项指定排除的文件 |
--delete-after | 传输结束之后再删除 |
--ignore-errors | 及时出现IO错误也进行删除 |
--max-delete=NUM | 最多删除NUM个文件 |
--partial | 保留那些因故没有彻底传输的文件,以是加快随后的再次传输 |
--force | 强制删除目录,即便不为空 |
--numeric-ids | 不将数字的用户和组ID匹配为用户名和组名 |
--timeout=TIME | IP超时时间,单位为秒 |
-I, --ignore-times | 不跳过那些有一样的时间和长度的文件 |
--size-only | 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间 |
--modify-window=NUM | 决定文件是否时间相同时使用的时间戳窗口,默认为0 |
-T --temp-dir=DIR | 在DIR中建立临时文件 |
--compare-dest=DIR | 一样比较DIR中的文件来决定是否须要备份 |
-P | 等同于 --partial |
--progress | 显示备份过程 |
-z, --compress | 对备份的文件在传输时进行压缩处理 |
--exclude=PATTERN | 指定排除不须要传输的文件模式 |
--include=PATTERN | 指定不排除而须要传输的文件模式 |
--exclude-from=FILE | 排除FILE中指定模式的文件 |
--include-from=FILE | 不排除FILE指定模式匹配的文件 |
--version | 打印版本信息 |
--address | 绑定到特定的地址 |
--config=FILE | 指定其余的配置文件,不使用默认的rsyncd.conf文件 |
--port=PORT | 指定其余的rsync服务端口 |
--blocking-io | 对远程shell使用阻塞IO |
-stats | 给出某些文件的传输状态 |
--progress | 在传输时现实传输过程 |
--log-format=formAT | 指定日志文件格式 |
--password-file=FILE | 从FILE中获得密码 |
--bwlimit=KBPS | 限制I/O带宽,KBytes per second |
-h, --help | 显示帮助信息 |
保持同步目录及文件属性:
这里的-avzP至关于 -vzetopdDlP,生产环境经常使用的参数为 -avzP
在脚本中能够报-vP去掉
--progress能够用-P代替
daemon启动扩展参数
参数 | 说明 |
---|---|
--daemon | daemon表示以守护进程的方式启动rsync服务。 |
--address | 绑定指定IP地址提供服务。 |
--config=FILE | 更改配置文件路径,而不是默认的/etc/rsyncd.conf |
--port=PORT | 更改其它端口提供服务,而不是缺省的873端口 |
指定ip
[root@vicodona backup]# rsync --daemon --address=47.107.108.121 [root@vicodona backup]# netstat -lntup|grep 873 tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 7860/rsync tcp6 0 0 :::873 :::* LISTEN 7860/rsync -------------------- [root@mico practices]# rsync -azvP /root/practices/tmp/1.txt rsync_backup@47.107.108.121::backup --password-file=/etc/rsync.passwd sending incremental file list 1.txt 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/1) sent 88 bytes received 43 bytes 262.00 bytes/sec total size is 0 speedup is 0.00
服务端指定服务端口
[root@vicodona ~]# rsync --daemon --port=5222 [root@vicodona ~]# netstat -lntup|grep rsync tcp 0 0 0.0.0.0:5222 0.0.0.0:* LISTEN 2598/rsync tcp 0 0 :::5222 :::* LISTEN 2598/rsync