Rsync实操

Rsync实操

一.rsync命令

复制代码
 #类似于cp
 [root@user2 ~]# rsync info.sh root@192.168.168.130:/root
 root@192.168.168.130's password: 
 [root@user1 ~]# ls
 anaconda-ks.cfg  ceph-release-1-1.el7.noarch.rpm  info.sh

二、使用rsync备份push方式

  • 服务器:server 192.168.168.130(需要进行数据备份的主机)

  • 客户端:client 192.168.168.131 (备份存储的主机)

    在客户端上编写rsync配置文件,创建一个存放备份的同步目录

    [root@targetpc ~]# vim /etc/rsyncd.conf
    [root@targetpc ~]# cat /etc/rsyncd.conf
    port=873
    address = 192.168.168.131 #客户端地址
    uid = root
    gid = root
    use chroot = yes
    max connections = 4
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    log file = /var/log/rsyncd.log
    motd file = /etc/rsyncd.motd #可弄可不弄
    hosts allow = 192.168.168.0/24 #允许哪个网段传输数据
    [data] #网络名
    path = /data/backup #数据存储目录 真实名 需要创建
    comment = bakcup data
    read only = false
    list = yes
    #独立验证
    auth users = rsyncuser
    secrets file = /etc/rsync.passwd #自己创建

    #设置独立账户密码
    [root@targetpc ~]# vim /etc/rsync.passwd
    #添加权限
    [root@targetpc ~]# chmod 600 /etc/rsync.passwd
    [root@targetpc ~]# vim /etc/rsync.passwd
    [root@targetpc ~]# cat /etc/rsync.passwd
    rsyncuser:123
    #设置标语
    [root@targetpc ~]# echo "来送数据了" > /etc/rsyncd.motd
    [root@targetpc ~]# systemctl restart rsyncd

    #测试 服务器测试
    [root@sourcepc data]# ls
    1.txt 2.txt 3.txt aaa
    [root@sourcepc data]# rsync -avz /data/* rsyncuser@192.168.168.131::data
    来送数据了

    Password:
    sending incremental file list
    3.txt

    sent 155 bytes received 36 bytes 20.11 bytes/sec
    total size is 0 speedup is 0.00
    #客户主机结果
    [root@targetpc backup]# ls
    1.txt 2.txt 3.txt aaa

复制代码
 ​

三、测试--delete命令push

复制代码
 #--delete实验 不能与*同时用
 [root@targetpc backup]# ls
 1.txt  2.txt  3.txt  aaa  del.txt
 [root@sourcepc data]# ls
 1.txt  2.txt  3.txt  aaa
 ​
 [root@sourcepc data]# rsync -avz --delete /data/* rsyncuser@192.168.168.131::data
 来送数据了
 ​
 Password: 
 sending incremental file list
 ​
 sent 119 bytes  received 13 bytes  13.89 bytes/sec
 total size is 0  speedup is 0.00
 [root@targetpc backup]# ls
 1.txt  2.txt  3.txt  aaa  del.txt
 #使用--delete时 去掉*
 [root@sourcepc data]# rsync -avz --delete /data/ rsyncuser@192.168.168.131::data
 来送数据了
 ​
 Password: 
 sending incremental file list
 deleting del.txt
 ./
 ​
 sent 149 bytes  received 31 bytes  18.95 bytes/sec
 total size is 0  speedup is 0.00
 [root@targetpc backup]# ls
 1.txt  2.txt  3.txt  aaa

四、免密同步

复制代码
 
复制代码
#免密
 [root@sourcepc data]# echo "123" > /etc/rsync.passwd
 [root@sourcepc data]# rsync -avz --delete /data/ rsyncuser@192.168.168.131::data --password-file=/etc/rsync.passwd #注意时--password 全称
 来送数据了
 ​
 ERROR: password file must not be other-accessible
 rsync error: syntax or usage error (code 1) at authenticate.c(196) [sender=3.1.2]
 #设置权限
 [root@sourcepc data]# chmod 600 /etc/rsync.passwd
 [root@sourcepc data]# rsync -avz --delete /data/ rsyncuser@192.168.168.131::data --password-file=/etc/rsync.passwd
 来送数据了
 ​
 sending incremental file list
 ​
 sent 142 bytes  received 13 bytes  23.85 bytes/sec
 total size is 0  speedup is 0.00

五、pull模式

以上的同步过程都是服务端主动推送数据给目标主机,这里演示下目标主机主动拉取数据进行同步

复制代码
 
复制代码
# 服务端上配置/etc/rsyncd.conf文件
 port=873
 address = 192.168.168.130
 uid = root
 gid = root
 use chroot = yes
 max connections = 4
 pid file = /var/run/rsyncd.pid
 lock file = /var/run/rsync.lock
 log file = /var/log/rsyncd.log
 #motd file = /etc/rsyncd.motd
 hosts allow = 192.168.168.0/24
 [data]
 path = /data
 comment =  data
 read only = false
 list = yes
 auth users = rsyncuser
 secrets file = /etc/rsync.passwd
 # 认证文件
 [root@sourcepc log]# cat  /etc/rsync.passwd
 rsyncuser:123
 # 服务端上启动rsync
 [root@sourcepc log]# systemctl start rsyncd
 [root@sourcepc log]# systemctl status rsyncd
 ● rsyncd.service - fast remote file copy program daemon
    Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; disabled; vendor preset: disabled)
    Active: active (running) since 四 2025-06-19 20:35:23 CST; 2s ago
  Main PID: 2097 (rsync)
    CGroup: /system.slice/rsyncd.service
            └─2097 /usr/bin/rsync --daemon --no-detach
 ​
 6月 19 20:35:23 sourcepc systemd[1]: Started fast remote file copy program daemon.
 # 目标主机上拉取数据
 [root@client data]# rsync -avz rsyncuser@192.168.168.130::data /data
 Password: 
 receiving incremental file list
 ./
 1.txt
 2.txt
 3.txt
 aaa12/
 bbb/
 ccc/
 ​
 sent 100 bytes  received 305 bytes  162.00 bytes/sec
 total size is 0  speedup is 0.00
 [root@client data]# ls
 1.txt  2.txt  3.txt  aaa12  backup  bbb  ccc

