Rsync搭建与备份

提到备份可以联想到cp、scp和ln等等这些命令,这并不是我们今天的主角,Rsync同样可以进行这些操作,比之这几个命令实现的备份功能更为强大,最重要的一点是能够实现增量备份,大量节省时间。大家需要记住的是rsync的端口是873!
================== 
               实验环境
==================
0)rsync  version 3.0.9 

1)rsync是C/S模式,一台服务端,可多台客户端。(至少两台).

2)配置为同一网段,C:192.168.1.10/24 ; S:192.168.1.20/24.

3)采用push模式,客户端需备份目录为/data,可以将数据打包放在这里.

4)服务端的模块为backup.

5)防火墙和Selinux处于关闭状态.

=====================
                服务端配置
=====================
1)安装rysncd,过程省略.
首先配置"/etc/rsyncd.conf"
锁文件:保持数据的安全性。
configure to /etc/rsyncd.conf
uid = rsync    #用户远端的命令访问rsync共享的目录.
gid = rsync    #用户组.
use chroot = no    #安全相关.
max connections = 200     #最大连接数.
pid file = /var/run/rsyncd.pid    #进程对应的进程号文件.
lock file = /var/run/rsync.lock    #锁文件,为了安全.
log file = /var/log/rsyncd.log    #日志文件.
timeout = 300     #超时时间.
ignore errors      #忽略I/O错误.
read only = false      #仅读为假,可读.
list = false    #不可以列表.
hosts allow = 10.0.0.0/24     #允许访问的网段.
#hosts deny = 0.0.0.0/32     #拒绝的网段,如果存在两个会出现优先于拒绝.
auth users = rsync_backup    #连接rsync的字符串,不算是一个用户.这个用户可以不存在.
secrets file = /etc/rsync.password    #上述的密码文件.
path = /backup      #服务器提供访问的服务目录.


->cp /etc/rsyncd.conf{,.bak} #对这个文件进行一个备份.

2)建立rsync虚拟用户
-> useradd rsync -s /sbin/nologin -M && chown -R rsync. /backup


3)创建密码文件

->echo "rsync_backup:123456" >/etc/rsync.password && chmod 600 /etc/rsync.password #安全机制文件必须为600.

4)rsysnc --daemon && lsof -i:873 #记住哦,rysnc端口是873!! 

================
           客户端配置
================
1)配置密码文件
-> echo 123456 >/etc/rsyncd.password && chmod 600 /etc/rsyncd.password  #客户端不需要做过多的配置,与服务端不通的是只需要一个密码即可。

2)采用push方式
-> rsync --avzP --delete /data/ [email protected]::backup --password-file=/etc/rsync.password


or -> rsync --avzP --delete /data/ rsync://[email protected]/backup --password-file=/etc/rsync.password



*rsync一些常用参数介绍
-v #输出
-vzrtopg #保持属性 等于“-avz"
-r #对子目录以递归模式,即目录下的所有目录都同样传输.
-t #保持文件时间信息.
-o #保持文件属主信息.
-p #保持文件权限.
-g #保持文件属组信息.
-P #保留因故障没传输完的文件,以加快随后的再次传输.
-z #对备份文件在传输时进行压缩.
-e #ssh方式传输.可以使用ssh key通过定时任务往服务器推.
eg. rsync -avz /etc/test.txt -e "ssh -p 22" [email protected]:/tmp/


差异化拷贝

--exclude={a..b}

--exclude=a --exclude=b


--delete #无差异同步,再推送的时候以客户端文件为标准。

--bwlimit=1000  #单位k,限速。


pull方式

-> rsync -avzP [email protected]::backup /backup --password-file=/rsync.password


rsync -avzP rsync://[email protected]/backup /backup --password-file=/etc/rsync.password

小总结

rsync优点:

1)可以与inotify支持增量备份,以及结合定时任务,支持daemon,以客户端为参照物的推拉。

2)远程SHELL通道模式可以加密SSH传输,daemon可以通过VPN或ipsec服务进行加密传输。

缺点:
1)大量小文件同步的时候,对比时间比较长,rysnc进程可能会停止。


2)同步大文件,10G这样的大文件有时也会中断,未完整同步前是隐藏文件,通过续传参数实现传输。

==========================
        服务端做了一个脚本,供参考
==========================
刚学的shell不久,写了一个Rsync服务端的脚本,供大家使用!
#!/bin/bash
#E-mail :[email protected]
user="rsync"
userchar="rsync_backup"
allow='10.0.0.0/24'
module1="backup"
path1="/backup"
module2="example"
path2='/example'
passfile="/etc/rsync.password"
password="123456"
red='\033[0;31m'
. /etc/init.d/functions
function usage(){
[[ $EUID -ne 0 ]] &&\
        echo -e "${red}Error:$0 This script must be run as root!" &&\
		exit 1
}
#comfirm ISO is CentOS!
function ISO(){
	if [ -f /etc/redhat-release ]; then
    		release="centos"
			action "IS CentOS" /usr/bin/true
    else
			action "Must be ISO as CentOS" /usr/bin/false
			exit 2
	fi
}
#install RSYNC
function install(){
	if [ `rpm -qa|grep rsync|wc -l` -eq 0 ] ;then
		yum install -y rsync & >/dev/null;
		action "RSYNC INSTALL SCUESS!" /usr/bin/true
	else
		echo  "rsync early install!" 
	fi
}
#create user
function user(){
	if [ `grep rsync /etc/passwd|wc -l` -eq 0 ];then
		/usr/sbin/useradd -s /sbin/nologin -M rsync & >/dev/null;
		action "rsync user create scuess!" /usr/bin/true
	fi
}
#declare /etc/rsyncd.conf
function config(){
#Configure for /etc/rsync.conf
cat >>/etc/rsyncd.conf<<-EOF
uid = $user
gid = $user
use chroot = no
max connections = 200
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
secrets file = $passfile
timeout = 300
ignore errors
read only = false
list = false
hosts allow = $allow
auth users = $userchar
#hosts deny = 0.0.0.0/32
[$module1]
path = $path1
[$module2]
path = $path2
	EOF
[ ! -d $path1 ] && mkdir -p $path1
[ ! -d $path2 ] && mkdir -p $path2
/usr/bin/chown rsync. $path1 $path2 -R
echo "$userchar:$password" >$passfile && chmod 600 $passfile
}
echo -------------------------------------------------
cat $passfile
#open rsyncd
function open(){
	if [ `netstat -tnlp|grep rsync|grep -v grep|wc -l` -eq 0 ];then
	rsync --daemon & >/dev/null
	action "rsync state" /usr/bin/true
else
	action "rsync has already started" /usr/bin/true
fi
}
function main(){
	usage
	ISO
	install
	user
	config
	open
}
main

===========================

希望我的文档对你有帮助,我会继续更新的

===========================

猜你喜欢

转载自blog.csdn.net/csdn_changsha/article/details/79939177