rsync远程同步

发起rsync远程同步的叫做发起端  响应远程同步的叫备份源 


1.配置ssh备份源  

新建操作用户 

root@rs-s ~]# useradd rget

[root@rs-s ~]# passwd rget

[root@rs-s ~]# useradd rput

[root@rs-s ~]# passwd rput

确认sshd服务正常运行,设置允许rget、rput访问

[root@rs-s ~]# vim /etc/ssh/sshd_config

... ...

UseDNS no                        //找到这个选项修改成“no”

AllowUsers rget rput        //添加这个选项

[root@rs-s ~]# service  sshd  restart


2.设置备份源文件和目录的ACL

要备份哪个文件夹就设置哪个

在linux中文件可有ACL 规则可以打开/etc/fstab

在default后面加上,ACL        (服务支持访问控制列表)

配置完后重启

设置权限以及ACL

root@localhost ~]# chown -R rput:rput /var/www/html      让rput有html目录的属主权限

[root@localhost ~]# setfacl -R -m user:nginx:rwx /var/www/html/upload    递归添加ACL条目

[root@localhost ~]# setfacl -m default:user:nginx:rwx /var/www/html/upload     设置默认ACL

以下是清空ACL规则命令

[root@localhost ~]# setfacl -R -b /var/www/html/upload/             这条命令可以清楚所有ACL             改用“-x user:...”可只删除指定ACL条目

  可以用这条命令查看新增的ACL

root@localhost ~]# getfacl /var/www/html/upload/

……

user::rwx

user:nginx:rwx

……

default:user::rwx

default:user:nginx:rwx

3.

应用示例

用户backuper,允许下行同步

操作的目录为 /var/www/html/

首先新建配置文件 

[root@localhost ~]# vi /etc/rsyncd_users.db

backuper:pwd123

othername:123456

……

[root@localhost ~]# chmod 600 /etc/rsyncd_users.db

root@localhost ~]# vi /etc/rsyncd.conf

uid = nobody

gid = nobody

use chroot = yes

address = 192.168.4.4                //自己的IP               

port 873

log file = /var/log/rsyncd.log

pid file = /var/run/rsyncd.pid

hosts allow = 192.168.4.0/24       //允许谁访问

[wwwroot]

    path = /var/www/html

    comment = Document Root of www1.benet.com

    read only = yes

    dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z           //不再压缩这些格式

    auth users = backuper                                    //这行和下行都是用户认证信息

    secrets file = /etc/rsyncd_users.db


4.

启用rsync服务

通过--daemon独自提供服务

[root@localhost ~]# rsync --daemon

[root@localhost ~]# netstat -anpt | grep rsync

tcp       0    0 192.168.4.200:873     0.0.0.0:*       LISTEN      21182/rsync

[root@localhost ~]# kill $(cat /var/run/rsyncd.pid


5.rsync命令的用法

rsync命令的用法

§基本格式:rsync [选项] 原始位置 目标位置

§常用选项:

-a:归档模式,递归并保留对象属性,等同于 -rlptgoD

-v:显示同步过程的详细(verbose)信息

-z:在传输文件时进行压缩(compress)

-H:保留硬连接文件

-A:保留ACL属性信息

--delete:删除目标位置有而原始位置没有的文件

--checksum:根据对象的校验和来决定是否跳过文件

-rlptgoD含义:

-r:递归模式,包含目录及子目录中所有文件

-l:对于符号链接文件仍然复制为符号链接文件

-p:保留文件的权限标记

-t:保留文件的时间标记

-g:保留文件的属组标记(仅超级用户使用)

-o:保留文件的属主标记(仅超级用户使用)

-D:保留设备文件及其他特殊文件

备份操作类型

备份操作类型

§本地同步

rsync  ...  本地目录1  本地目录2

§rsync+ssh同步

rsync  ...  SSH源  本地目录

rsync  ...  本地目录  SSH源

§rsync+rsync同步

rsync  ...  rsync源  本地目录

