rsync+inotify实现远程数据备份
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rsync+inotify实现远程数据备份相关的知识,希望对你有一定的参考价值。
一、rsync的基本介绍
1、 什么是rsync
Rsync是一款开源的、快速的、多功能的、可以实现增量的本地货远程数据镜像同步备份的优秀工具,Rsync使用与unix,linux,windows等多种平台
2、 Rsync的特性
1) 支持拷贝特殊文件
2) 可以有排除指定文件或目录
3) 可以保持原来文件或目录的权限
4) 可以实现增量同步,即只同步变化的数据
5) 可以使用rcp,ssh等方式配合传输文件
6) 支持匿名或认证的进程模式传输
7) 传输前会进行压缩,适合异地备份
8)使用tcp 873端口
3、 rsync工作方式
1) 本地数据传输
Rsync [option] src dst
案例:
[[email protected] ~]# cd /opt/
[[email protected] opt]# mkdir test
[[email protected] opt]# touch test/11.txt
[[email protected] opt]# chmod -R 700 test/
[[email protected] opt]# ls
rh test
[[email protected] opt]# ls -l
total 8
drwxr-xr-x. 2 root root 4096 Mar 26 2015 rh
drwx------. 2 root root 4096 Sep 9 10:01 test
[[email protected] opt]# rsync -avz /opt/ /tmp/
(其中/opt/,仅仅把/opt/目录里面的内容同步过去,opt目录本身不同步,而/opt表示把opt目录以及内容全部同步到/tmp下)
sending incremental file list
created directory /tmp
./
rh/
test/
test/11.txt
sent 128 bytes received 42 bytes 340.00 bytes/sec
total size is 0 speedup is 0.00
[[email protected] opt]# ls -l /tmp
total 8
drwxr-xr-x. 2 root root 4096 Mar 26 2015 rh
drwx------. 2 root root 4096 Sep 9 10:01 test
2) 远程传输(通过ssh传输)
拉取(pull):所有主机定时去找一台主机拉数据
rsync [option] [[email protected][HOST….:src….dest
推送(push):一台主机负责把数据传给其他主机
rsync [option]src….[[email protected]]host:dest
实例:
服务器:server1.cn IP: 192.168.119.128
客户端:server2.cn IP: 192.168.119.130
(在远程同步任务中,负责发起rsync同步操作的客户端称为发起端,而负责响应
来自客户端的rsync同步操作的服务器为备份源)
在server1服务器上
[[email protected] ~]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64
[[email protected] ~]# yum -y install xinetd
[[email protected] ~]# vim /etc/xinetd.d/rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
# allows crc checksumming etc.
service rsync
{
disable = no
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
[[email protected] ~]# service xinetd start
Starting xinetd: [ OK ]
[[email protected] ~]# ss -tnl |grep 873
LISTEN 0 64 :::873 :::*
常用的选项:
-a 相当于-rlptgoD(经常使用)
-r :对子目录以递归模式处理
-p:保持文件原有权限
-z: --compress表示压缩传输(经常使用)
-p:显示传输速度(经常使用)
--delete 删除那些目标位置有而原始位置没有的文件
--exclude= 需要过滤的文件
-v显示同步过程的详细信息
实战:备份server1上/usr/local/nginx/html 到server2的/web-back上,创建用户reg1
在server1上配置如下
[[email protected] ~]# useradd rget1
[[email protected] ~]# echo ‘test123‘|passwd --stdin rget1
Changing password for user rget1.
passwd: all authentication tokens updated successfully.
[[email protected] ~]# cd /usr/local/nginx/html/
[[email protected] html]# ld - /usr/local/nginx/html/
ld: -: No such file: No such file or directory
[[email protected] html]# ls -d /usr/local/nginx/html/
/usr/local/nginx/html/
[[email protected] html]# ls -ld /usr/local/nginx/html/
drwxr-xr-x. 2 root root 4096 Sep 9 10:59 /usr/local/nginx/html/
[[email protected] html]# setfacl -R -m user:rget1:rwx /usr/local/nginx/html/
[[email protected] html]# setfacl -R -m default:rget1:rwx /usr/local/nginx/html/
[[email protected] html]# getfacl /usr/local/nginx/html/
getfacl: Removing leading ‘/‘ from absolute path names
# file: usr/local/nginx/html/
# owner: root
# group: root
user::rwx
user:rget1:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:rget1:rwx
default:group::r-x
default:mask::rwx
default:other::r-x
在server2上配置如下
[[email protected] ~]# mkdir /web-backup
[[email protected] ~]# rsync -avzp --delete [email protected]:/usr/local/nginx/html/ /web-backup/ (拉取)
[email protected]‘s password:
receiving incremental file list
./
50x.html
index.html
index.php
sent 71 bytes received 923 bytes 284.00 bytes/sec
total size is 1169 speedup is 1.18
[[email protected] ~]# ls -l /web-backup/
total 12
-rw-rwxr--. 1 root root 537 Sep 8 10:37 50x.html
-rw-rwxr--. 1 root root 612 Sep 8 10:37 index.html
-rw-rwxr--. 1 root root 20 Sep 8 11:59 index.php
实战2:使用ssh密钥实现无交互备份,将server1上的数据定期备份到server2上
在server2上配置:
[[email protected] ~]# ssh-keygen -t 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:
9e:c0:ed:c3:cc:db:d5:49:2a:58:7f:7a:f6:3d:f2:87 [email protected]
The key‘s randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| . . |
| o S . . |
| B + . + . |
| X . + +. |
| + o +E.o|
| . . .ooo=|
+-----------------+
[[email protected] ~]# ssh-copy-id [email protected]
[email protected]‘s password:
Now try logging into the machine, with "ssh ‘[email protected]‘", and check in:
.ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting.
测试:ssh [email protected]
[[email protected] ~]# rsync -azp --delete [email protected]:/usr/local/nginx/html/ /web-backup/
[[email protected] ~]# ll -l /web-backup/
total 12
-rw-rwxr--. 1 root root 537 Sep 8 10:37 50x.html
-rw-rwxr--. 1 root root 612 Sep 8 10:37 index.html
-rw-rwxr--. 1 root root 20 Sep 8 11:59 index.php
编写脚本:
[[email protected] ~]# vim rsync.sh
Src=/usr/local/nginx/html/
Dst=/web-backup
rsync -az --delete [email protected]:$Src $Dst
[[email protected] /]# crontab -e
no crontab for root - using an empty one
01 3 * * * /root/rsync.sh
3) 以守护进程的方式传输
实战3:配置rsync服务器及需要备份的目录,不使用系统用户进行备份
在server1端创建/etc/rsyncd.conf
[[email protected] ~]# vim /etc/rsyncd.conf
uid=nobody
gid=nobody
address=192.168.119.128
port=873
host allow= 192.168.119.130 192.168.119.131
use chroot=yes (锁定家目录)
max connections=5 (最大连接数)
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsync.lock
log file=/var/log/rsyncd.log
motd file=/etc/rsyncd.motd
[wwwroot]
path=/usr/local/nginx/html/
comment=rsync wwwroot of www.test.com
read only=yes (以只读的方式提供备份)
list=yes(允许查看模块信息)
auth users=backuper(指定备份的用户,与系统用户无关)
secrets file = /etcrsync.passwd(指定存放用户的密码文件)
[[email protected] ~]# echo "welcome to backup server" > /etc/rsyncd.motd
[[email protected] ~]# vim /etc/rsync.passwd
Backuper:pwd123
[[email protected] ~]# chmod 600 /etc/rsync.passwd
[[email protected] ~]#vim /etc/xinetd.d/rsync (把disabled=yes改成disabled=no)
[[email protected] ~]# service xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
在server2上测试:
[[email protected] ~]# rsync -azP [email protected]::wwwroot /web-backup/
rsync: failed to connect to 192.168.119.128: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.6]
(表示对方有防火墙阻止掉了,需要放行873端口)
[[email protected] ~]# rsync -azP [email protected]::wwwroot /web-backup/
welcome to backup server
Password:
receiving incremental file list
./
html/
html/50x.html
537 100% 524.41kB/s 0:00:00 (xfer#1, to-check=5/10)
html/index.html
612 100% 39.84kB/s 0:00:00 (xfer#2, to-check=4/10)
html/index.php
20 100% 1.22kB/s 0:00:00 (xfer#3, to-check=3/10)
logs/
logs/access.log
1977 100% 113.57kB/s 0:00:00 (xfer#4, to-check=2/10)
logs/error.log
1495 100% 81.11kB/s 0:00:00 (xfer#5, to-check=1/10)
sbin/
sbin/nginx
6382486 100% 10.70MB/s 0:00:00 (xfer#6, to-check=0/10)
sent 190 bytes received 2747564 bytes 50417.50 bytes/sec
total size is 6387127 speedup is 2.32
[[email protected] ~]# ls -l /web-backup/
total 24
-rw-rwxr--. 1 root root 537 Sep 8 10:37 50x.html
drwxrwxr-x. 2 root root 4096 Sep 9 10:59 html
-rw-rwxr--. 1 root root 612 Sep 8 10:37 index.html
-rw-rwxr--. 1 root root 20 Sep 8 11:59 index.php
drwxr-xr-x. 2 root root 4096 Sep 8 10:39 logs
drwxr-xr-x. 2 root root 4096 Sep 8 10:37 sbin
不用输入密码,直接备份数据,可以脚本
[[email protected] ~]# export RSYNC_PASSWORD=pwd123456
[[email protected] ~]# rsync -azP [email protected]::wwwroot /web-backup/
二、配置rsync+inotify实现实时同步
以上是关于rsync+inotify实现远程数据备份的主要内容,如果未能解决你的问题,请参考以下文章