Linux下rsync操作使用

目录

概述

特性

rsync传输模式  

格式

选项 

作为rsync服务 

rsync+inotfy实时同步  


概述:
    rsync是linux 下一个远程数据同步工具
    他可通过LAN/WAN快速同步多台主机间的文件和目录,并适当利用rsync 算法减少数据的传输
    会对比两个文件的不同部分,传输差异部分,因此传输速度相当快
    rsync可拷贝、显示目录属性,以及拷贝文件,并选择性的压缩及递归拷贝

特性
    快速:第一次传输全部,下一次传输差异
               rsync在传输过程中可以实行压缩和解压缩,使用更少的带宽
    安全:可以使用scp、ssh等方式传输
               直接通过socket连接
               支持匿名传输、方便进行网站镜像

应用场景:增量同步、备份迁移

数据的同步方式:数据备份:拉取(下载)、数据恢复:推送(上传)

rsync传输模式
    本地传输:本地同步数据,类似于cp
    远程传输:远程同步数据,类似于scp
    守护进程:通过模块化的方式实现批量传输

rsync应用
    安装:yum -y install rsync
    监听端口号:TCP/873

格式:

作为远程命令:拉取——rsync [OPTION...] [USER@]HOST:SRC... [DEST]
                          推送——rsync [OPTION...] SRC... [USER@]HOST:DEST

[root@localhost ~] rsync 192.168.9.254:666.txt /opt #将192.168.9.254的666.txt 放到我当前主机的/opt
root@192.168.9.254's password:  #输入192.168.9.254密码

[root@localhost opt] ll
-rw-r--r--. 1 root root   0 5月  29 23:11 666.txt  #传过来了
[root@localhost ~] rsync 1.txt 192.168.9.254:/opt   #将当前1.txt传到192.168.9.254
root@192.168.9.254's password: 

[root@localhost opt] ll   
总用量 4
-rw-r--r--. 1 root root 4 5月  29 15:21 1.txt  #192.168.9.254收到
drwxr-xr-x. 2 root root 6 10月 31 2018 rh

选项 :-a        归档模式,递归并保留对象属性,等同于-rlptgoD
            -v        -verbose           显示同步过程的详细信息
            -z        在传输文件时进行压缩

[root@localhost ~] rsync -avz /etc/hosts /opt  #将/etc/hosts放到/opt
sending incremental file list   #成功
hosts

sent 136 bytes  received 35 bytes  342.00 bytes/sec
total size is 158  speedup is 0.92
[root@localhost ~] rsync -avz /etc/hosts 192.168.9.254:/opt  将/etc/hosts放到192.168.9.254的/opt下
root@192.168.9.254's password: 
sending incremental file list    #成功
hosts

sent 136 bytes  received 35 bytes  48.86 bytes/sec
total size is 158  speedup is 0.92

作为rsync服务
    拉取:rsync [OPTION...] [USER@]HOST::SRC... [DEST]
               rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
    推送:rsync [OPTION...] SRC... [USER@]HOST::DEST
               rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

[root@localhost ~] vim /etc/rsyncd.conf 
# configuration example:

 uid = rsync
 gid = rsync
fake sper =yes
list=false
auth users =rsync_backup      #用于指定认证用户
secrets file=/etc/rsync.passwd    #指定认证用户密码
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
read only =false
[backup]
path=/backup
                                          
#服务器修改
[root@localhost ~] useradd rsync -s /sbin/nologin -M    #创建用户
 [root@localhost ~] echo "rsync_backup:123456">/etc/rsync.passwd    #将密码写入密码文件
[root@localhost ~] chmod 600 /etc/rsync.passwd    #修改密码文件权限
[root@localhost ~] mkdir /backup    
[root@localhost ~] chown rsync.rsync /backup    #修改属主属组


#客户端输入
[root@localhost ~] rsync -avz /etc/hosts rsync_backup@192.168.9.1::backup  
Password: 
sending incremental file list

sent 38 bytes  received 12 bytes  14.29 bytes/sec
total size is 158  speedup is 3.16

rsync+inotfy实时同步
    服务器端:安装网站服务,启动,但是不写首页文件
                      修改主配置文件

[root@localhost html] vim /etc/rsy

# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

# uid = nobody
# gid = nobody
 uid = root
 gid = root
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
read only = false
[wwwroot]
path = /var/www/html/
hosts allow = 192.168.9.0/24
comment = backup export area


开发客户端:
安装inotify工具
inotify-tools
tar xzf  inotify-tools-3.14.tar.gz
./configure && make && make install
  

[root@localhost opt] vim /etc/sysctl.conf 

fs.inotify.max_user_instances = 1024
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_watches = 1048576
                   

[root@localhost opt] vim /opt/inotify_rsync.sh
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete  /var/www/html/ 192.168.9.254::wwwroot"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
$RSYNC_CMD
done

[root@localhost opt] chmod +x /opt/inotify_rsync.sh 

[root@localhost opt] vim /etc/rc.local
/root/inotify_rsync.sh
                    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我还能再学点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值