rsync定时同步配置
rsync定时同步配置rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。
rsync定时同步配置1. rsync简述:rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。可以很容易做到保持原来文件的权限、时间、软硬链接等等。无须特殊权限即可安装。快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。 2. rsync语法Local: rsync [OPTION...] SRC... [DEST] Access via remote shell: Pull: rsync [OPTION...] [[email?protected]]HOST:SRC... [DEST] Push: rsync [OPTION...] SRC... [[email?protected]]HOST:DEST Access via rsync daemon: Pull: rsync [OPTION...] [[email?protected]]HOST::SRC... [DEST] rsync [OPTION...] rsync://[[email?protected]]HOST[:PORT]/SRC... [DEST] Push: rsync [OPTION...] SRC... [[email?protected]]HOST::DEST rsync [OPTION...] SRC... rsync://[[email?protected]]HOST[:PORT]/DEST rsync 在本地进行推送的话类似于cp命令,远程推送类似于scp,例如把本机的/etc/hosts文件推送到/tmp 通过ssh加密通道推送文件有 [[email?protected] ~]# rsync -avz -e ‘ssh -p 22‘ sersync2.5.4_64bit_binary_stable_final.tar.gz [email?protected]:/tmp/ [email?protected]‘s password: sending incremental file list sent 70 bytes received 12 bytes 32.80 bytes/sec total size is 727290 speedup is 8869.39 通过ssh加密通道拉取文件 [[email?protected] ~]# rsync -avz -e ‘ssh -p 22‘ [email?protected]:/tmp/sersync2.5.4_64bit_binary_stable_final.tar.gz /tmp/ [email?protected]‘s password: receiving incremental file list sersync2.5.4_64bit_binary_stable_final.tar.gz sent 30 bytes received 727651 bytes 161706.89 bytes/sec total size is 727290 speedup is 1.00 [[email?protected] ~]# ls -l /tmp/ |grep ser* -rw-r--r--. 1 root root 727290 May 21 2018 sersync2.5.4_64bit_binary_stable_final.tar.gz 3. 参数详解
4. rsync 的daemon模式平时如果不用daemon模式也可以随便的进行传输,但是需要输入密码,如果利用ssh key进行弄则系统可能不安全,不利于全网备份,因此最好的解决方案就是利用rsync的daemon模式来解决rsync推送需要密码的问题。
[[email?protected] ~]# uname -r 2.6.32-696.23.1.el6.x86_64 [[email?protected] ~]# uname -m x86_64 [[email?protected] ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [[email?protected] ~]# rsync --version rsync version 3.0.6 protocol version 30 [[email?protected] ~]# uname -r 2.6.32-696.23.1.el6.x86_64 [[email?protected] ~]# uname -m x86_64 [[email?protected] ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [[email?protected] ~]# rsync --version rsync version 3.0.6 protocol version 30
touch /etc/rsyncd.conf 在该配置文件中添加如下内容: # rsyncd-conf start uid = rsync gid = rsync use chroot = no max connections = 2000 timeout = 600 pid file= /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log ignore = errors read only = false list = false hosts allow = 192.168.50.0/24 hosts deny = 0.0.0.0/32 auth users = rsync-backup secrets file = /etc/rsync.password ##################################### [backup] comment = backup path = /backup
[[email?protected] ~]# useradd -g rsync -M -s /bin/nologin rsync [[email?protected] ~]# grep "rsync" /etc/passwd rsync:x:506:506::/home/rsync:/sbin/nologin [[email?protected] ~]# mkdir /backup [[email?protected] /]# chown -R rsync.rsync /backup [[email?protected] data]# echo "rsync-backup:root" >/etc/rsync.password [[email?protected] data]# chmod 600 /etc/rsync.password [[email?protected] data]# ls -l /etc/rsync.password -rw-------. 1 root root 18 May 10 21:57 /etc/rsync.password
rsync --daemon echo "rsync --daemon" >>/etc/rc.local [[email?protected] /]# netstat -lntup |grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1351/rsync tcp 0 0 :::873 :::* LISTEN 1351/rsync [[email?protected] /]# lsof -i :873 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsync 1351 root 3u IPv4 12638 0t0 TCP *:rsync (LISTEN) rsync 1351 root 5u IPv6 12639 0t0 TCP *:rsync (LISTEN) [[email?protected] /]#
[[email?protected] data]# echo "root" >>/etc/rsync.password [[email?protected] data]# chmod 600 /etc/rsync.password [[email?protected] data]# ls -l /etc/rsync.password -rw-------. 1 root root 5 May 14 20:44 /etc/rsync.password
[[email?protected] data]# rsync -avzP /data/file [email?protected]::backup/ --password-file=/etc/rsync.password sending incremental file list sent 50 bytes received 8 bytes 116.00 bytes/sec total size is 6 speedup is 0.10 5. rsync如果无法推送的排错思路
6. rsync的注意事项:
7. 利用rsync进行全网定时备份如果全网有很多机器,每个机器上都有很多各种各样的重要的配置文件,以及其他的重要的文件数据需要进行定时备份,我们可以用一台服务器安装rsync daemon,这样其他机器都把重要的数据往备份服务器这里推送,进行全网备份,比如每天凌晨2点进行备份一次,解决方案可以利用脚本和cron进行定时备份。
[[email?protected] /]# ifconfig eth0 |awk -F ‘[ :]+‘ ‘NR==2 {print $4}‘ 192.168.50.1
#!/bin/sh
dir="`ifconfig eth0 |awk -F ‘[ :]+‘ ‘NR==2 {print $4}‘`_$(date +%F)"
path=/backup
mkdir $path/$dir -p &&/bin/cp /var/spool/cron/root $path/$dir/cron_root_$(date +%F)/ &&/bin/cp /etc/rc.local $path/$dir/rc.local_$(date +%F)/ &&rsync -az $path [email?protected]::backup/ --password-file=/etc/rsync.password
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |