一、rsync特性
可以镜像保存整个目录树和文件系统。

可以很容易做到保持原来文件的权限、时间、软硬链接等等。

无须特殊权限即可安装。

快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。

安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。

支持匿名传输,以方便进行网站镜象。

rsync常用选项
-a 包含-rtplgoD (平时用-a就够啦)
-r 同步目录时要加上,类似cp时的-r选项
-v 可视化,告诉你拷贝了什么文件,统计用了多久,拷贝了多少字节,速度等,同步时显示一些信息,让我们知道同步的过程
-l 保留软连接,拷贝的语言目录有软连接文件,用到另外一个目录下,加了-l 会把软连接本身拷贝到目录里
-L 加上该选项后,同步软链接时会把源文件给同步
-p 保持文件的权限属性
-o 保持文件的属主,这边是www,拷贝另外一边也是www,如果你不没有,就显示数字
-g 保持文件的属组,root属组,到另外一个地方也是一样
-D 保持设备文件信息
-t 保持文件的时间属性
–delete 删除DEST(目标目录)中SRC(源目录)没有的文件
–exclude 过滤指定文件,如–exclude (可以排查一些目录或者文件,如日志文件,节省空间) “logs”会把文件名包含logs的文件或者目录过滤掉,不同步
-P 显示同步过程,比如速率,比-v更加详细
-u 是update的简写,加上该选项后,如果DEST(目标)中的文件比SRC(源文件)新,则不同步
-z 传输时压缩(传输前自动压缩,传输后,自动解压)

二、方式一:使用SSH通道同步
说明: 1、文件源:A (服务端) 2、目标端:B (客户端)、C、D….. (接收文件)
1.在目标端B 安装rsync
安装
yum install rsync -y

卸载
yum remove rsync -y

启动
rsync –daemon

杀死进程
pkill rsync

查看端口873
检查是否开启了对873端口的监听
lsof -i:873

2.在文件源端A 安装
安装
yum install rsync -y

卸载
yum remove rsync -y

免密钥登录配置
第一步,在文件源端 生成密钥
[root@izwz93hm3gyjyjwx5yyxuhz ~]# ssh-keygen -t rsa //指定算法为rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): //保存密钥全路径
Enter passphrase (empty for no passphrase): //密码可以为空
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa. //私钥
Your public key has been saved in /root/.ssh/id_rsa.pub. //公钥
The key fingerprint is:
SHA256:uuMJxNrH7HYKoKK4iqfX/Bxc3KgyvHx0glaKoygOdjA root@izwz93hm3gyjyjwx5yyxuhz
The key’s randomart image is:
+—[RSA 2048]—-+
| |
| |
| |
| . .. o |
| E..o+ S . |
| .=*++o+. |
|=o.*B.Oo |
|@ +.oX++. |
|@* o=O+ |
+—-[SHA256]—–+
[root@izwz93hm3gyjyjwx5yyxuhz ~]#

此时会在/root/.ssh目录下生成密钥对
[root@izwz93hm3gyjyjwx5yyxuhz .ssh]# ls -la
total 20
drwx—— 2 root root 4096 Jul 4 08:40 .
dr-xr-x—. 7 root root 4096 Jul 4 08:44 ..
-rw——- 1 root root 0 Jul 3 08:24 authorized_keys
-rw——- 1 root root 1675 Jul 4 08:40 id_rsa
-rw-r–r– 1 root root 410 Jul 4 08:40 id_rsa.pub
-rw-r–r– 1 root root 176 Jul 3 09:50 known_hosts

第二步,在目标端把密钥上传到文件源服务器
ssh-copy-id root@120.77.250.120
[root@izwz93hm3gyjyjwx5yyxuhz .ssh]# ssh-copy-id root@120.77.250.120
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/.ssh/id_rsa.pub”
The authenticity of host ‘120.77.250.120 (120.77.250.120)’ can’t be established.
ECDSA key fingerprint is SHA256:okdQRMFZve4+bVy0aCrDiKpNXe3E20bac2G7gG0VkdQ.
ECDSA key fingerprint is MD5:88:a4:7a:72:db:10:a5:db:8d:47:bc:23:cb:4f:a9:eb.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed — if you are prompted now it is to install the new keys
root@120.77.250.120’s password:

Number of key(s) added: 1

Now try logging into the machine, with: “ssh ‘root@120.77.250.120′”
and check to make sure that only the key(s) you wanted were added.

第三步,测试同步文件
在/www/wwwroot/file 里面新建文件
rsync -avzP /www/wwwroot/file/ root@120.77.250.120:/www/wwwroot/file/

//使用了免密方式,就不会提示输入密码

同步结果:( success)
sending incremental file list
plus-yixueqing/
plus-yixueqing/222.php

sent 90,389 bytes received 756 bytes 60,763.33 bytes/sec
total size is 132,583,590 speedup is 1,454.64
[root@izwz93hm3gyjyjwx5yyxuhz file]# rsync -avzP /www/wwwroot/file/ root@120.77.250.120:/www/wwwroot/file/
sending incremental file list

