1、rsync与scp比较
- SCP:无法备份大量数据,类似于windows的复制
- rcyns=变量复制,边统计,边比较。(差异备份)
2、rsync特性和优点
- 可以镜像保存整个目录树和文件系统。。
- 可以很容易做到保持原来文件的权限、时间、软硬链接等等。
- 无须特殊权限即可安装。
- 快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。
- 压编传输: rsync在传输数据的过程中可以实行压编及解压编操作,因此可以使用更少的带宽。
- 安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。
- 支持匿名传输,以方便进行网站镜象。
- 选择性保持:符号连接,硬链接,文件属性。权限,时间等。
3、技术路线
3.1 运行模式和端口
- 采用C/S模式(客户端与服务端模式)[点到点的传输,直接使用rsync命令]
- 端口 873
3.2 发起端和备份源
- 发起端:负责发起rsync同步操作的客户机叫做发起端,通知服务端我要备份你的数据
- 备份端:负责相应来自客户机rsync同步操作的服务器所在的备份源,需要备份的服务器。
- 服务端:运行rsyncd服务,需要备份的服务器。
- 客户端:存放备份数据
3.3 数据同步方式
- 推push : -台主机负责吧数据传送给其他主机,服务器开销很大,比较适合后端服务器少的情况
- 拉pull:所有主机定时去找一主机拉数据,可能就会导致数据缓慢。
- 推:目的主机配百为rsyng服务器,源主机周期性的使用rsync命令把要同步的目录推过去
- 拉:源主机配置为rsync服务器,目的E机周期性的使用rsync命令把要同步的目录拉过来
两种方案,rsync都有对应的命令来实现
后台启动
rsync --daemon
1. 本地备份
[root@lidong rsync_test]# rsync -avz /var/www/mysql /var/www/rsync_test
[root@lidong rsync_test]# pwd
/var/www/rsync_test
[root@lidong rsync_test]# ls
mysql
2、rsync远程同步方法
方法一(ssh链接同步)
ssh连接远程服务器,对对方电脑进行操控监听服务器仅需要安装ssh(scp)服务,并非必须安装rsync
方法二(模组同步)
模组同步需要监听服务器安装Rsync并运行rsync进程,默认监听端口为873,数据不经过加密传输。
1. 通过ssh同步到监听服务器的指定目录下
客户端发起同步操作 rsync -avzP --stats --progress root@10.9.25.23:/data/jenkins/ /home/test/
[root@localhost .ssh]# rsync -avzP --stats --progress root@10.9.25.23:/data/jenkins/ /home/test/
receiving incremental file list
Number of files: 3,022 (reg: 2,253, dir: 769)
Number of created files: 0
Number of deleted files: 0
Number of regular files transferred: 0
Total file size: 374,097,265 bytes
Total transferred file size: 0 bytes
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 29,386
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 796
Total bytes received: 88,070
sent 796 bytes received 88,070 bytes 59,244.00 bytes/sec
total size is 374,097,265 speedup is 4,209.68
客户端检查是否存在
[root@localhost .ssh]# ls /home/test/
%C hudson.plugins.git.GitTool.xml jenkins.telemetry.Correlator.xml plugins updates
config.xml identity.key.enc jobs queue.xml.bak userContent
credentials.xml jenkins.install.InstallUtil.lastExecVersion logs secret.key users
fingerprints jenkins.install.UpgradeWizard.state nodeMonitors.xml secret.key.not-so-secret
hudson.model.UpdateCenter.xml jenkins.model.JenkinsLocationConfiguration.xml nodes
2. 通过模组进行传输
在监听服务器(239)配置rsyncd.conf文件,rsync.conf一般默认在/etc/文件夹下。如果没有,官网下载安装即可。
在rsync.conf文件中配置了两个模组,一个名称为rsync_file的模组,另一个为rsyn_uwsgi的模组。其中rsync_file模组指定了可访问拿下目录,哪些用户可以访问和密码,rsyn_uwsgi模组仅仅指定了可以访问哪些目录,无需密码和用户,具体如下:
# /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 = 4
lock file=/var/run/rsyncd.lock
log file = /var/log/rsyncd.log
transfer logging = yes
#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
[rsync_file] # 模组名称随意
path = /var/www/
list=yes
#认证用户 和系统用户无关 作为rsync工具认证使用
auth users = test
read only = no
###密码文件
secrets file = /etc/rsync/screts.pas
[rsyn_uwsgi]
path = /var/www/house_statistic_query_dev/dev_config/uwsgi
list=yes
read only = no
ignore errors
4、创建rsync_file模组的密码文件,并授予权限
注意:权限一定要用600,不能打也不能小,一定要等于600
[root@lidong etc]# cat /etc/rsync/screts.pas
test:123456
[root@lidong etc]# chmod 600 /etc/rsync/screts.pas
[root@lidong etc]# cd /etc/rsync
[root@lidong rsync]# ll
total 4
-rwxrwxrwx. 1 root root 18 Nov 26 01:59 screts.pas
1、查看服务端拥有模组
[root@lidong ~]# rsync 172.16.10.239::
rsync_file
rsyn_uwsgi
2、客户端查看服务端rsyn_uwsgi模组指定的文件夹
[root@lidong ~]# rsync 172.16.10.239::rsyn_uwsgi
drwxr-xr-x 70 2021/11/23 10:04:30 .
-rw-r----- 2,505 2021/11/23 10:04:36 --ini
-rw-r--r-- 1,634 2021/11/23 11:10:27 uwsgi.ini
-rw-r----- 36,409 2021/11/23 10:03:55 uwsgi.log
-rw-rw-rw- 6 2021/11/23 10:04:30 uwsgi.pid
2、查看服务端模组指定的文件夹
[root@lidong ~]# rsync test@172.16.10.239::rsync_file
Password:
drwxr-xr-x 162 2021/11/25 16:43:56 .
-rw------- 6 2021/11/23 17:34:58 passwd
drwxr-xr-x 6 2021/11/23 15:14:15 bak-config
drwxr-xr-x 48 2021/11/17 17:09:00 crawlab
drwxr-xr-x 96 2021/11/16 17:27:51 house_statistic_query_dev
drwxr-xr-x 70 2021/11/25 16:52:48 leanote
drwxr-xr-x 81 2021/11/16 17:18:47 mysql
drwxr-xr-x 19 2021/11/25 09:46:10 mysql_bak
drwxr-xr-x 53 2021/11/22 10:59:07 nginx
drwxr-xr-x 23 2021/11/24 10:18:43 rsync_test
3、服务端文件同步至客户端
[root@lidong www]# rsync -av 172.16.10.239::rsyn_uwsgi /var/www/
receiving incremental file list
./
--ini
uwsgi.ini
uwsgi.log
uwsgi.pid
sent 107 bytes received 40,888 bytes 81,990.00 bytes/sec
total size is 40,554 speedup is 0.99
[root@lidong www]# ls
--ini uwsgi.ini uwsgi.log uwsgi.pid
4、客户端免密同步
[root@lidong www]# rsync -av --password-file=/etc/rsyncd.password test@172.16.10.239::rsync_file/passwd /var/www
receiving incremental file list
passwd
sent 43 bytes received 106 bytes 298.00 bytes/sec
total size is 6 speedup is 0.04
[root@lidong www]# ls
passwd
[root@lidong www]# cat passwd
admin
5、windows rsync客户端同步
rsync.exe -avz --progress /cygdrive/d/test.txt rsynctest@192.168.11.1::test
6、定时执行
1.1. 编写shell文件
[root@lidong www]# cat rcync_cop.sh
#!/bin/bash
rsync -av --password-file=/etc/rsyncd.password test@172.16.10.239::rsync_file/passwd /var/www
hello.py passwd rcync_cop.sh
[root@lidong www]# chmod 777 rcync_cop.sh
[root@lidong www]# ls
hello.py passwd rcync_cop.sh
[root@lidong www]# ./rcync_cop.sh #执行测试
receiving incremental file list
sent 24 bytes received 53 bytes 154.00 bytes/sec
total size is 6 speedup is 0.08
1.2. 添加定时任务
查看服务器是否拥有crond,并启动(没有可以使用yum安装)
[root@lidong www]# service crond status
Redirecting to /bin/systemctl status crond.service
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-08-16 15:44:35 CST; 3 months 10 days ago
Main PID: 1031 (crond)
Tasks: 1
Memory: 76.0K
CGroup: /system.slice/crond.service
└─1031 /usr/sbin/crond -n
使用 crontab命令编辑定时任务
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
1 0 9 * * * /var/wwwrcync_cop.sh
[root@lidong www]# crontab -l
30 0 * * * * /var/wwwrcync_cop.sh
7、限速
对于小带宽 VPS,rsync 可能会占满带宽,导致服务器/网站连不上,我们可以使用 bwlimit 参数限制最大传输速度,单位为 KB/s,如下命令,限制最大速度为 300 KB/s:
P 支持断点续传和进度显示
rsync -avP --bwlimit=300 -e 'ssh -p 27384' /etc/data.zip root@144.44.44.44:/root/backup