Rsync的端口是:873
man rsyncd.conf 查看帮助
1.vim /etc/rsyncd.conf vi /etc/rsyncd.conf -->man rsyncd.conf 查看帮助 ######rsyncd.conf####### uid = rsync ----->非系统虚拟用户 gid = rsync use chroot =no -->防止出现安全问题 maxconnections = 200 --->最大连接数 timeout = 300 --->超时时间 pid file =/var/run/rsyncd.pid --->进程pid所在的文件 lock file =/var/run/rsync.lock -->锁 log file =/var/log/rsyncd.log -->出错的日志文件 [backup] --->模块 path = /backup ->可以理解为共享目录 ignore errors --->忽略错误 read only =false --->可读写 list = false -->是否允许列表 hosts allow =192.168.25.0/24 --->允许的主机 hosts deny =0.0.0.0/32 auth users =rsync_backup -->虚拟用户 secrets file= /etc/rsync.password -->用户对应的密码文件 #######rsyncd.config######## 启动/关闭rsync服务进程 rsync --daemon pkill rsync 检查rsync服务/端口 ps -ef|grep rsync|grep -v "grep" ->检查一下进程 netstat -lntup|grep 873 ->检查端口 ==> ss -lntup|grep 873 ==> lsof -i:873
查看日志,确认服务开启
cat /var/log/rsyncd.log
创建用户rsync
useradd rsync -s /sbin/nologin -M 虚拟用户,不需要创建家目录
创建共享目录backup
mkdir /backup chown -R rsync.rsync /backup ls -ld /backup
写入密码进入/etc/rsync.password
echo "rsync_backup:cnp200@HW" >> /etc/rsync.password ->这里 rsync_backup rsync的虚拟用户名,cnp200@HW是rsync的虚拟用户名的密码 cat /etc/rsync.password chmod 600 /etc/rsync.password ls -dl /etc/rsync.password
开机自启动
echo "rsync --daemon">>/etc/rc.local tail /etc/rc.local
写入密码到rsync.password文件
echo "cnp200@HW" > /etc/rsync.password -->只需要密码 cat /etc/rsync.password chmod 600 /etc/rsync.password ls -dl /etc/rsync.password
客户端/etc/hosts目录以及目录本身同步到服务端的back目录(配置文件里配置的module)下
客户端:
rsync -avz /etc/hosts rsync_backup@192.168.25.141::backup --password-file=/etc/rsync.password rsync -avz rsync_backup@192.168.25.141::backup /tmp --password-file=/etc/rsync.password
服务端:
注意: 1.使用绝对路径 2. destination must be a directory when copying more than 1 file
无差异同步(尽量不用) 尽量不用,用之前一定要备份
以本地/tmp为准,tmp有服务器端有,服务器多余的文件删除
push: rsync -avz --delete /tmp rsync_backup@192.168.25.137::backup --password-file=/etc/rsync.password
以服务器端为准,服务器端有,同步到本地/tmp, /tmp多余的文件删除排除
pull: rsync -avz --delete rsync_backup@192.168.25.137::backup /tmp --password-file=/etc/rsync.password
1.服务端:/etc/rsyncd.conf里面添加exclude= a b test/h.txt
2.客户端:
rsync -avz --exclude=/tmp/{1,2} /tmp/ rsync_backup@192.168.25.137::backup --password-file=/etc/rsync.password rsync -avz --exclude=/tmp/{a..g} /tmp/ rsync_backup@192.168.25.137::backup --password-file=/etc/rsync.password rsync -avz --exclude-from=paichu.txt /tmp/ rsync_backup@192.168.25.137::backup --password-file=/etc/rsync.password
1.编写脚本文件hh.sh
#!/bin/sh IP="$(ifconfig eth0|awk -F ‘[ :]+‘ ‘NR==2{print $4}‘)" path="/backup_$IP.$(date +%F)" dir="$IP.$(date +%F)" echo ".................Starting copy rc.local................" mkdir -p $path/$dir $$\ /bin/cp /etc/rc.local $path/$dir/rc.local_$(date +%F) echo ".................Ending copy rc.local................" echo ".................Starting rsync......................." rsync -az $path rsync_backup@192.168.25.141::backup --password-file=/etc/rsync.password echo ".................Ending rsync......................." exit2.设置定时任务
crontab -e #################Crontab for rsync########### 0 0 20 8 * /bin/sh /home/omc/hh.sh >/dev/null 2>&1
搭建企业级全网数据定时备份方案【cron + rsync】2
原文:https://www.cnblogs.com/ftl1012/p/9291758.html