[TOC]
rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。可以很容易做到保持原来文件的权限、时间、软硬链接等等。无须特殊权限即可安装。快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。
Local: rsync [OPTION...] SRC... [DEST]
Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
Access via rsync daemon:
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
rsync 在本地进行推送的话类似于cp命令,远程推送类似于scp,例如把本机的/etc/hosts文件推送到/tmprsync -avz /etc/hosts /tmp/
rsync -avz /etc/hosts root@172.16.50.1:/tmp/
--delete 目标目录内容与源目录内容保持一致,前面有的,后面可以有,前面没有的,后面的必须删除,如:rsync -avz --delete /test/ /tmp
通过ssh加密通道推送文件有
[root@server ~]# rsync -avz -e ‘ssh -p 22‘ sersync2.5.4_64bit_binary_stable_final.tar.gz root@192.168.50.4:/tmp/
root@192.168.50.4‘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加密通道拉取文件
[root@server ~]# rsync -avz -e ‘ssh -p 22‘ root@192.168.50.4:/tmp/sersync2.5.4_64bit_binary_stable_final.tar.gz /tmp/
root@192.168.50.4‘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
[root@server ~]# ls -l /tmp/ |grep ser*
-rw-r--r--. 1 root root 727290 May 21 2018 sersync2.5.4_64bit_binary_stable_final.tar.gz
平时如果不用daemon模式也可以随便的进行传输,但是需要输入密码,如果利用ssh key进行弄则系统可能不安全,不利于全网备份,因此最好的解决方案就是利用rsync的daemon模式来解决rsync推送需要密码的问题。
[root@rsync-client ~]# uname -r
2.6.32-696.23.1.el6.x86_64
[root@rsync-client ~]# uname -m
x86_64
[root@rsync-client ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@rsync-client ~]# rsync --version
rsync version 3.0.6 protocol version 30
[root@rsync-server ~]# uname -r
2.6.32-696.23.1.el6.x86_64
[root@rsync-server ~]# uname -m
x86_64
[root@rsync-server ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@rsync-server ~]# 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
[root@rsync-server ~]# useradd -g rsync -M -s /bin/nologin rsync
[root@rsync-server ~]# grep "rsync" /etc/passwd
rsync:x:506:506::/home/rsync:/sbin/nologin
[root@rsync-server ~]# mkdir /backup
[root@rsync-server /]# chown -R rsync.rsync /backup
[root@rsync-server data]# echo "rsync-backup:root" >/etc/rsync.password
[root@rsync-server data]# chmod 600 /etc/rsync.password
[root@rsync-server 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
[root@rsync-server /]# 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
[root@rsync-server /]# 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)
[root@rsync-server /]#
[root@rsync-client data]# echo "root" >>/etc/rsync.password
[root@rsync-client data]# chmod 600 /etc/rsync.password
[root@rsync-client data]# ls -l /etc/rsync.password
-rw-------. 1 root root 5 May 14 20:44 /etc/rsync.password
[root@rsync-client data]# rsync -avzP /data/file rsync-backup@192.168.50.4::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
/etc/rsyncd.conf
rsync -avz --delete rsync:://rsync-backup@192.168.50.4/backup /tmp/ --password-file=/etc/rsync.password
如果全网有很多机器,每个机器上都有很多各种各样的重要的配置文件,以及其他的重要的文件数据需要进行定时备份,我们可以用一台服务器安装rsync daemon ,这样其他机器都把重要的数据往备份服务器这里推送,进行全网备份,比如每天凌晨2点进行备份一次,解决方案可以利用脚本和cron进行定时备份。
[root@rsync-client /]# 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 rsync-backup@192.168.50.4::backup/ --password-file=/etc/rsync.password
echo "0 2 * * * sh /service/scripts/bak.sh >/dev/null" >>/var/spool/cron/root
原文地址:http://blog.51cto.com/ouyangtao/2120939