使用Rsync推与拉方式:同步Web网页数据

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

Ø  思路

更新:管理服务器(主机shenxiang62) 使用rsync推送数据到(主机shenxiang61)即可
备份:管理服务器(主机shenxiang62) 使用rsync从主机(shenxiang61)拉入数据到本地
对此,shenxiang61 为rsync服务端 shenxiang62为客户端

使用Rsync推与拉方式:同步Web网页数据_第1张图片
1、主机shenxiang61配置

  • rsync服务安装,并使用xinetd守护进程启动
[root@shenxiang61 ~]# yum install -y xinetd
[root@shenxiang61 ~]# vim /etc/xinetd.d/rsync
service rsync
{
disable = no             #把yes改为no
flags           = IPv6
socket_type     = stream
wait            = no
user            = root
server          = /usr/bin/rsync
server_args     = –daemon
log_on_failure  += USERID
}
  • 添加配置文件/etc/rsyncd.conf
[root@shenxiang61 ~]# vim /etc/rsyncd.conf
[root@shenxiang61 ~]# cat !$
Ø  因为这里文件都是apache服务来使用;所以读写用户设为apache
cat /etc/rsyncd.conf
uid = apache 
gid = apache 
use chroot = no 
max conections = 200 
timeout = 300 
pid file = /var/run/rsyncd.pid  
lock file = /var/run/rsync.lock 
log file = /var/log/rsync.log 
[webdata] 
path = /www/web/ 
ignore errors 
read only = no 
list = yes 
hosts allow = 192.168.1.62 
auth users = xuegod 
secrets file = /etc/rsync.password
  •  虚拟用户认正文件

注意:
#文件格式:[用户:密码]
#认证文件权限要为600

[root@shenxiang61 ~]# vim /etc/rsync.password
[root@shenxiang61 ~]# cat !$
cat /etc/rsync.password
xuegod:xg123456
[root@shenxiang61 ~]# chmod 600 !$
chmod 600 /etc/rsync.password
[root@shenxiang61 ~]# ll !$
ll /etc/rsync.password
–rw——- 1 root root 16 Feb 18 00:12 /etc/rsync.password
  • 重启并检查监听端口
[root@shenxiang61 ~]# /etc/init.d/xinetd restart
[root@shenxiang61 ~]# lsof -i :873
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
xinetd  26469 root    6u  IPv6  54207      0t0  TCP *:rsync (LISTEN)

2、主机shenxiang62配置

  • 更新rsync
[root@shenxiang62 ~]# yum install -y rsync
  •  创建免交互密码文件
[root@shenxiang62 ~]# vim /etc/rsync.webpwd
[root@shenxiang62 ~]# cat !$
cat /etc/rsync.webpwd
xg123456
[root@shenxiang62 ~]# chmod 600 !$
chmod 600 /etc/rsync.webpwd
[root@shenxiang62 ~]# ll !$
ll /etc/rsync.webpwd
-rw——- 1 root root 9 Feb 18 11:46 /etc/rsync.webpwd

3、备份网页数据到主机shenxiang62

Ø 原理:使用rsync拉pull数据同步方式
Ø 执行动作:主机shenxiang62

  • 创建备份存放目录/data/webdata,并备份到本地
[root@shenxiang62 ~]# mkdir /data/webdata
  • 备份测试(Rsync拉Pull)
[root@shenxiang62 ~]# rsync -avz [email protected]::webdata –password-file=/etc/rsync.webpwd /data/webdata/

......省略
uc_server/view/default/pm_send.htm
uc_server/view/default/pm_view.htm
uc_server/view/default/templates.lang.php
uc_server/view/default/user_avatar.htm 

sent 80375 bytes  received 12036146 bytes  4846608.40 bytes/sec
total size is 28106599  speedup is 2.32
  • 查看备份数据
[root@shenxiang62 ~]# ll /data/webdata/
total 116
-rw-r–r–  1 apache apache 2739 Feb 17 22:08 admin.php
drwxr-xr-x 11 apache apache 4096 Feb 17 22:08 api
-rw-r–r–  1 apache apache  727 Feb 17 22:08 api.php
drwxr-xr-x  2 apache apache 4096 Feb 17 22:08 archiver
drwxr-xr-x  2 apache apache 4096 Feb 17 22:45 config

......省略
  • 编写备份脚本Web网页脚本
[root@shenxiang62 ~]# cd /data_bak/
[root@shenxiang62 data_bak]# vim webbak.sh
[root@shenxiang62 data_bak]# cat !$
cat webbak.sh
#!/bin/bash
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
webdata='/data_bak/webdata/'
webdaemon=webdata
webuser=xuegod
webhost='192.168.1.61'
mysqlhost='192.168.1.222'
webpwd='/etc/rsync.webpwd'
time=$(date +"%Y%m%d")
rsync -avz $webuser@$webhost::$webdaemon --password-file=$webpwd /data/webdata/$time_bak \
&& echo "NFS server webdata ${time} backup success!!" >>/data_bak/bak.log && find /data/webdata -mtime +8 |xargs rm -rf
  • 添加计划任务
[root@shenxiang62 data_bak]# echo “30 3 * * * root /bin/bash /data_bak/webbak.sh” >> /etc/crontab
[root@shenxiang62 data_bak]# tail /etc/crontab
# .—————- 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
30 4 * * * root /bin/bash /data_bak/webbak.sh

4、更新(恢复)主机shenxiang61上的网页数据

Ø 原理:rsync使用推push数据同步方式
Ø 执行动作:主机shenxiang62

  • 清空主机shenxiang61上的网页数据
[root@shenxiang61 ~]# cp -r /www/web/ /www/web.bak
[root@shenxiang61 ~]# rm -rf /www/web/*
[root@shenxiang61 ~]# ll /www/web
total 0
  • 更新测试(Rsync推Push)
[root@shenxiang62 ~]# rsync -avz –delete /data/webdata/ [email protected]::webdata –password-file=/etc/rsync.webpwd

......省略
uc_server/view/default/pm_nav.htm
uc_server/view/default/pm_send.htm
uc_server/view/default/pm_view.htm
uc_server/view/default/templates.lang.php
uc_server/view/default/user_avatar.htm

sent 12036078 bytes  received 80324 bytes  1615520.27 bytes/sec
total size is 28106599  speedup is 2.32
  • 查看更新数据
[root@shenxiang61 ~]# ll /www/web
total 116
-rw-r–r–  1 apache apache 2739 Feb 17 22:08 admin.php
drwxr-xr-x 11 apache apache 4096 Feb 17 22:08 api
-rw-r–r–  1 apache apache  727 Feb 17 22:08 api.php
drwxr-xr-x  2 apache apache 4096 Feb 17 22:08 archiver
drwxr-xr-x  2 apache apache 4096 Feb 17 22:45 config
-rw-r–r–  1 apache apache  922 Feb 17 22:08 connect.php
-rw-r–r–  1 apache apache  253 Feb 17 22:08 cp.php

......省略

 

转载于:https://my.oschina.net/u/3409834/blog/1548938

你可能感兴趣的:(使用Rsync推与拉方式:同步Web网页数据)