环境介绍:
==================================
备份服务器: 192.168.2.241
需要备份的目录:/var/www
备份时验证用户名和密码: backup/secret
备份客户机(备份数据存放地):192.168.2.241
客户机和服务器均需安装 rsync 软件包
1.安装rsync
[root@web1 ~]# yum install rsync
[root@web1 ~]# rpm -ql rsync
2创建主配置文件
# vi /etc/rsyncd.conf (在配置文件里面写)1.注意在配置文件里面写的配置信息的最后面不要有空格
motd file=/var/rsync/welcome.msg
pid file = /var/rsync/rsyncd.pid
lock file = /var/rsync/rsync.lock
log file = /var/rsync/rsyncd.log
[www]
comment = bbs
path = /var/www/ (文件备份路径)
use chroot = no
max connections = 4
read only = yes
list = false
uid = nobody
gid = nobody
secrets file = /etc/rsyncd.secrets (密码文件的路径)
hosts allow = 192.168.2.241
hosts deny = 0.0.0.0/0
ignore errors = yes
transfer logging = yes
log format = "%a %f %l"
auth users = backup
上面定义了1个备份点,只允许 192.168.2.241 连接,需要验证的用户名为 backup
然后创建密码文件,每一行格式为: 用户名:密码,都是明文的
3.编辑密码文件
# vi /etc/rsyncd.secrets
在文本里面写入
backup:123456
保持并退出,
给权限 # chmod 600 /etc/rsyncd.secrets
4. 创建日志或运行目录
# mkdir /var/rsync
然后启动服务 # rsync --daemon
检查rsync时候启动 # ps -ef | grep rsync
mkdir /var/www
记得关闭防火墙或者是开放端口
5. 客户端的配置(备份服务器的配置)
安装rsync
[root@web1 ~]# yum install rsync
[root@web1 ~]# rpm -ql rsync
创建备份根目录: mkdir /var/backup,其他的目录备份时会自动被创建
输入代码 # echo "secret" > /etc/rsyncd.conf
再给一个权限 chmod 600 /etc/rsyncd.conf
客户端基本不用配置什么文件。一个脚本就OK了。
输入编辑代码 # vi /etc/rc.d/init.d/rsync.sh
写入代码
#!/bin/bash
unset verb progress
for i in $*; do
[ "$i" = "verb" ] && verb=v
[ "$i" = "progress" ] && progress="--progress"
done
RSYNC_SERVER=192.168.2.240 #rsync server IP #(这里给的是要备份的服务的id地址)
AUTH_USER=backup
BAKROOT=/var/backup/
INC_DIR=increment
NOW=`date +%Y-%m-%d_%H:%M`
[ ! -e $BAKROOT ] && mkdir -p $BAKROOT
PASSWD_FILE=/etc/rsyncd.conf
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=$BAKROOT/$INC_DIR/$BACKUP_MODULES/$NOW -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
输入权限命令
chmod a+x /etc/rc.d/init.d/rsync.sh
输入 mkdir /var/log/rsync
再输入 vi /etc/rsyncd.conf 把文本信息改成123456
开始备份
# /etc/rc.d/init.d/rsync.sh verb progress
显示一下代码显示,备份成功
[root@localhost /]# /etc/rc.d/init.d/rsync.sh verb progress
WARNING: permission of passwd file changed to 0600
receiving incremental file list
(new) backup_dir is /var/backup//increment/www/2018-07-04_15:22
./
sent 27 bytes received 47 bytes 148.00 bytes/sec
total size is 0 speedup is 0.00
备份文件保存在/var/backup/www/里面 注=绿色字体不要写入文本里面
备份文件保存在/var/backup/www/里面
显示rsync: failed to connect to 192.168.2.245 (192.168.2.245): Connection refused (111)错误 k'ke
sudo -s rsync --daemon 启动服务
当出现
WARNING: permission of passwd file changed to 0600
receiving incremental file list
rsync: opendir "myuser/新文件夹 (2)" (in www) failed: Permission denied (13)
rsync: opendir "myuser/新文件夹 (3)" (in www) failed: Permission denied (13)
rsync: opendir "myuser/新文件夹 (4)" (in www) failed: Permission denied (13)
rsync: opendir "myuser/新文件夹" (in www) failed: Permission denied (13)
(new) backup_dir is /var/backup//increment/www/2018-07-10_17:06
这一类报错 去ftp服务器里面修改RSYNC配置文件的 uid
gid ,把后缀改成root 这样就有权限