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] </opt/mail_body_flag_$(date +%F).txt
fi
5.2.2 定時任務
####
00 01 * * * /bin/sh /server/scripts/check_md5.sh
到這裏基本的rsync同步服務器就就配置成功了。當然可能存在一定安全問題,先跑通,再變通。歡迎大佬指正。
Rsync同步服務器配置詳解