背景:
0. 服务器的数据是非常重要的,一旦发生磁盘损坏就麻烦了。作为一个管理人员,对数据进行实时地同步备份那是很有必要的。
1. rsync 是一个很好和备份工具,下面以Ubuntu为例讲一下它的配制。
2. 假如我的服务器A的ip为10.141.247.12, 我的备份客户机B的ip为10.141.247.13
服务器端的配制:
1. 打开rsync作为服务器的开关,修改文件/etc/default/rsync的内容其中一行如下(整个文件内容见链接)
RSYNC_ENABLE=true
2. 新建配制文件/etc/rsyncd.conf 内容如下(或者见链接)
#[globale] strict modes = yes #rsync default port port = 873 logfile = /var/log/rsyncd.log pidfile = /var/run/rsyncd.pid max connections = 4 auth users = backup, user secrets file = /etc/rsyncd.scrt #[modules] each path responding to a module [appbackup] path = /home/aborn/backup #hosts allow=9.4.122.24 [databackup] path = /home/data
说明:a) auth users 配制一定要和/etc/rsyncd.scrt里的用户名保持一致,但不一定是系统里的用户名,名字随便取
b) 要备份的每个路径为一个module,这里有两个路径分别对应module为[appbackup]和[databackup]
3. 新建密码文件/etc/rsyncd.scrt 对应内容如下:
backup:configurebackup@#$^&*()google user:passwordpassword这里有两个用户backup和user,冒号对应为密码,注意该文件属性为600 (其他用户没用读写执行权限)
4. 开启备份服务(开户后可用netstat -tupln查看873端口有没有打开,若打开则成功)
sudo /etc/init.d/rsync start
客户端配制
1. 假如当前工作目录为~/backup
2. 在当前工作目录创建密码文件rsyncd.scrt,内容和服务器端保持一样,属性也为600
3. 在当前工作目录创建配制文件client.conf,内容如下:
BACKUPPATH="/home/aborn/backup/"; SERVERIP="10.141.247.12" MODULE="appbackup databackup" #OPTIONS="-vazu --progress --delete" OPTIONS="-vazu --progress"
说明: 其中BACKUPPATH为客户端数据存放路径
SERVERIP为服务器A机器的IP地址
MODULE为对应于服务端的/etc/rsyncd.conf下的module,多个module以空格分开
4. 运行备份脚本 rsyncclient.sh,其内容如下
#!/bin/bash ################################################################## # NAME # rsyncclient.sh ---- running in client machine, which # is used to backup data in client machine # # USAGE # ./rsyncclient.sh # # AUTHOR # Aborn Jiang (aborn.jiang@gmail.com) # # NOTE # pls configure the file client.conf and rsyncd.scrt # ################################################################## ABSPATH=$(dirname $0) source ${ABSPATH}/client.conf function get-user-pwd() { # obtain usrname and password iUSR=$(cat ${ABSPATH}/rsyncd.scrt|tr -d ‘ ‘ |grep -v "^$" | grep -v "^#"|head -n 1|awk -F : ‘{print $1}‘) iPWD=$(cat ${ABSPATH}/rsyncd.scrt|tr -d ‘ ‘ |grep -v "^$" | grep -v "^#"|head -n 1|awk -F : ‘{print $2}‘) if [ -z ${iUSR} ] || [ -z ${iPWD} ];then echo "iUSR=$iUSR iPWD=$iPWD" echo "rsyncd.scrt format illegal, please check!"; exit -1; fi # produce password file echo "$iPWD" > ${ABSPATH}/.pass chmod 600 ${ABSPATH}/.pass [ ! -d $BACKUPPATH ] && mkdir -p ${BACKUPPATH} } function backup-module() { # print key information iModule=$1 echo echo "---------------------------------------------------" echo "---- backup module ${iModule}@${SERVERIP} begin " echo "---- TIME=`date`----" echo "ABSPATH=${ABSPATH}" echo "BACKUPPATH=${BACKUPPATH}" echo "iUSR=$iUSR iPWD=$iPWD" echo "OPTIONS=${OPTIONS}" iModuleBackpath=${BACKUPPATH}/${iModule}; [ ! -d ${iModuleBackpath} ] && mkdir -p ${iModuleBackpath} # begin backup rsync ${OPTIONS} ${iUSR}@${SERVERIP}::${iModule} ${iModuleBackpath} --password-file=${ABSPATH}/.pass if [ $? != 0 ];then echo "---- backup module ${iModule}@${SERVERIP} failed." else echo "---- backup module ${iModule}@${SERVERIP} succuess. " fi echo "---- TIME=`date`----" echo "---------------------------------------------------" echo } function __main__() { get-user-pwd for md in $MODULE do backup-module $md done } __main__
注:整个工程文件见我的GitHub这个链接configure.rsync