六、rsync+sersync 实现数据实时同步push

复制代码
 #在需要备份主机处 安装sersync 使用的外部安装包
 #使用rz 调入安装包
 [root@sourcepc ~]# rz
 [root@sourcepc ~]# ls
 anaconda-ks.cfg                  info.sh
 ceph-release-1-1.el7.noarch.rpm  sersync2.5.4_64bit_binary_stable_final.tar.gz
 #解压
 [root@sourcepc ~]# tar -xf sersync2.5.4_64bit_binary_stable_final.tar.gz
 [root@sourcepc ~]# ls
 anaconda-ks.cfg                  GNU-Linux-x86  sersync2.5.4_64bit_binary_stable_final.tar.gz
 ceph-release-1-1.el7.noarch.rpm  info.sh
 #进行配置
 [root@sourcepc ~]# cd GNU-Linux-x86/
 [root@sourcepc GNU-Linux-x86]# ls
 confxml.xml  sersync2
 [root@sourcepc GNU-Linux-x86]# vim confxml.xml 
 #修改的部分
  </filter>
     <inotify>
         <delete start="true"/>
         <createFolder start="true"/>
         <createFile start="true"/>
         <closeWrite start="true"/>
         <moveFrom start="true"/>
         <moveTo start="true"/>
         <attrib start="true"/>
         <modify start="true"/>
     </inotify>
 ​
     <sersync>
         <localpath watch="/data">
             <remote ip="192.168.168.131" name="data"/>
             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
         </localpath>
         <rsync>
             <commonParams params="-artuz"/>
             <auth start="true" users="rsyncuser" passwordfile="/etc/rsync.passwd"/>
             <userDefinedPort start="false" port="874"/><!-- port=874 -->
             <timeout start="false" time="100"/><!-- timeout=100 -->
             <ssh start="false"/>
         </rsync>
 ​
 #执行文件
 [root@sourcepc GNU-Linux-x86]# ./sersync2 -d -r -o ./confxml.xml 
 set the system param
 execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
 execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
 parse the command param
 option: -d      run as a daemon
 option: -r      rsync all the local files to the remote servers before the sersync work
 option: -o      config xml name:  ./confxml.xml
 daemon thread num: 10
 parse xml config file
 host ip : localhost     host port: 8008
 will ignore the inotify createFile event 
 daemon start,sersync run behind the console 
 use rsync password-file :
 user is rsyncuser
 passwordfile is         /etc/rsync.passwd
 config xml parse success
 please set /etc/rsyncd.conf max connections=0 Manually
 sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) 
 Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
 please according your cpu ,use -n param to adjust the cpu rate
 ------------------------------------------
 rsync the directory recursivly to the remote servers once
 working please wait...
 execute command: cd /data && rsync -artuz -R --delete ./ rsyncuser@192.168.168.131::data --password-file=/etc/rsync.passwd >/dev/null 2>&1 
 [root@sourcepc GNU-Linux-x86]# run the sersync: 
 watch path is: /data
 ​
 #测试
 [root@sourcepc data]# ls
 1.txt  2.txt  3.txt  aaa
 [root@sourcepc data]# mkdir bbb
 ​
 [root@targetpc backup]# ls
 1.txt  2.txt  3.txt  aaa  bbb
 ​
 ​
 [root@sourcepc data]# ls
 1.txt  2.txt  3.txt  aaa  bbb  ccc
 [root@sourcepc data]# mv aaa aaa12
 [root@targetpc backup]# ls
 1.txt  2.txt  3.txt  aaa12  bbb  ccc
相关推荐
Jackiejin5268 小时前
WPS表格选择性粘贴快捷键怎么使用?WPS如何给表格某一行都加上指定数字?
数据分析·excel·wps
办公解码器9 小时前
Excel怎么将八位数字设置为日期格式?
excel
gis99 小时前
批量地址解析坐标,支持WPS、EXCEL软件,支持导出SHP、GEOJSON、DXF等文件格式
excel·wps
赵庆明老师10 小时前
ASP.NET Core读取Excel文件
excel
luyun02020213 小时前
流批了,pdf批量转excel
windows·pdf·excel·figma
老师可可13 小时前
成绩发布工具使用方法,附成绩分析教程
学习·信息可视化·小程序·excel·学习方法
SamDeepThinking18 小时前
处理大型excel文件的技术选型
excel
技术钱1 天前
vue3前端解析excel文件
前端·vue.js·excel
VBAMatrix1 天前
数据重构!按一级科目拆分序时账,批量生成明细账
excel·财务·审计·会计师事务所·tb工具箱·明细账
缺点内向2 天前
Java 使用 Spire.XLS 库合并 Excel 文件实践
java·开发语言·excel