rsync  ...  本地目录  rsync源

6.rsync同步操作示例

下行SSH源:/var/www/html  → /wwwroot

(就是本地复制本地用SSH  rsync不安全)

ssh:

[root@localhost ~]# mkdir /wwwroot

[root@localhost ~]# rsync -avzH --delete [email protected]:/var/www/html/ /wwwroot

[email protected]'s password:

receiving incremental file list

created directory /wwwroot

html/

html/index.html

html/style/

……

以上操作就是把html目录里的东西同步到/wwwroot里

rsync:

[root@localhost ~]# mkdir /myweb

[root@localhost ~]# rsync -avzH --delete [email protected]::wwwroot /myweb

Password:

receiving incremental file list

./

index.html

index.php

……

以上操作时把刚刚新建的wwwroot被分到/myweb中 

用户backuper是在上面的配置文件中写的

wwwroot 是在 /etc/rsyncd.conf    中加载的。 



7. rsync源的免交互处理  (创建公钥对)


在CentOS6.4中证书上传很方便 

然后根据不用的备份源 执行不同的方案 

1)ssh备份源的无交互式登录

ssh-keygen -t rsa    //生成

ssh-copy-id [email protected]  //上传               

ssh-copy-id [email protected] //最好把rput的也上传了


2)rsync备份源 


设置环境变量:RSYNC_PASSWORD

或者,使用 --password-file= 选项调用密码文件

export RSYNC_PASSWORD=pwd123  (填写你的密码就好了 backuper的)

  然后执行脚本


脚本内容

[root@localhost ~]# vi /root/rsync_get_wwwroot.sh

#!/bin/bash

CMD="/usr/bin/rsync"

RSYNC_USER="backuper"

RSYNC_PASSWROD="pwd123"              //用户backuper的密码

ARGS="-az --delete"

SRC="192.168.4.4::wwwroot"           

DST="/wwwroot"

mkdir -p $DST

$CMD $ARGS $RSYNC_USER@$SRC $DST

给这个脚本700权限 

crontab -e 编写任务计划

开机自启

service crond restart

chkconfig crond on 


配置rsync+inotify实时同步 

一、vLinux内核的inotify机制

§从版本2.6.13开始提供

§可以监控文件系统的变动情况,并作出通知响应

§辅助软件:inotify-tools

1)调整inotify内核参数

max_queue_events:监控队列大小

max_user_instances:最多监控实例数

max_user_watches:每个实例最多监控文件数

[root@localhost ~]# vi /etc/sysctl.conf

……

fs.inotify.max_queued_events = 16384

fs.inotify.max_user_instances = 1024

fs.inotify.max_user_watches = 1048576

[root@localhost ~]# sysctl -p


操作都是在备份源上做的 

2)安装inotify-tools辅助工具

inotifywait:用于持续监控,实时输出结果

inotifywatch:用于短期监控,任务完成后再出结果

root@localhost ~]# inotifywait -mrq -e modify,create,move,delete /var/www/html

Setting up watches.  Beware: since -r was given, this may take a while!

Watches established.

/var/www/html/ CREATE index.php

/var/www/html/ MODIFY index.php

/var/www/html/ MOVED_FROM index.php

/var/www/html/ MOVED_TO test.php



-m,持续进行监控

-r,递归监控所有子对象

-q,简化输出信息


-e,指定要监控哪些事件类型


3)

通过inotifywait触发rsync同步操作

使用while死循环来不断获取监控结果

一旦侦测到目标变化,inotifywait将终结运行,则运行rsync进行备份

[root@localhost ~]# vi /opt/inotify_rsync.sh

#!/bin/bash

INOTIFY_CMD="inotifywait -rq -e modify,create,attrib,move,delete /var/www/html/"

RSYNC_CMD="rsync -azH --delete /var/www/html/ [email protected]:/var/www/html"

while true

do

    $INOTIFY_CMD  &>  /dev/null

    $RSYNC_CMD  &>  /dev/null

done

给权限 

执行


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章