设为首页 收藏本站
查看: 1283|回复: 0

[经验分享] linux——rsync简介

[复制链接]

尚未签到

发表于 2019-2-17 13:25:03 | 显示全部楼层 |阅读模式
  rsync简介
rsync是Linux系统下的数据镜像备份工具,使用快速增量备份工具 Remote sync 可以远程同步,支持本地复制,或者与其他SSH,rsync主机同步
  rsync
  -a 归档模式
-v 详细输出
-q 静默输出
-r 对子目录递归模式处理
-p 保持原有的权限属性
-z  在传输时压缩
--delete 在源服务器上做删除操作也会在目标服务器上同步
  -c 打开效验开关
-R 使用相对路径
-b 创建备份
  rsync命令
//Rsync的三种命令格式
  rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
  //对应以上三种命令格式,rsync有三种不同的工作模式:
1.拷贝本地文件
[root@localhost ~]# rsync -a nfs.sh a.sh
  2.使用远程shell程序(rsh,ssh)来实现将本地机器的内容拷贝到远程机器
[root@localhost ~]# rsync -avz nfs.sh root@192.168.56.138:/root/b.sh
[root@localhost ~]# ssh root@192.168.56.138 'ls -l /root'
  3.使用一个远程shell程序(如rsh,ssh)来实现将远程机器的内容拷贝到本地机器
[root@localhost ~]# rsync -avz root@192.168.56.138:/etc/yum.repos.d /root/
  环境说明
A机——源服务器——IP192.168.56.11——应用(rsync,inotify-tools,脚本 )——centos7系统
B机——目标服务器——IP192.168.56.138——应用(rsync)——centos7系统
  使用ssh,传输密钥给B机,方便免密码登陆
  [root@localhost ~]# ssh-keygen -t rsa                   //密钥生成
