备份服务器: 192.168.1.254
需要备份的目录:/var/htdocs/www 
备份时验证用户名和密码: backup/bshr(/etc/rsyncd.bshr)

备份客户机(备份数据存放地):1923.168.1.253
客户机和服务器均需安装 rsync 软件包

一、Server端配置:
 (1)  vim /etc/rsyncd.conf
motd file=/var/rsync/welcome.msg
pid file = /var/rsync/rsyncd.pid
lock file = /var/rsync/rsync.lock
log file = /var/rsync/rsyncd.log
##(上面4个文件需手动建)

###需要备份的模块
[www]
comment = our data for service
path = /var/htdocs/www
use chroot = no
max connections = 4
read only = true
list = false
uid = root
gid = root
secrets file = /etc/rsyncd.bshr
hosts allow = 192.168.1.253
hosts deny = 0.0.0.0/0
ignore errors = yes
transfer logging = yes
log format = "%a %f %l"
auth users = backup

##如需备份多个目录,请按上面添加

保存退出

PS:上面定义了1个备份点,只允许 192.168.1.253 连接,需要验证的用户名为 backup

(2)创建密码文件
 echo "backup:bshr" > /etc/rsyncd.bshr
 chmod 600 /etc/rsyncd.bshr 给予600权限。

(3)创建日志文件目录
 目录mkdir /var/log/rsync

(4)启动服务端rsync
 rsync --daemon  (如需关闭rsync,执行killall -9 rsync)
至此服务端配置完成。。。


二、Client配置
(1)echo "bshr" > /etc/rsyncd.bshr 注意,这里不同于server端,这里只写密码即可
 chmod 600 /etc/rsyncd.bshr 给予600权限。

(2)客户端基本不用配置什么文件。一个脚本就OK了。


#!/bin/bash

unset verb progress
for i in $*; do
    [ "$i" = "verb" ] && verb=v
    [ "$i" = "progress" ] && progress="--progress"
done

RSYNC_SERVER=192.168.1.254  #rsync server IP
AUTH_USER=backup
BAKROOT=/var/htdocs
[ ! -e $BAKROOT ] && mkdir -p $BAKROOT
PASSWD_FILE=/etc/rsyncd.bshr

PASSWD_PERM=`ls -l $PASSWD_FILE|awk '{print $1}'`

if [ "$PASSWD_PERM" != "-rw-------" ]; then
    echo -e "\nWARNING: permission of passwd file changed to 0600 \n";
    chmod 0600 $PASSWD_FILE
fi

LOGFILE=/var/log/rsync/rsyncbak.log

EXCLUDES=/var/log/rsync/exclude_file

[ ! -e $EXCLUDES ] && touch $EXCLUDES

BACKUP_MODULES="www" #如需备份多个目录,在“”内添加server端的模块名

OPTIONS="--force --ignore-errors --delete --delete-excluded \
    --exclude-from=$EXCLUDES $progress \
    --password-file=$PASSWD_FILE --backup"

rm -f /var/log/rsync/log.*

log_id=0
for bakdir in $BACKUP_MODULES; do
    log_id=`expr $log_id + 1`
    SUBOPTS="--backup-dir=$INC_DIR/$bakdir/$BAKTIME -az${verb}"
    rsync $OPTIONS $SUBOPTS $AUTH_USER@$RSYNC_SERVER::$bakdir \
        $BAKROOT/$bakdir | tee /var/log/rsync/log.$log_id
done

### 合并临时日志到备份日志中
cat /var/log/rsync/log.* >> $LOGFILE
rm -f /var/log/rsync/log.*


##### end of rsync.sh

PS:如要显示执行过程sh rsync.sh verb progress