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=边复制 ,边统计,边比较,可以备份大量数据.可以镜像保存整个目录 ...
随机推荐
- 转载:log4j.properties log4j.xml 路径问题
自动加载配置文件: (1)如果采用log4j输出日志,要对log4j加载配置文件的过程有所了解.log4j启动时,默认会寻找source folder下的log4j.xml配置文件,若没有,会寻找lo ...
- bool 类型存在数据库中为 0 和 1
bool 类型存在数据库中为 0 和 1 但是在程序中应该使用 true 和 false 查询. 例如: bIsStart = 0 在数据中bIsStart为 0 sql 查询的时候,使用:sele ...
- 一个页面中显示多个button时总行数计算公式。
总行数 = (按钮总数 + 每一行按钮数 - 1) / 每一行按钮数. 同理.假设我们要显示一定总数的item.每页固定数量,则总页数为. 总页数 = (总显示数量 + 每页显示的数量 - 1) / ...
- asp.net LINQ防止SQL注入式攻击
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- mvc Razor 视图中找不到 ViewBag的定义
在Razor 视图中,我们有时会看到 ViewBag.Title 下会划一个红线,当鼠标放上去的时候会提示这样的一个错误: 找不到编译动态表达式所需的一种或多种类型,是否缺少引用? 但在项目启动运行时 ...
- Android Runtime
[Android Runtime] Every Android application runs in its own process, with its own instance of the Da ...
- The processing instruction target matching ''[xX][mM][lL]" is not allowed
报错的来源是: <?xml version="1.0" encoding="UTF8"?> 解决方案::,一般是WSDL的头文件的格式出了问题,比如 ...
- C++的一些小的知识点
1.初始化: 对于内置类型 ]; // 10个未初始化int ](); // 10个值初始化为0的int 对于自定义类型: 只要一调用new,那么编译器不仅仅给它分配内存,还调用它的默认构造函数初始化 ...
- db2 with ur
这几天查询DB2数据库,老遇到select * from XXX with ur, 好奇ur是什么作用,现在记录一下. DB2中,共有四种隔离级:RS,RR,CS,UR,DB2提供了这4种不同的保护级 ...
- WIN7环境下CUDA7.5的安装、配置和测试(Visual Studio 2010)
以下基于"WIN7(64位)+Visual Studio 2010+CUDA7.5". 系统:WIN7,64位 开发平台:Visual Studio 2010 显卡:NVIDIA ...