rsync定时任务同步(服务端到客户端和客户端到服务端)

rsync与scp比较
  • SCP:无法备份大量数据,类似于windows的复制
  • rcyns=变量复制,边统计,边比较。(差异备份)
rsync特性和优点
  • 可以镜像保存整个目录树和文件系统。。
  • 可以很容易做到保持原来文件的权限、时间、软硬链接等等。
  • 无须特殊权限即可安装。
  • 快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。
  • 压编传输: rsync在传输数据的过程中可以实行压编及解压编操作,因此可以使用更少的带宽。
  • 安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。
  • 支持匿名传输,以方便进行网站镜象。
  • 选择性保持:符号连接,硬链接,文件属性。权限,时间等。
技术路线
运行模式和端口
  • 采用C/S模式(客户端与服务端模式)[点到点的传输,直接使用rsync命令]
  • 端口 873
发起端和备份源
  • 发起端:负责发起rsync同步操作的客户机叫做发起端,通知服务端我要备份你的数据
  • 备份端:负责相应来自客户机rsync同步操作的服务器所在的备份源,需要备份的服务器。
  • 服务端:运行rsyncd服务,需要备份的服务器。
  • 客户端:存放备份数据
数据同步方式
  • 推push : -台主机负责吧数据传送给其他主机,服务器开销很大,比较适合后端服务器少的情况
  • 拉pull:所有主机定时去找一主机拉数据,可能就会导致数据缓慢。
  • 推:目的主机配百为rsyng服务器,源主机周期性的使用rsync命令把要同步的目录推过去
  • 拉:源主机配置为rsync服务器,目的E机周期性的使用rsync命令把要同步的目录拉过来
    两种方案,rsync都有对应的命令来实现
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jAcEuF1C-1637907045405)
准备两台服务器,分别作为客户端和服务端

由于centos 7.X 自带rsync,我们使用默认的即可,如果没有,可以使用yum安装。

服务器

IP

客户端

172.16.10.228

服务端

172.16.10.239

案例
  1. 服务端到服务端备份(mysql目录备份到rsync_test目录下)
[root@lidong rsync_test]# rsync -a /var/www/mysql /var/www/rsync_test
[root@lidong rsync_test]# pwd
/var/www/rsync_test
[root@lidong rsync_test]# ls
mysql
  1. 服务端到服务端备份(dev_config目录下的文件备份到rsync_test目录下)
[root@lidong rsync_test]# rsync -a /var/www/house_statistic_query_dev/dev_config/ /var/www/rsync_test
[root@lidong rsync_test]# pwd
/var/www/rsync_test
[root@lidong rsync_test]# ls
docker-compose.yml  nginx.conf  ocker-compoose.yml.bac  uwsgi  venv
  1. 使用-v参数可以看到同步详细信息
[root@lidong rsync_test]# rsync -av /var/www/house_statistic_query_dev/dev_config/uwsgi/uwsgi.ini /var/www/rsync_test
sending incremental file list
uwsgi.ini

sent 1,728 bytes  received 35 bytes  3,526.00 bytes/sec
total size is 1,634  speedup is 0.93
rsync远程同步方法
  1. 方法一(ssh链接同步):

ssh连接远程服务器,对对方电脑进行操控监听服务器仅需要安装ssh(scp)服务,并非必须安装rsync

  1. 方法一(模组同步):

模组同步需要监听服务器安装Rsync并运行rsync进程,默认监听端口为873,数据不经过加密传输。

