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=边复制 ,边统计,边比较,可以备份大量数据.可以镜像保存整个目录 ...
随机推荐
- case when语句后的表达式
SQL中Case When语句的语法如下 Simple CASE expression: CASE input_expression WHEN when_expression THEN result_ ...
- oracle 返回第一个不为空的列的值
) from emp; 作用是返回函数coalesce参数中第一个不为null的值.
- 新年PR交期回写,展望期由14天改为30天,FP_PR2SAP ;转单量改为100W;FP_PR2SAP_MOD_NEW
- oracle 正则表达式
1.获取不包含 欧洲|*北美|*中国|*欧拉非 字符串的行. SELECT/* count(*)*/* FROM table t WHERE NOT regexp_like(t.xxxx, '( ...
- sscanf与正则表达式(转)
今天翻google reader的时候看到这样一篇文章,介绍的是sscanf的高级用法.直到今天我才知道sscanf是可以直接用正则表达式的,惭愧. 在msdn中sscanf的声明如下 int ssc ...
- jquery里面的名称冲突解决方法
jQuery 使用 $ 符号作为 jQuery 的简介方式. 某些其他 JavaScript 库中的函数(比如 Prototype)同样使用 $ 符号. jQuery 使用名为 noConflict( ...
- LDAP与SSH
一般情况下,客户端配置好之后,ssh是可以直接用的,若不能,则需手动配置 1:vim /etc/ssh/sshd_config 把UsePAM改成yes 2:在vim /etc/pam.d/sshd添 ...
- 第5章 LINQ
5.4 LINQ查询运算符 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- Android之数据库升级onUpgrade降级onDowngrade
借用API文档解释: public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 当数据库需要升 ...
- 足球游戏AI_资料收集
实况足球中文官网 浅谈足球游戏的人工智能 用遗传算法加强足球游戏的人工智能 足球规则图解 守门员的技巧你知道吗? 教你足球守门员守门技术练习方法和技巧 足球守门员规则 判断点球方向