Rsync实操

Rsync实操

一.rsync命令

复制代码
 #类似于cp
 [root@user2 ~]# rsync info.sh [email protected]:/root
 [email protected]'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/* [email protected]::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/* [email protected]::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/ [email protected]::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/ [email protected]::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/ [email protected]::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 [email protected]::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 ./ [email protected]::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
相关推荐
弓.长.8 分钟前
从Excel到知识图谱再到数据分析:数据驱动智能体构建指南
数据分析·excel·知识图谱
写写闲篇儿3 小时前
将多个Excel合并到一个Excel中的方法
excel
葡萄城技术团队5 小时前
这几个 Excel 提升办公效率方法,你知道吗?
excel
流浪猪头拯救地球5 小时前
WPS 和 office (word/excel/ppt) 找到模板所在位置以及更改模板的方式(公文写作格式要求、字体安装、模板下载)
word·excel·wps
CPU不够了7 小时前
使用NPOI库导出多个Excel并压缩zip包
excel
2501_920552568 小时前
Mac电脑 Office 2024 LTSC 长期支持版(Excel、Word、PPT)
macos·word·powerpoint·excel·mac
爱上妖精的尾巴1 天前
3-18 WPS JS宏 颜色设置实例应用(按条件设置单元格颜色)学习笔记
javascript·笔记·学习·excel·wps·js宏·jsa
最美不过下雨天啊1 天前
tp框架导出excel的时候报错:unexcepted identifier “Closure“,excepting variable
php·excel·thinkphp6
Ryo_Yuki2 天前
【上市公司文本分析】根据句号和分号进行文本分割,提取含有特定关键词的语句并导出为EXCEL
python·excel