操作案例
  1. 通过ssh模组将服务器A的文件同步到监听服务器的指定目录下
  • 服务端发起同步操作
  • rsync 目标服务器端口 rsync服务端和客户端_linux

  • 客户端检查是否存在
  • rsync 目标服务器端口 rsync服务端和客户端_mysql_02

  1. 通过模组进行传输
  • 在监听服务器(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
  1. 创建rsync_file模组的密码文件,并授予权限

注意:权限一定要用600,不能打也不能小,一定要等于600

[root@lidong etc]# cat  /etc/rsync/screts.pas 
rsync_file:123456
[root@lidong etc]# chmod 777 /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
  1. 客户端查看服务端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
  1. 客户端查看服务端rsync_file模组指定的文件夹(需要输入密码)
[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
  1. 客户端查看服务端rsync_file模组指定的文件夹(无需要输入密码)
[root@lidong ~]# vim /etc/rsyncd.password   #配置密码脚本,与服务端对应
[root@lidong ~]# cat  /etc/rsyncd.password
123456
[root@lidong ~]# chmod 600 /etc/rsyncd.password #修改权限
[root@lidong ~]# rsync --password-file=/etc/rsyncd.password test@172.16.10.239::rsync_file
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
  1. 服务端rsyn_uwsgi模组下的文件同步至客户端的/var/www目录下
[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
  1. 将服务端rsyn_uwsgi模组下的文件夹下的uwsgi.ini文件同步至客户端的/var/www目录下

以下命令会使用服务端指定path+path下的文件或文件夹进行拷贝

[root@lidong www]# rsync -av  172.16.10.239::rsyn_uwsgi/uwsgi.ini  /var/www/
receiving incremental file list
uwsgi.ini

sent 43 bytes  received 1,736 bytes  3,558.00 bytes/sec
total size is 1,634  speedup is 0.92
[root@lidong www]# ll
total 4
-rw-r--r-- 1 root root 1634 Nov 23 11:10 uwsgi.in
  1. 将客户端的aa.html文件同步到服务端rsyc_uwsgi指定的目录
  • 客户端推
[root@lidong www]# rsync -av /var/www/aa.html  172.16.10.239::rsyn_uwsgi  
sending incremental file list
aa.html

sent 118 bytes  received 43 bytes  322.00 bytes/sec
total size is 21  speedup is 0.13
[root@lidong www]#
  • 服务端查看,此时aa.html已经存在
[root@lidong etc]# cd /var/www/house_statistic_query_dev/dev_config/uwsgi/
[root@lidong uwsgi]# ls
aa.html  --ini  uwsgi.ini  uwsgi.log  uwsgi.pid
  1. 将服务端rsync_file模组下的文件夹下的passwd文件同步至客户端的/var/www目录下
[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
  1. 将客户端的/var/www目录下的hello.py同步至服务端rsync_file模组下指定的文件夹下
  • 客户端推
[root@lidong www]# rsync -av --password-file=/etc/rsyncd.password /var/www/hello.py test@172.16.10.239::rsync_file
sending incremental file list
hello.py

sent 119 bytes  received 43 bytes  324.00 bytes/sec
total size is 21  speedup is 0.13
  • 服务端查看,此时hello.py已经存在
[root@lidong etc]# cd /var/www/
[root@lidong www]# ls
bak-config  crawlab  hello.py  house_statistic_query_dev  leanote  mysql  mysql_bak  nginx  passwd  rsync_test
  1. 写入shell文件,定时执行

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

Nov 18 09:00:01 lidong crond[1031]: /usr/sbin/sendmail: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such f...r directory
Nov 19 09:00:01 lidong crond[1031]: /usr/sbin/sendmail: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such f...r directory
Nov 20 09:00:01 lidong crond[1031]: /usr/sbin/sendmail: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such f...r directory
Nov 21 09:00:01 lidong crond[1031]: /usr/sbin/sendmail: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such f...r directory
Nov 22 09:00:01 lidong crond[1031]: /usr/sbin/sendmail: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such f...r directory
Nov 23 09:00:01 lidong crond[1031]: /usr/sbin/sendmail: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such f...r directory
Nov 24 09:00:01 lidong crond[1031]: /usr/sbin/sendmail: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such f...r directory
Nov 25 09:00:01 lidong crond[1031]: /usr/sbin/sendmail: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such f...r directory
Nov 26 09:00:01 lidong crond[1031]: /usr/sbin/sendmail: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such f...r directory
Nov 26 11:20:01 lidong crond[1031]: (root) RELOAD (/var/spool/cron/root)
Hint: Some lines were ellipsized, use -l to show in full.
  • 使用 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
  • 查看定时任务(每天凌晨0点30同步)
[root@lidong www]# crontab -l
30 0 * * * * /var/wwwrcync_cop.sh