目录
一 rsync同步简介
1.1 关于rsync
● rsync,全称为:Remote Sync(远程同步),是一款开源的快速增量备份工具,可以在不同主机之间镜像同步整个目录树
● 还支持本地复制,增量备份、保持连接和权限,或者与其他SSH,rsync主机同步
● 采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用
●官方网站:http://rsync.samba.org
二 配置rsync备份原
2.1 rsync同步源
● 指备份操作的远程服务器,也称为备份原
2.2 配置rsync源的基本思路
●建立rsyncd.conf配置文件,独立的账号文件
●启用rsync的–daemon模式
三 rsync命令基本用法
3.1 rsync命令的用法
rsync 【选项】原始位置 目标位置
3.2 常用选项:
-a:归档模式,递归并保留对象属性,等同于 -rlptgoD
-r 对子目录以递归模式处理,主要是针对目录来说的,如果单独传一个文件不需要加-r,但是传输的是目录必须加-r选项
-l 保留软链接
-p 保持文件权限
-v:显示同步过程的详细(verbose)信息
-z:在传输文件时进行压缩(compress)
-H:保留硬连接文件
-A:保留ACL属性信息
--delete:删除目标位置有而原始位置没有的文件
--checksum:根据对象的校验和来决定是否跳过文件
--progress 在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、同步的文件传输速度等等
3.3配置源的两种表示方法
(1):命令格式用户名@主机地址::共享模块名
例如:[root@rsyncClient ~]# rsync -avz backuper@20.0.0.11::wwwroot /opt
(2):命令格式:rsync://用户名@主机地址/共享模块名
例如:[root@slave opt]# rsync -avz rsync://backuper@20.0.0.11/wwwroot /root
四 rsync备份操作实例
##配置rsync源服务器##
1. 关闭防火墙,核心防护
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
setenforce: SELinux is disabled
2. 安装rsync
[root@localhost ~]# rpm -q rsync
package rsync is not installed
[root@localhost ~]# yum -y install rsync
3.修改rsync配置文件
[root@localhost ~]# vi /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 ##禁锢家目录
pid file = /var/run/rsyncd.pid ##pid文件位置
log file = /var/log/rsyncd.log ##日志文件位置
address = 20.0.0.11 ##本地IP地址
port 873 ##端口号
hosts allow = 20.0.0.0/24 ##设置白名单
# max connections = 4
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 ##设置哪些类型文件不会被压缩
[wwwroot] ##共享模块名
path = /var/www/html ##共享的站点
comment = www.abc.cn ##描述信息
read only = yes ##设置只读
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
auth users = backuper ##创建的一个独立账号
secrets file = /etc/rsyncd_users.db ##创建的用户名密码文件
# [ftp]
# path = /home/ftp
4. 安装apache服务
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "hello" > abc.txt //创建测试文件
[root@localhost html]# ls
abc.txt
[root@localhost html]# cat abc.txt
hello
5. 配置rsync账号文件
[root@localhost etc]# vi /etc/rsyncd_users.db
backuper:abc123
[root@localhost etc]# chmod 600 /etc/rsyncd_users.db
6. 启用rsync服务
[root@localhost etc]# rsync --daemon
[root@localhost etc]# netstat -ntap | grep rsync
tcp 0 0 20.0.0.11:873 0.0.0.0:* LISTEN 2332/rsync
##客户端##
1. 关闭防火墙,核心防护
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
setenforce: SELinux is disabled
2. 安装rsync
[root@localhost ~]# yum -y install rsync
[root@localhost myrsync]# cd /opt/
[root@localhost opt]# mkdir myrsync
3. rsync同步操作
第一种格式
[root@localhost ~]# rsync -avz rsync://backuper@20.0.0.11/wwwroot /opt/myrsync
Password:
receiving incremental file list
./
abc.txt
sent 83 bytes received 159 bytes 69.14 bytes/sec
total size is 6 speedup is 0.02
第二种格式
[root@localhost ~]# rsync -avz backuper@20.0.0.11::wwwroot /opt/myrsync
4. 查看同步文件
[root@localhost opt]# cd myrsync/
[root@localhost myrsync]# ll
total 0
-rw-r--r-- 1 root root 0 Oct 23 23:13 abc.txt
5. 配置rsync源的免交互处理文件
[root@localhost opt]# vi /etc/server.pass
abc123
[root@localhost opt]# chmod 600 /etc/server.pass
6. 测试免交互
[root@localhost opt]# rm -rf abc.txt
[root@localhost opt]# rsync -avz --delete --password-file=/etc/server.pass backuper@20.0.0.11::wwwroot /opt/myrsync
receiving incremental file list
./
abc.txt
sent 83 bytes received 159 bytes 161.33 bytes/sec
total size is 6 speedup is 0.02
7.可以创建周期性任务进行同步源服务器
[root@slave opt]# crontab -e
30 22 * * * /usr/bin/rsync -avz --delete --password-file=/etc/server.pass backuper@20.0.0.11::wwwroot /opt/ '//每天晚上10点半对服务器网站目录更新一次'
[root@slave opt]# systemctl restart crond
[root@slave opt]# systemctl enable crond
五 rsync+inotify结合使用
##客户端配置##
1. 修改内核信息
[root@localhost opt]# vi /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@localhost opt]# sysctl -p
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
2. 安装inotify-tools辅助工具
##提前上传好包源
[root@localhost opt]# tar zxvf inotify-tools-3.14.tar.gz
[root@localhost opt]# cd inotify-tools-3.14
[root@localhost inotify-tools-3.14]# yum -y install gcc gcc-c++ make
[root@localhost inotify-tools-3.14]# ./configure
[root@localhost inotify-tools-3.14]# make && make install
3.重新打开一个终端,监控
[root@localhost ~]# inotifywait -mrq -e modify,create,move,delete /opt/myrsync/
[root@localhost myrsync]# touch aaa.txt
[root@localhost myrsync]# rm -rf aaa.txt
[root@localhost inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /opt/myrsync/
/opt/myrsync/ CREATE aaa.txt
/opt/myrsync/ DELETE aaa.txt
4.服务器编辑脚本并启动
[root@localhost inotify-tools-3.14]# vi /opt/inotify_rsync.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /opt/myrsync/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /opt/myrsync/ backuper@20.0.0.11::wwwroot"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ]; then
$RSYNC_CMD
fi
done
[root@localhost inotify-tools-3.14]# chmod +x /opt/inotify_rsync.sh
[root@localhost inotify-tools-3.14]# cd /opt/
[root@localhost opt]# ll
total 360
-rwxr-xr-x 1 root root 338 Oct 23 16:47 inotify_rsync.sh
drwxrwxrwx 5 cwj cwj 4096 Oct 23 16:38 inotify-tools-3.14
-rw-r--r-- 1 root root 358772 Oct 23 16:36 inotify-tools-3.14.tar.gz
drwxr-xr-x 2 root root 21 Oct 23 16:43 myrsync
[root@localhost opt]# chmod 777 myrsync/
##源端##
[root@localhost www]# chmod 777 /var/www/html/
[root@localhost www]# vi /etc/rsyncd.conf
read only = no
[root@localhost www]# netstat -anpt | grep rsync
tcp 0 0 20.0.0.11:873 0.0.0.0:* LISTEN 1550/rsync
[root@localhost www]# pkill -9 rsync
[root@localhost www]# netstat -anpt | grep rsync
[root@localhost www]# rsync --daemon
failed to create pid file /var/run/rsyncd.pid: File exists
[root@localhost www]# cd /var/run/
[root@localhost run]# rm -rf rsyncd.pid
[root@localhost run]# rsync --daemon
[root@localhost run]# netstat -anpt | grep rsync
tcp 0 0 20.0.0.11:873 0.0.0.0:* LISTEN 1581/rsync
##客户端##
[root@localhost opt]# ./inotify_rsync.sh
[root@localhost ~]# cd /opt/myrsync/
[root@localhost myrsync]# touch 222.txt
[root@localhost ~]# ./inotify.sh
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
##源端##
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ll
total 0
-rw-r--r-- 1 nobody nobody 0 Oct 24 23:30 222.txt
六 脚本报错解决办法
[root@localhost ~]# ./inotify.sh
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
##源端##
[root@localhost html]# vi /etc/rsyncd.conf //将uid,pid用户改为root
uid = root
gid = root
[root@localhost html]# netstat -anpt | grep 873
tcp 0 0 20.0.0.11:873 0.0.0.0:* LISTEN 1150/rsync
[root@localhost html]# kill -9 1150
[root@localhost html]# netstat -anpt | grep 873
[root@localhost html]# rsync --daemon
failed to create pid file /var/run/rsyncd.pid: File exists
[root@localhost html]# cd /var/run/
[root@localhost run]# rm -rf rsyncd.pid
[root@localhost run]# rsync --daemon
[root@localhost run]# netstat -anpt | grep 873
tcp 0 0 20.0.0.11:873 0.0.0.0:* LISTEN 1172/rsync
##客户端##
[root@localhost myrsync]# touch 333.txt
[root@localhost ~]# ./inotify.sh //没有报错
##源端##
[root@localhost run]# cd /var/www/html/
[root@localhost html]# ll
total 0
-rw-r--r-- 1 root root 0 Oct 24 23:30 222.txt
-rw-r--r-- 1 root root 0 Oct 24 23:35 333.txt