rsync远程同步

本文介绍了rsync工具的用法,包括如何配置和执行远程同步。通过设置rsync守护进程,并结合inotifywait工具,可以实现实时文件同步。实验展示了在源服务器上创建文件后,客户机自动接收同步的过程,强调了rsync在实时备份和减少无谓同步上的优势。
摘要由CSDN通过智能技术生成

rsync远程同步简介

1.什么是rsync

是一种快速增量备份工具,能远程同步,支持本地复制,或与其他SSH、rsync主机同步

2. 用法

rsync [选项] 原始位置 目标位置

常用选项

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

-v:显示同步过程的详细详细

-z;在传递文件是进行压缩

-H:保留硬链接文件

-A;保留ACL属性信息

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

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

3.配置方法

(1:用户名@主机地址::共享模块名

(2:rsync://用户名@主机地址/共享模块名

4.同步分类

定期同步

实时同步

5.两者优缺点

定期同步缺点:执行备份时间固定,延迟明显、实时性差,当同步源长期不变时,定期任务就没有必要了

实时同步优点:一旦同步源出现变化,立即启动备份,只有没有变化,就不启动备份。

6实验

同步

服务器20.0.0.5

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

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

# See rsyncd.conf man page for more options.

# configuration example:

uid = nobody
gid = nobody
use chroot = yes
pid file = /var/run/rsyncd.pid
address = 20.0.0.5
port 873
log file = /var/log/rsyncd.log
hosts allow = 20.0.0.0/24

# max connections = 4

# exclude = lost+found/

# transfer logging = yes

# timeout = 900

# ignore nonreadable = yes

# [ftp]

#        path = /home/ftp

#        comment = ftp export are

#

#        a

#
[wwwroot]
path = /var/www/html
comment = www.kgc.cn
read only =yes
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
auth users = backuper
secrets file = /etc/rsyncd_users.db
[root@localhost ~]# vim /etc/rsyncd_users.db
backuper:abc123
[root@localhost ~]# chmod 600 /etc/rsyncd_users.db
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# rsync --daemon
[root@localhost ~]# netstat -ntap | grep rsync
tcp        0      0 20.0.0.5:873            0.0.0.0:*               LISTEN      73588/rsync         
[root@localhost ~]# cd /var/www/html
[root@localhost html]# ll
total 0
[root@localhost html]# echo "this is abc" > ab.txt
[root@localhost html]# ll
total 4
-rw-r--r--. 1 root root 12 Oct 23 11:19 ab.txt

客户机20.0.06

[root@localhost ~]# cd /opt
[root@localhost opt]# rsync -avz backuper@20.0.0.5::wwwroot /opt/
Password: 
receiving incremental file list
./
ab.txt

sent 46 bytes  received 119 bytes  36.67 bytes/sec
total size is 12  speedup is 0.07
[root@localhost opt]# cat ab.txt
this is abc



实时同步

客户机20.0.0.6

[root@localhost opt]#  vim /etc/sysctl.conf
后面加
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 10224
fs.inotify.max_user_watches = 1048576
 [root@localhost opt]# sysctl -p
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 10224
fs.inotify.max_user_watches = 1048576

上传软件包到opt目录下

[root@localhost opt]# tar zxvf inotify-tools-3.14.tar.gz 
   [root@localhost opt]#  cd inotify-tools-3.14/
   [root@localhost inotify-tools-3.14]#   yum -y install gcc gcc-c++ make
  [root@localhost inotify-tools-3.14]# ./configure 
  [root@localhost inotify-tools-3.14]#  make && make install
   inotifywait -mrq -e modify,create,move,delete /var/www/html/
    yum -y install httpd
     inotifywait -mrq -e modify,create,move,delete /var/www/html/
  [root@localhost inotify-tools-3.14]# cd /opt/
[root@localhost opt]# ls

 [root@localhost opt]# vim inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/
backuper@20.0.0.5::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
 if [ $(pgrep rsync | wc -l) -le 0 ];then
  $RSYNC_CMD
 fi
done


[root@localhost opt]# chmod +x inotify.sh
[root@localhost opt]# ll
总用量 364
-rw-r--r--. 1 root  root      12 10月 23 11:19 ab.txt
-rwxr-xr-x. 1 root  root     316 10月 23 15:56 inotify.sh
drwxrwxrwx. 6 zhang zhang   4096 10月 23 14:24 inotify-tools-3.14
-rw-r--r--. 1 root  root  358772 10月 23 12:22 inotify-tools-3.14.tar.gz
drwxr-xr-x. 2 root  root       6 10月 31 2018 rh
[root@localhost opt]# ls -l /var/www/
总用量 0
drwxr-xr-x. 2 root root 6 11月  5 2018 cgi-bin
drwxr-xr-x. 2 root root 6 10月 23 14:52 html
[root@localhost opt]# chmod 777 /var/www/
[root@localhost opt]# ls -l /var/www/
总用量 0
drwxr-xr-x. 2 root root 6 11月  5 2018 cgi-bin
drwxr-xr-x. 2 root root 6 10月 23 14:52 html
[root@localhost opt]# ls -l /var/www/html
总用量 0
[root@localhost opt]# chmod 777 /var/www/html
[root@localhost opt]# ls -l /var/www/
总用量 0
drwxr-xr-x. 2 root root 6 11月  5 2018 cgi-bin
drwxrwxrwx. 2 root root 6 10月 23 14:52 html
[root@localhost opt]# ./inotify.sh

在打开一台20.0.0.6的客户机

[root@localhost html]# echo "this is test" > test.txt
[root@localhost html]# 

源服务器

root@localhost html]# ls -l /var/www
total 0
drwxr-xr-x. 2 root root  6 Nov  5  2018 cgi-bin
drwxr-xr-x. 2 root root 20 Oct 23 11:19 html
[root@localhost html]# chmod 777 /var/www/html
[root@localhost html]# ls -l /var/www
total 0
drwxr-xr-x. 2 root root  6 Nov  5  2018 cgi-bin
drwxrwxrwx. 2 root root 20 Oct 23 11:19 html
[root@localhost html]# vim /etc/rsyncd.conf
[wwwroot]
path = /var/www/html
comment = www.kgc.cn
read only =no    //yes改为no
"/etc/rsyncd.conf" 33L, 671C    
[root@localhost html]# netstat -ntap | grep rsync
tcp        0      0 20.0.0.5:873            0.0.0.0:*               LISTEN      73588/rsync         
[root@localhost html]# kill -9 73588
[root@localhost html]# netstat -ntap | grep rsync
[root@localhost html]# cd /var/run
[root@localhost run]# rm -rf rsyncd.pid
[root@localhost run]# rsync --daemon
[root@localhost run]# netstat -ntap | grep rsync
tcp        0      0 20.0.0.5:873            0.0.0.0:*               LISTEN      76246/rsync   
[root@localhost run]# cd /var/www/html/
[root@localhost html]# ls
test.txt
[root@localhost html]# cat test.txt
this is test
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值