rsync免密传输 linux rsync 密码

sync 免密方式自动同步文件:

rsync的客户端方式能够自动同步文件,非常方便,但是它在远程拷贝文件之前提示还需要输入一个密码,要实现脚本自动拷贝,必须要优化掉这个步骤,让rsync以密码文件的方式工作,就不需要再手工输入密码了,但密码文件的方式工作需要两步简单的配置。

环境说明:

suse安装后自带rsync命令程序,使用最简单的客户端方式工作。

备份机:192.168.11.25, 同步文件的目的地。

服务器:192.168.11.24, 同步文件的数据源,一般是从服务器拷贝数据去备份。

命令:

一、在备份机192.168.0.1上产生密码文件,有了这个文件,我们就可以不再手动输入密码了:
-----------------------------------
 

ssh-keygen

输入这个命令之后 一路回车  yes

二、拷贝这个密码文件到服务器主机上:

ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.11.24

三、就这么简单,我们在备份机上用rsync同步命令试试,果然再不需要手工输入密码了:

#没有斜杠/
rsync -av 192.168.11.24:/home/runner/pbmonitor  /usr/local/rsync/   #同步文件夹 pbmonitor
#有斜杠/
rsync -av 192.168.11.24:/home/runner/pbmonitor/  /usr/local/rsync/  #不同步文件夹 pbmonitor


如果是端口不是3322的话

rsync -avH --delete   -e "ssh -p 3322"  192.168.11.24:/usr/local/nginx/html/  /usr/local/nginx/html/

四、我们把这个命令加到定时脚本中,让备份服务器自动每天从服务器主机上同步一次文件。

五、我们不能让备份机上的文件无休止的增长,需要脚本命令删除掉10天前的文件:

find  /usr/local/rsync/*  -mtime +10 -exec rm -f {} \;

六、安装Inotify-tools实时触发rsync进行同步
这里可以参考github上的官方wiki文档(包含安装及配置使用示例)

 Home · inotify-tools/inotify-tools Wiki · GitHub

1、先安装epel源

yum install -y epel-release && yum update

1、下载安装Inotify-tools

# uname -r        #Linux下支持inotify的内核最小为2.6.13
2.6.32-642.el6.x86_64

# 安装前要先下载epel源
# yum install inotify-tools -y

查看其程序是否安装成功
# rpm -qa inotify-tools
inotify-tools-3.14-1.el6.x86_64

查看程序包含的文件
#rpm -ql inotify-tools
/usr/bin/inotifywait
/usr/bin/inotifywatch
/usr/lib64/libinotifytools.so.0
/usr/lib64/libinotifytools.so.0.4.1
/usr/share/doc/inotify-tools-3.14
/usr/share/doc/inotify-tools-3.14/AUTHORS
/usr/share/doc/inotify-tools-3.14/COPYING
/usr/share/doc/inotify-tools-3.14/ChangeLog
/usr/share/doc/inotify-tools-3.14/NEWS
/usr/share/doc/inotify-tools-3.14/README
/usr/share/man/man1/inotifywait.1.gz
/usr/share/man/man1/inotifywatch.1.gz

2、配置inotify-tools

# sysctl -a|egrep -i "max_queued_events|max_user_watches|max_user_instances"    #修改inotify默认参数(inotify默认内核参数值太小)
fs.inotify.max_user_instances = 128
fs.inotify.max_user_watches = 8192
fs.inotify.max_queued_events = 16384
fs.epoll.max_user_watches = 201420

# vim /etc/sysctl.conf 添加
fs.inotify.max_queued_events = 99999999
fs.inotify.max_user_watches = 99999999
fs.inotify.max_user_instances = 65535

#sysctl  -p   参数立即生效

# cat /proc/sys/fs/inotify/{max_user_instances,max_user_watches,max_queued_events}  #检查参数是否生效
65535
99999999
99999999


注释:
    max_queued_events:inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
    max_user_watches:要同步的文件包含多少目录,可以用:find /app/rsync_server/ -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/app/rsync_server/为同步文件目录)
    max_user_instances:每个用户创建inotify实例最大值

3、创建实时同步脚本

# vim  /usr/local/inotify/rsync.sh
#!/bin/bash
src_dir="/app/rsync_server/"
dst_dir="app_rsync_client"
exclude_dir="/usr/local/inotify/exclude.list"
rsync_user="rsync"
rsync_passwd="/etc/passwd.txt"
dst_ip="192.168.0.45"
rsync_command(){
                  rsync -avH --port=873 --progress --delete --exclude-from=$exclude_dir $src_dir $rsync_user@$ip::$dst_dir --password-file=$rsync_passwd
}
for ip in $dst_ip;do
     rsync_command
done
    /usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $src_dir \
| while read file;do
   for ip in $dst_ip;do
       rsync_command
       echo "${file} was rsynced" >> /tmp/rsync.log 2>&1
   done
 done 

注释:
    src_dir="/app/rsync_server/"    #源服务器同步目录
    dst_dir="app_rsync_client"    #目标服务器rsync同步目录模块名称
    exclude_dir="/usr/local/inotify/exclude.list"    #不需要同步的目录,如果有多个,每一行写一个目录,使用相对于同步模块的路径;
    例如:不需要同步/app/rsync_server/"目录下的a目录和b目录下面的b1目录,exclude.list文件可以这样写
    a/
    b/b1/
    
    rsync_user="rsync"    #目标服务器rsync同步用户名
    rsync_passwd="/etc/passwd.txt"    #目标服务器rsync同步用户的密码在源服务器的存放路径
    dst_ip="192.168.0.45"    #目标服务器ip,多个ip用空格分开
##赋权,添加开机启动

# chmod +x /usr/local/inotify/rsync.sh
# touch /usr/local/inotify/exclude.list
# vim /etc/rc.d/rc.local
nohup /bin/sh /usr/local/inotify/rsync.sh &
# nohup /bin/sh /usr/local/inotify/rsync.sh &

4、测试

在rsync_server(数据源)192.168.0.44的/app/rsync_server创建文件
# cd /app/rsync_server
# touch test{1..9}
# touch test{a..j}
# ls
test1  test2  test3  test4  test5  test6  test7  test8  test9  testa  testb  testc  testd  teste  testf  testg  testh  testi  testj

在rsync_client(目标端)192.168.0.45上查看已经同步
# cd /app/rsync_client
# ls
test1  test2  test3  test4  test5  test6  test7  test8  test9  testa  testb  testc  testd  teste  testf  testg  testh  testi  testj

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值