一、項目需求說明
某公司有多台服務器,里面的數據很重要,如果磁盤壞了,數據就會丟失,所以公司要求把重要服務器數據備份以便出現問題時可以進行恢復,要求:每天晚上00點整在所有服務器上打包備份系統配置文件、網站程序目錄及訪問日志並通過rsync命令推送備份服務器backup上備份保留(備份思路可以是先在本地按日期打包,然后再推到備份服務器backup上)
二、具體需求規划
所有服務器的備份目錄必須一致 /backup/
要備份的系統配置文件包括但不限於:
一、定時任務服務器的配置文件(/var/spool/cron/root 適合web服務器和nfs服務器)
二、開機自啟動服務的配置文件(/etc/rc.local 適合web和nfs服務器)
三、日常腳本目錄(/server/scripts)
四、防火牆iptables的配置文件 (/etc/init.d/iptables)
1、web服務器站點目錄例如(/var/html/www)
2、web服務器訪問日志路徑例如(/app/logs)
3、web服務器保留打包后的7天的備份數據即可(因為本地服務器的磁盤會滿)
4、備份服務器上,保留近180天的備份數據,6個月前的數據清除 每周一的所有數據進行保留
5、備份服務器上,要按照備份服務器的內網IP為目錄保存備份,備份的文件按照時間名字保存
6、需要保存的數據盡量完整正確,在備份服務器上對備份的數據進行檢查,把本分成功及失敗的結果信息發送到系統管理員郵箱中
三、服務器信息
服務器說明 |
外網IP |
內網IP |
服務器主機名 |
Nginx web服務器 |
10.0.0.8/24 |
172.16.1.8/24 |
web01 |
NFS存儲服務器 |
10.0.0.31/24 |
172.16.1.31/24 |
nfs01 |
rsync備份服務器 |
10.0.0.41/24 |
172.16.1.41/24 |
backup |
四、項目實戰部署—搭建rsync服務端(backup)
1、rsync主配置文件
cat >/etc/rsyncd.conf<<EOF #rsync server# #created by yanxinjiang 2017-8-15 ##rsyncd.conf start## uid = rsync gid = rsync use chroot = no max connections = 200 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log ignore errors read only = false list = false hosts allow = 172.16.1.0/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password [backup] path = /backup EOF
2、創建統一備份數據的目錄,添加備份目錄管理用戶
useradd -s /sbin/nologin -M rsync mkdir /backup -p chown -R rsync.rsync /backup/
3、創建用戶認證文件
echo "rsync_backup:123456" >/etc/rsync.password chmod 600 /etc/rsync.password
4、啟動rsync服務並設置開機自啟動
rsync --daemon lsof -i:873 echo "rsync --daemon" >>/etc/rc.local
5、rsync客戶端創建用戶認證文件
echo "123456" >/etc/rsync.password chmod 600 /etc/rsync.password
6、客戶端驗證rsync服務推送功能
①nfs01服務器驗證 [root@nfs01 backup]#rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password sending incremental file list hosts ②web01服務器驗證 [root@web01 ~]#rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password sending incremental file list hosts
7、rsync服務腳本一鍵部署
#!/bin/sh . /etc/init.d/functions #created by yanxinjiang 2017-12-15 BakPath=/backup Passwd=123456 IP=172.16.1.0/24 Port=`netstat -lntup|grep 873|wc -l` Create_file(){ cat >/etc/rsyncd.conf<<EOF uid = rsync gid = rsync use chroot = no max connections = 200 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log ignore errors read only = false list = false hosts allow = $IP hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password [backup] path = $BakPath EOF if [ ! -f /etc/rsyncd.conf ] then action "rsync配置" /bin/false exit 1 elif [ ! -f /usr/bin/rsync ] then action "rsync命令" /bin/false exit 1 fi } Create_user(){ id rsync &>/dev/null if [ $? -ne 0 ];then useradd rsync -s /sbin/nologin -M elif [ ! -d $BakPath ];then mkdir -p $BakPath chown -R rsync.rsync $BakPath echo "rsync_backup:$Passwd" >/etc/rsync.password /bin/chmod 600 /etc/rsync.password fi } Start_rsync(){ if [ $Port -lt 2 ];then rsync --daemon action "Starting rsync..." /bin/true else action "Rsync is running..." /bin/true fi } main(){ Create_file Create_user Start_rsync } main
五、rsync客戶端編寫備份腳本(web01)
#!/bin/sh . /etc/init.d/functions Backup_Dir=/backup Passwd=123456 Passwd_File=/etc/rsync.password IP=`hostname -I|awk '{print $2}'` Remote_IP=172.16.1.41 #no.1 create backup dir Create_dir(){ [ ! -d ${Backup_Dir}/$IP ] && mkdir -p ${Backup_Dir}/$IP echo "$Passwd" >$Passwd_File && chmod 600 $Passwd_File } #no.2 compress system data to backup_dir Backup_File(){ cd / &&\ tar zchf ${Backup_Dir}/$IP/sysconfig_$(date +%F).tar.gz var/spool/cron/root etc/rc.local server/scripts etc/sysconfig/iptables &&\ tar zchf ${Backup_Dir}/$IP/html_$(date +%F).tar.gz application/nginx/html/ &&\ tar zchf ${Backup_Dir}/$IP/conf_$(date +%F).tar.gz application/nginx/conf/ } #no.3 push finger info data to remote backup Push_date(){ find ${Backup_Dir}/$IP/ -type f -name "*.tar.gz"|xargs md5sum >${Backup_Dir}/$IP/zhiwen_$(date +%F).txt rsync -az ${Backup_Dir}/$IP "rsync_backup"@${Remote_IP}::backup --password-file=${Passwd_File} if [ $? -eq 0 ];then action "backup" /bin/true else action "backup" /bin/false fi find ${Backup_Dir}/$IP -type f -name "*.tar.gz" -mtime +5|xargs rm -f if [ $? -eq 0 ];then action "rm" /bin/true else action "rm" /bin/false fi } main(){ Create_dir Backup_File Push_date } main
六、rsync客戶端編寫備份腳本(nfs01)
#!/bin/sh . /etc/init.d/functions Backup_Dir=/backup Passwd=123456 Passwd_File=/etc/rsync.password IP=`hostname -I|awk '{print $2}'` Remote_IP=172.16.1.41 Create_dir(){ [ ! -d ${Backup_Dir}/$IP ] && mkdir -p ${Backup_Dir}/$IP echo "$Passwd" >$Passwd_File && chmod 600 $Passwd_File } Backup_File(){ cd / &&\ tar zchf ${Backup_Dir}/$IP/sysconfig_$(date +%F).tar.gz var/spool/cron/root etc/rc.local server/scripts etc/sysconfig/iptables } Push_date(){ find ${Backup_Dir}/$IP/ -type f -name "*.tar.gz"|xargs md5sum >${Backup_Dir}/$IP/zhiwen_$(date +%F).txt rsync -az ${Backup_Dir}/$IP "rsync_backup"@${Remote_IP}::backup --password-file=${Passwd_File} if [ $? -eq 0 ];then action "backup" /bin/true else action "backup" /bin/false fi find ${Backup_Dir}/$IP -type f -name "*.tar.gz" -mtime +5|xargs rm -f if [ $? -eq 0 ];then action "rm" /bin/true else action "rm" /bin/false fi } main(){ Create_dir Backup_File Push_date } main
七、rsync服務端編寫檢驗腳本(backup)
#!/bin/sh . /etc/init.d/functions BakPath=/backup Check_backup(){ if [ ! -d $BakPath ] then exit else find $BakPath -type f -name "zhiwen*.txt"|xargs md5sum -c >$BakPath/check_info.txt mail -s "check_data mail" 774181401@qq.com <$BakPath/check_info.txt &>/dev/null find $BakPath -type f -name "*.tar.gz" -mtime +7 ! -name "*1.tar.gz" |xargs rm -f fi if [ $? -eq 0 ];then action "check" /bin/true else action "check" /bin/false fi } Check_backup
八、編寫全網備份定時任務
1.nfs01服務器定時任務編寫
#nfs01 backup data info-cron 00 00 * * * /bin/sh /server/scripts/nfs_backup.sh &>dev/null
2.web01服務器定時任務編寫
#web01 backup data info-cron 00 00 * * * /bin/sh /server/scripts/web_backup.sh &>dev/null
3.backup服務器定時任務編寫
# backup: backup data info cron 00 05 * * * /bin/sh /server/scripts/backup_server.sh &>/dev/null