关于Rsync的作用其他应用命令这里不做具体阐述,这里只对rsync同步服务器的配置过程给出详细过程
1 Rsync部署环境准备
1.1 服务器准备
服务器系统 |
角色 |
Ip |
Centos6.6x86_64 |
Backup服务器 |
1.2 检查环境
[root@nfs-server ~]# cat /etc/redhat-release
CentOS release 6.6 (Final)
[root@nfs-server nfs]# uname -r
2.6.32-504.el6.x86_64
[root@nfs-server nfs]# uname -i
x86_64
1.3 修改服务器名称
root@root ~]# hostname backup
[root@root ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=backup
[root@root ~]# cat /etc/hosts
192.168.196.136 backup
2 Backup 服务端端配置
2.1 Rsync软件列表
Centos6默认装3版本
2.2 检查软件是否安装
[root@backup ~]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64
2.3 安装软件
[root@backup ~]# yum install -y rsync
2.4 配置rsync(尽可能的复制目录等信息)
2.4.1 配置文件
Rsync默认配置文件是不存在的所以需要自己建立配置。
2.4.1.1 创建文件
[root@backup ~]# touch /etc/rsyncd.conf
2.4.1.2 编辑配置文件
[root@backup ~]# cat /etc/rsyncd.conf
#Rsync server
#created by xiao_k
##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/rync.lock
log file = /var/log/rsync.log
ignore errors
read only = false
list = false
hosts allow = 192.168.196.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
###################################
[backup]
comment = www by xiao_k
path = /backup
2.5 启动服务
Rsync默认没有启动脚本(自己写)
[root@backup ~]# rsync –daemon
2.6 检查是否启动
[root@backup ~]# ps -ef |grep rsync|grep -v grep
root 3474 1 0 06:27 ? 00:00:00 rsync --daemon
2.7 检查端口
[root@backup ~]# netstat -lntup|grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 3474/rsync
tcp 0 0 :::873 :::* LISTEN 3474/rsync
2.8 添加用户配置用户
2.8.1 添加
[root@backup ~]# useradd rsync -s /sbin/nologin -M
2.8.2 检查
[root@backup ~]# id rsync
uid=505(rsync) gid=505(rsync) groups=505(rsync)
2.9 创建以及配置备份服务器存放目录
2.9.1 创建目录
[root@backup ~]# mkdir /backup
2.9.2 修改权限
[root@backup ~]# chown -R rsync /backup/
2.9.3 检查
[root@backup ~]# ls -ld /backup
drwxr-xr-x. 2 rsync root 4096 Aug 7 06:43 /backup
2.10 配置密码文件
配置文件默认不存在,自己需要创建
2.10.1 创建文件
[root@backup ~]# echo "rsync_backup:xiao_k">/etc/rsync.password
2.10.2 检查配置结果
[root@backup ~]# cat /etc/rsync.password
rsync_backup:xiao_k
用户:密码 用户就是配置文件中指定的用户。
2.10.3 检查并修改密码文件权限
2.10.3.1 检查文件权限
[root@backup ~]# ll /etc/rsync.password
-rw-r--r--. 1 root root 20 Aug 7 06:50 /etc/rsync.password
2.10.3.2 修改权限
[root@backup ~]# chmod 600 /etc/rsync.password
2.10.3.3 检查
[root@backup ~]# ll /etc/rsync.password
-rw-------. 1 root root 20 Aug 7 06:50 /etc/rsync.password
2.11 启动服务
2.11.1 启动服务
[root@backup ~]# rsync –daemon
2.11.2 检查启动结果
[root@backup ~]# ps -ef|grep rsync|grep -v grep
root 3474 1 0 06:27 ? 00:00:00 rsync --daemon
2.11.3 添加到开机自启动
[root@backup ~]# cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
rsync --daemon
3 客户端配置
3.1 创建客户端密码文件
[root@lnmp01 ~]# echo "xiao_k" /etc/rsync.password
3.2 检查
[root@lnmp01 ~]# cat /etc/rsync.password
xiao_k
3.3 修改文件权限
[root@lnmp01 ~]# chmod 600 /etc/rsync.password
3.4 检查
配置完成
4 定时备份推送。
4.1 命令行测试送
4.1.1 本地打包:
cd / && tar -acvf /backup/config_$(date +%F-%H:%M).tar.gz /var/spool/cron/root /etc/rc.local /etc/sysconfig/iptables /server/scripts
cd / && tar zcvf /backup/www_$(date +%F-%H:%M).tar.gz /var/html/www
cd / tar zcvf /backup/logs_$(date +%F-%H:%M).tar.gz app/logs/
4.1.2 向远端服务器推送
[root@lnmp01 ~]# rsync -avz /backup/ [email protected]::backup --password-file=/etc/rsync.password
4.1.3 删除本地过期备份
[root@lnmp01 /]# find /backup -type f -name "*.tar.gz" -mtime +180|xargs rm -f
4.1.4 编写脚本
4.1.4.1 备份脚本
#!/bin/sh
IP=$(ifconfig eth0|awk -F '[ :]+' 'NR==2 {print $4}')
Path="/backup/$IP"
[ ! -d $Path ] && mkdir $Path -p
#backup
tar -acf $Path/config_$(date +%F-%H:%M).tar.gz /var/spool/cron/root /etc/rc.local /etc/sysconfig/iptables /serve
r/scripts
tar -zcf $Path/www_$(date +%F-%H:%M).tar.gz /var/html/www
tar -zcf $Path/logs_$(date +%F-%H:%M).tar.gz app/logs/
#to back server
rsync -az /backup/ [email protected]::backup --password-file=/etc/rsync.password
#delete
find /backup -type f -name "*.tar.gz" -mtime +7|xargs rm -f
4.1.5 删除
[root@lnmp01 /]# cat /server/scripts/del_back.sh
#########################################################################
# File Name: del_back.sh
# Author: xiao_k
# mail: [email protected]
# Created Time:Sun 05 Aug 2018 09:48:42 AM CST
#########################################################################
#!/bin/bash
/bin/find /backup -type f -name "*.tar.gz" -mtime +180|xargs rm -f
4.1.6 编写定时任务
[root@lnmp01 /]# crontab -l
##########################################################00 00 * * * /bin/sh /server/scripts/backup.sh &>/dev/null
##########################################################
00 01 * * * /bin/sh /server/scripts/del_back.sh &>/dev>null
5 检查数据完整性脚本及定时备份检查
5.1 客户端
[root@lnmp02 backup]# cat /server/scripts/backup.sh
#########################################################################
# File Name: backup.sh
# Author: xiao_k
# mail: [email protected]
# Created Time:Sun 05 Aug 2018 01:43:54 PM CST
#########################################################################
#!/bin/bash
IP=$(ifconfig eth0|awk -F '[ :]+' 'NR==2 {print $4}')
Path="/backup/$IP"
[ ! -d $Path ] && mkdir $Path -p
#backup
tar -acf $Path/config_$(date +%F).tar.gz /var/spool/cron/root /etc/rc.local /etc/sysconfig/iptables /server/scripts &&\
tar -zcf $Path/www_$(date +%F).tar.gz /var/html/www &&\
tar -zcf $Path/logs_$(date +%F).tar.gz /app/logs/ && \
find /backup/ -type f -name "*.tar.gz"|xargs md5sum>>$Path/flag_$(date +%F)
#to back server
rsync -az /backup/ [email protected]::backup --password-file=/etc/rsync.password
#delete
find /backup -type f -name "*.tar.gz" -mtime +7|xargs rm -f
5.2 服务器端
5.2.1 编写脚本检查
[root@backup opt]# cat /server/scripts/check_md5.sh
#########################################################################
# File Name: check_md5.sh
# Author: xiao_k
# mail: [email protected]
# Created Time:Tue 07 Aug 2018 09:40:26 PM CST
#########################################################################
#!/bin/bash
find /backup -type f -name "flag_$(date +%F)"|xargs md5sum -c |grep FAILED >/opt/mail_body_flag_$(date +%F).txt
if [ -f /opt/mail_body_flag_$(date +%F).txt -o -s /opt/mail_body_flag_$(date +%F).txt ]
then
echo "ok">>/opt/ok_mail_body_flag_$(date +%F).txt
fi
if [ ! -f /opt/mail_body_flag_$(date +%F).txt -o -s /opt/mail_body_flag_$(date +%F).txt ]
then
mail -s "$(date +%U%T) back" [email protected]
fi
5.2.2 定时任务
####
00 01 * * * /bin/sh /server/scripts/check_md5.sh
到这里基本的rsync同步服务器就就配置成功了。当然可能存在一定安全问题,先跑通,再变通。欢迎大佬指正。