rsync远程同步

rsync远程同步
1、配置rsync源服务器
一、配置SSH备份源
(1)新建备份用户rget、rput分别用来下载、上传
[root@host1 ~]# useradd rget html

[root@host1 ~]# passwd rget                #设置密码为rget vim

[root@host1 ~]# useradd rput 安全

[root@host1 ~]# passwd rput                #设置密码为rput 服务器

确认sshd服务正常运行,且容许用户rget、rput访问
[root@host1 ~]# vim /etc/ssh/sshd_config
122 UseDNS no                                    #去掉注释
139 AllowUsers rget rput root                #添加此行能够限定远程登陆用户 ssh

[root@host1 ~]# /etc/init.d/sshd restart socket

调整/var/www/html/目录权限,使rget用户有权读取、rput用户有权写入
[root@host1 ~]# ls -ld /var/www/html/
drwxr-xr-x 6 root root 4096 Jun 30 23:27 /var/www/html/ tcp

[root@host1 ~]# setfacl -R -m u:rput:rwx /var/www/html/        #设置ACL ide

[root@host1 ~]# ls -ld /var/www/html/
drwxr-xr-x+ 6 root root 4096 Jun 30 23:27 /var/www/html/ ui

[root@host1 ~]# getfacl /var/www/html/
getfacl: Removing leading '/' from absolute path names
# file: var/www/html/
# owner: root
# group: root
user::rwx
user:rput:rwx
group::r-x
mask::rwx
other::r-x     rest

配置rsync备份源
(1)创建/etc/rsyncd.conf 配置文件
[root@host1 ~]# vim /etc/rsyncd.conf                        #本身建立
uid = nobody
gid = nobody
use chroot = yes                                                 #禁锢在源目录
adress = 192.168.10.1                                        #监听地址(服务器IP地址)
port 873                                                            #监听端口(服务器端口)
log file = /var/log/rsyncd.log                                #日志文件位置
pid file = /var/run/rsyncd.pid                               #存放进程ID的文件位置
hosts allow = 192.168.10.0/24             #容许访问的客户机地址
[wwwroot]                                         #共享模块名称
        path = /var/www/html                #源目录的实际路径
        read only = yes                         #是否为只读
comment = Document Root of host1.benet.com        #描述
        dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z    #同步时再也不压缩的文件类型
auth users = backup                            #受权帐户
secrets file = /etc/rsyncd_users.db        #存放帐户信息的数据文件    
基于安全性考虑,对于rsync的备份源最好仅容许以只读方式作下行同步,若确实须要作上行同步时,建议改用SSH备份源。另外,下行备份能够采用匿名的方式,只要将其中的“auth users”和“secrets file”配置记录去掉就能够了。

为备份帐户建立数据文件
[root@host1 ~]# cat /etc/rsyncd_users.db
backup:pwd123

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

启动rsync服务程序,运行参数为“--daemon”
[root@host1 ~]# rsync --daemon                    #启动rsync服务

[root@host1 ~]# netstat -anpt | grep rsync
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      1522/rsync         
tcp        0      0 :::873                      :::*                        LISTEN      1522/rsync

[root@host1 ~]# kill $(cat /var/run/rsyncd.pid)            #关闭rsync服务

[root@host1 ~]# cat /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                        #将yes修改成no
        flags           = IPv6
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}    

[root@host1 ~]# yum -y install xinetd                          #安装xinetd软件包               
[root@host1 ~]# /etc/init.d/xinetd restart                    #启动xinetd 服务

配置备份源
命令格式及经常使用备份选项
    备份的基本格式为“rsync [选项] 原始位置 目标位置”,其中经常使用的一些命令选项以下:

-r :递归模式,包含目录及子目录中的全部文件
-l :对于符号连接文件仍然复制为符号连接文件
-v :显示同步过程的详细(verbose)信息
-a :归档模式,保留文件的权限、属性等信息,等同于组合选项 “--rlptgoD”
-z :在传输文件时进行压缩
-p :保留文件的权限标记
-t :保留文件的时间标记
-g :保留文件的属组标记(仅超级用户使用)
-o :保留文件的属主标记(仅超级用户使用)
-H :保留硬连接文件
-A :保留ACL属性信息
-D :保留设备文件及其余特殊文件
--delete :删除目标位置有而原始位置没有的文件
--checksum :根据校验和(而不是文件大小、修改时间)来决定是否跳过文件。

配置源的表示方法
在执行远程同步任务时,rsync命令须要指定备份源服务器中的资源位置。对于下行备份操做,备份源对应“原始位置”;对于上行备份操做,备份源对应“目标位置”。根据备份源的类型不一样,其资源表示方式也不一样。

SSH备份源
[root@host2 ~]# rsync -avz rget@192.168.10.1:/var/www/html/ /var/www/html/

rsync备份源 [root@host2 ~]# rsync -avz backup@192.168.10.1::wwwroot /var/www/html/ 或 [root@host2 ~]# rsync -avz rsync://backup@192.168.10.1/wwwroot /var/www/html/

相关文章
相关标签/搜索