[root@localhost ~]# ls .ssh/
id_rsa  id_rsa.pub
[root@localhost ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.56.138     //A机将生成的公钥交给B机
[root@localhost ~]# ssh root@192.168.56.138             //尝试登陆,发现无需密码
[root@localhost ~]# exit                                //退出B机
  rsync+inotify
需求:把源服务器上/etc目录实时同步到目标服务器的/tmp/下
  安装环境 A B 先关闭防火墙
  [root@localhost ~]# systemctl status firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
AB机安装rsync
  [root@localhost ~]# yum -y install rsync       A机
[root@localhost ~]# yum -y install rsync       B机
B机设置rsyncd.conf配置文件
  [root@localhost ~]# touch /etc/rsync.pass
[root@localhost ~]# cat >> /etc/rsyncd.conf  /etc/rsync.pass
[root@localhost ~]# cat /etc/rsync.pass
123456
  //设置文件权限,只设置文件所有者具有读取,写入权限即可
[root@localhost ~]# chmod 600 /etc/rsync.pass
[root@localhost ~]# ll /etc/rsync.pass
-rw-------. 1 root root 7 8月  13 17:51 /etc/rsync.pass
  [root@localhost ~]# ls          //创建测试目录
anaconda-ks.cfg
  [root@localhost ~]# mkdir -pv /root/etc/test
mkdir: 已创建目录 "/root/etc"
mkdir: 已创建目录 "/root/etc/test"
  [root@localhost ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.56.138::etc_from_client --password-file=/etc/rsync.pass
  sending incremental file list
deleting systemd-private-5434e220d18940898b6d9672af036026-vmtoolsd.service-MRNEKi/tmp/vmware-root/
deleting systemd-private-5434e220d18940898b6d9672af036026-vmtoolsd.service-MRNEKi/tmp/
deleting systemd-private-5434e220d18940898b6d9672af036026-vmtoolsd.service-MRNEKi/
deleting systemd-private-5434e220d18940898b6d9672af036026-vgauthd.service-EHAGL9/tmp/
deleting systemd-private-5434e220d18940898b6d9672af036026-vgauthd.service-EHAGL9/
deleting systemd-private-5434e220d18940898b6d9672af036026-cups.service-cQXF9F/tmp/
deleting systemd-private-5434e220d18940898b6d9672af036026-cups.service-cQXF9F/
deleting .font-unix/
deleting .esd-1000/
deleting .XIM-unix/
deleting .X11-unix/
deleting .Test-unix/
deleting .ICE-unix/
./
test/
  sent 75 bytes  received 670 bytes  1,490.00 bytes/sec
total size is 0  speedup is 0.00
  B机
  [root@localhost ~]# ls /tmp
test
[root@localhost ~]# ll /proc/sys/fs/inotify/           查看服务器是否支持inotify,有三max则支持
总用量 0
-rw-r--r--. 1 root root 0 8月  14 02:04 max_queued_events
-rw-r--r--. 1 root root 0 8月  14 02:04 max_user_instances
-rw-r--r--. 1 root root 0 8月  14 02:04 max_user_watches
  //安装inotify-tools
[root@localhost ~]# yum -y install make gcc gcc-c++
[root@localhost ~]# yum -y install inotify-tools
  A机
  //写同步脚本,最重要的一步,让脚本自动去检测我们制定的目录下 \
//文件发生的变化,然后执行rsunc的命令把它同步到服务器端
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# touch /scripts/inotify.sh
[root@localhost ~]# chmod 755 /scripts/inotify.sh
[root@localhost ~]# ll /scripts/inotify.sh
-rwxr-xr-x. 1 root root 0 8月  13 18:15 /scripts/inotify.sh
  [root@localhost ~]# vim /scripts/inotify.sh
host=192.168.56.138
src=/etc
des=etc_from_client
password=/etc/rsync.pass
user=admin
inotifywait=/usr/bin/inotifywait
  $inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files;do
rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
  //检查脚本
[root@localhost bin]# bash -x /scripts/inotify.sh
  //启动脚本
[root@localhost ~]# nohup bash /scripts/inotify.sh &
[1] 58297
  [root@localhost bin]# ps -ef|grep inotify
root      74599      1  0 17:01 pts/0    00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc
root      74621   2316  0 17:03 pts/0    00:00:00 bash /scripts/inotify.sh
root      74622  74621  0 17:03 pts/0    00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc
root      74623  74621  0 17:03 pts/0    00:00:00 bash /scripts/inotify.sh
root      74625   2316  0 17:03 pts/0    00:00:00 grep --color=auto inotify
  //在源服务器上生成一个新文件
[root@localhost ~]# mkdir -p /etc/httpd24
[root@localhost ~]# ls /etc/httpd24
[root@localhost ~]#  echo 'hello world' > /etc/httpd24/test
  //查看inotify生成的日志
[root@localhost ~]# tail /tmp/rsync.log
20180816 17:05 /etc/httpd24CREATE,ISDIR was rsynced         //创建,ISDIR是同步的
20180816 17:05 /etc/httpd24/testCREATE was rsynced            //同步创建
20180816 17:05 /etc/httpd24/testMODIFY was rsynced           //同步修改
  设置开机自启动
  [root@localhost ~]# chmod +x /etc/rc.d/rc.local
[root@localhost ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 4月  11 15:36 /etc/rc.d/rc.local
  [root@localhost ~]#  echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
[root@localhost ~]# tail /etc/rc.d/rc.local
to run scripts during boot instead of using this file.
  In contrast to previous versions due to parallel execution during boot
this script will NOT be run after all other services.
  Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
that this script will be executed during boot.
touch /var/lock/subsys/local
nohup /bin/bash /scripts/inotify.sh
  到目标服务器上查看是否把新生成的文件自动传上去了:
  [root@localhost tmp]# pwd
/tmp
[root@localhost tmp]# ls
etc  test
  [root@localhost tmp]#  ls etc/httpd24/
test
//以将源服务器的/etc目录整个同步到了目标服务器,新增的test文件也自动同步了




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-673604-1-1.html 上篇帖子: 我与Linux 下篇帖子: linux之raid
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表