sent 90,342 bytes received 726 bytes 60,712.00 bytes/sec
total size is 132,583,590 speedup is 1,455.87

三、rsync 守护进程 (推荐)
1.在目标端
是要接收文件的,需要配置两个文件
安装
yum install rsync -y

卸载
yum remove rsync -y

第一、配置文件/etc/rsyncd.conf
写模块,分配虚拟用户名和密码等信息
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
[faceke]
path = /www/wwwroot/file/
ignore errors
read only = no
list = false
auth users = anson
secrets file = /etc/rsync.password

配置说明:
vim /etc/rsyncd.conf
uid = rsync #用户id
gid = rsync
use chroot = no #安全性,内网一般不考虑,设为no
max connections = 200 #最多有多少个客户端连接我
timeout = 300 #超时时间,秒
pid file = /var/run/rsyncd.pid #pid文件
lock file = /var/run/rsync.lock #传输时会给文件加锁
log file = /var/log/rsyncd.log #日志文件
[test] #模块
path = /test/ #客户端来同步,就是同步该目录
ignore errors #传输过程中遇到错误,自动忽略
read only = false #可读可写
list = false #不允许列表
hosts allow = 10.0.0.0/24 #允许的IP段
hosts deny = 0.0.0.0/32 #拒绝
auth users = rsync_backup #这是个虚拟用户(自定义)
secrets file = /etc/rsync.password #虚拟用户对应的密码文件

第二、配置密码文件 /etc/rsync.password
(写入虚拟用户名和密码)
#仅写入和服务端虚拟用户对应的密码
echo “anson:hao123456” > /etc/rsync.password

#修改密码文件的权限(必须)
chmod 600 /etc/rsync.password

查看端扣873
检查是否开启了对873端口的监听
lsof -i:873

杀死进程
pkill rsync

启动服务
rsync –daemon

2.安装与配置客户端 (文件源端)
客户端只需要密码文件,文件里只存放密码:
安装
yum install rsync -y

仅写入和服务端虚拟用户对应的密码
修改/etc/rsync.password
echo “hao123456” > /etc/rsync.password
chmod 600 /etc/rsync.password

写给目录,开始同步操作
创建目录,创建测试文件
cd /www/wwwroot/

mkdir file

vim aaa.php
vim bbb.php …

开始同步
rsync -arvzp –delete /www/wwwroot/file/ anson@120.77.250.120::faceke –password-file=/etc/rsync.password

或者使用rsync协议
rsync -avz rsync://anson@120.77.250.120/test /www/wwwroot/file/ –password-file=/etc/rsync.password

也可以将要排除的文件和目录写入一个文件,一行一个: vim excefile.conf
test.py
*.log
dir1

使用—exclude-from
rsync -avz –exclude-from=excefile.conf /data/ rsync_backup@10.0.07::/test –password-file=/etc/rsync.password

一些问题排查
https://yq.aliyun.com/articles/111512?t=t1

//软连接/rsyncd-munged/ 报错解决方案
http://blog.sina.com.cn/s/blog_4da051a60102wo5v.html

写定时任务
crontab -e
#此时会进入vi的编辑界面让你编辑工作。注意到,每项工作都是一行。
12 * * * mail dmtsai -s “at 12:00” < /home/dmtsai/.bashrc
#分 时 日 月 周 |《==============命令行=======================》|

代表意义 分钟 小时 日期 月份 周 命令
数字范围 0~59 0~23 1~31 1~12 0~7 就命令啊
周的数字为0或7时,都代表“星期天”的意思。另外,还有一些辅助的字符,大概有下面这些:
*(星号)
代表任何时刻都接受的意思。举例来说,范例一内那个日、月、周都是*,就代表着不论何月、何日的礼拜几的12:00都执行后续命令的意思。

,(逗号)
代表分隔时段的意思。举例来说,如果要执行的工作是3:00与6:00时,就会是:
3,6 * * * command

时间还是有五列,不过第二列是 3,6 ,代表3与6都适用

-(减号)
代表一段时间范围内,举例来说,8点到12点之间的每小时的20分都进行一项工作:
8-12 * * * command

仔细看到第二列变成8-12.代表 8,9,10,11,12 都适用的意思

/n(斜线)
那个n代表数字,即是每隔n单位间隔的意思,例如每五分钟进行一次,则:

*/5 * * * * command

用*与/5来搭配,也可以写成0-59/5,意思相同

写同步脚本
可手动执行,也可以定时执行
在目录www/wwwroot/file/创建脚本rsync.sh
vim rsync.sh

#/bin/sh
rsync -arvzp –delete /www/wwwroot/file/ anson@120.77.250.120::faceke –password-file=/etc/rsync.password
//如果有多台服务器,就在这里设置多台

done

配置定时任务
在这里是定时1分钟执行一次脚本
crontab -e

*/1 * * * * sh /www/wwwroot/file/rsync.sh

同步文件大小
du -sh ./*

ls -alh

压缩命令
tar -zcvf plus-yixueqing-h5.zip plus-yixueqing-h5/