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
案例:
[root@db ~]# cd /opt/
[root@db opt]# mkdir test
[root@db opt]# touch test/11.txt
[root@db opt]# chmod -R 700 test/
[root@db opt]# ls
rh test
[root@db 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
[root@db 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
[root@db 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] [user@[HOST….:src….dest
推送(push):一台主机负责把数据传给其他主机
rsync [option]src….[user@]host:dest
实例:
服务器:server1.cn IP: 192.168.119.128
客户端:server2.cn IP: 192.168.119.130
(在远程同步任务中,负责发起rsync同步操作的客户端称为发起端,而负责响应
来自客户端的rsync同步操作的服务器为备份源)
在server1服务器上
[root@server4 ~]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64
[root@server4 ~]# yum -y install xinetd
[root@server4 ~]# 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
}
[root@server4 ~]# service xinetd start
Starting xinetd: [ OK ]
[root@server4 ~]# 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上配置如下
[root@server4 ~]# useradd rget1
[root@server4 ~]# echo 'test123'|passwd --stdin rget1
Changing password for user rget1.
passwd: all authentication tokens updated successfully.
[root@server4 ~]# cd /usr/local/nginx/html/
[root@server4 html]# ld - /usr/local/nginx/html/
ld: -: No such file: No such file or directory
[root@server4 html]# ls -d /usr/local/nginx/html/
/usr/local/nginx/html/
[root@server4 html]# ls -ld /usr/local/nginx/html/
drwxr-xr-x. 2 root root 4096 Sep 9 10:59 /usr/local/nginx/html/
[root@server4 html]# setfacl -R -m user:rget1:rwx /usr/local/nginx/html/
[root@server4 html]# setfacl -R -m default:rget1:rwx /usr/local/nginx/html/
[root@server4 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上配置如下
[root@db ~]# mkdir /web-backup
[root@db ~]# rsync -avzp --delete rget1@192.168.119.128:/usr/local/nginx/html/ /web-backup/ (拉取)
rget1@192.168.119.128'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
[root@db ~]# 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上配置:
[root@db ~]# 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 root@db
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| . . |
| o S . . |
| B + . + . |
| X . + +. |
| + o +E.o|
| . . .ooo=|
+-----------------+
[root@db ~]# ssh-copy-id rget1@192.168.119.128
rget1@192.168.119.128's password:
Now try logging into the machine, with "ssh 'rget1@192.168.119.128'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
测试:ssh reget1@192.168.119.128
[root@db ~]# rsync -azp --delete rget1@192.168.119.128:/usr/local/nginx/html/ /web-backup/
[root@db ~]# 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
编写脚本:
[root@db ~]# vim rsync.sh
Src=/usr/local/nginx/html/
Dst=/web-backup
rsync -az --delete rget1@192.168.119.128:$Src $Dst
[root@db /]# crontab -e
no crontab for root - using an empty one
01 3 * * * /root/rsync.sh
3) 以守护进程的方式传输
实战3:配置rsync服务器及需要备份的目录,不使用系统用户进行备份
在server1端创建/etc/rsyncd.conf
[root@server4 ~]# 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(指定存放用户的密码文件)
[root@server4 ~]# echo "welcome to backup server" > /etc/rsyncd.motd
[root@server4 ~]# vim /etc/rsync.passwd
Backuper:pwd123
[root@server4 ~]# chmod 600 /etc/rsync.passwd
[root@server4 ~]#vim /etc/xinetd.d/rsync (把disabled=yes改成disabled=no)
[root@server4 ~]# service xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
在server2上测试:
[root@db ~]# rsync -azP backuper@192.168.119.128::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端口)
[root@db ~]# rsync -azP backuper@192.168.119.128::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
[root@db ~]# 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
不用输入密码,直接备份数据,可以脚本
[root@db ~]# export RSYNC_PASSWORD=pwd123456
[root@db ~]# rsync -azP backuper@192.168.119.128::wwwroot /web-backup/
二、配置rsync+inotify实现实时同步
rsync+inotify实现远程数据备份的更多相关文章
- rsync+inotify实现服务器数据同步
一.什么是rsync rsync,remote synchronize是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限.时间.软硬链接等附加信息.rsync是用 “rsync算 ...
- Rsync+inotify自动同步数据
一.简介 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足. 首先,rsync在同步数据时,需要扫描所有文件后进行比对,进行差量传 ...
- 【集群实战】Rsync试题-异机数据备份解决方案
企业案例:Rsync上机实战考试题: 某公司有一台Web服务器,里面的数据很重要,但是如果硬盘坏了,数据就会丢失,现在领导要求你把数据在其它机器上做一个周期性定时备份. 要求如下: 每天晚上00点整在 ...
- rsync+inotify实现server实时备份
inotify实现对文件夹下文件进行监听的原理: inotify集成到内核中,通过内核提供的接口.使用inotify作为第三方的软件对文件夹变化进行监控. inotifywait命令能够对文件夹中的文 ...
- rsync本地及远程复制备份【原创】
1.安装rsyncyum instsall rsync 2.本地复制 rsync -auq --progress --delete /tongbu1/ /tongbu2/ rsync -auq --p ...
- 通过rsync+inotify实现数据实时备份
rsync的优点与不足 与传统的cp,scp,tar,备份方式相比,rsync具有安全性高备份迅速支持增量备份的优点,可以满足对实时性要求不高的需求,例如定期备份文件服务器数据到远端服务器,但是,当数 ...
- centos7 rsync+inotify软件实现集群服务的数据备份(一)
一.rsync软件的说明: 1.1 什么是rsync rsync是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.它使用所谓的“Rsync演算法”来使本地和远程两个主机之间的文件达 ...
- linux之rsync远程数据同步备份
rsync服务是一种高效的远程数据备份的工具,该服务的port号为873, 是Liunx下的一种非独立服务.由xinetd超级服务管理,取代监听873port. 长处: 1.rsync能够利用ssh和 ...
- 开源服务专题之-------rsync数据备份
RSYNC是Remote Sync 远程同步的简称,与SCP的比较,SCP= 无法备份大量数据,类似windows的复制,而rsync=边复制 ,边统计,边比较,可以备份大量数据.可以镜像保存整个目录 ...
随机推荐
- Android 工程师如何快速学会web前段
Android 工程师如何快速学会web前段 今天主要聊一下本人最近在学习web前段的感受,最近html5是越来越火了,前段时间公司做了一个项目然后让我们“android”的程序猿过去帮忙把客户 端框 ...
- js获取IP和MAC地址
1.IP 百度一下有很多 可以用这个 <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> ...
- 循序渐进Python3(十)-- 0 -- RabbitMQ
RabbitMQ RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需求,虽然在同步消息 ...
- The user specified as a definer ('root'@'%') does not exist
The user specified as a definer ('root'@'%') does not exist 此种报错主要是针对访问视图文件引起的(没有权限) 解决方法: 2.进入mysql ...
- 编码UTF-8
☯,首先,这并不是图片,这是一个unicode字符,Yin Yang,即阴阳符,码点为U+262F.如果你的浏览器无法显示,可以查看这个链接http://www.fileformat.info/inf ...
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
控制台包空指针后跟这个异常,是因为控制层调用service时的失败,无法读到sql,问题在于controller在引入的service没有自动装配,在引入多个service时,每个service都要自 ...
- iOS 根据字符串内容动态计算行高
+ (CGFloat)textHeightFromTextString:(NSString *)text width:(CGFloat)textWidth fontSize:(CGFloat)size ...
- [刘阳Java]_MyBatis_映射文件的常用标签总结_第5讲
MyBatis中常用标签的总结,简单给出自己的总结 MyBatis映射文件中的标签使用介绍1.<select>:用于编写查询语句用的标签 id:表示当前<select>标签的唯 ...
- strstr函数的用法
C语言函数 编辑 包含文件:string.h 函数名: strstr 函数原型: extern char *strstr(char *str1, const char *str2); 语法: ...
- SpringMVC 400 Bad Request 问题
摘要 SpringMVC 400 Bad Request 在提交表单时,发生400错误,并未进入save方法. @RequestMapping(value="/!save",met ...