晓夏

YoungCheung

Zhang Sir's technical way

全网备份脚本之web备份

浏览量:930

将服务器的重要配置文件、网页、日志进行打包备份到rsync备份服务器,每天晚上4点定时执行。

一、脚本

#!/bin/bash
#every day 04:00 tar==>backup
#author:zhangyang        
#E-mail:s.c.young@hotmail.com
#
. /etc/init.d/functions
Ip=$(ifconfig eth1|sed -n 2p|cut -c 21-30)
if [ -d /backup/$Ip ];then
	exit 1
else
	mkdir /backup/$Ip -p
fi
conf_list=(
var/spool/cron/root
etc/rc.local
server/scripts/
)
html_list=(`ls /usr/local/nginx/html/`)
log_list=(`ls /app/logs`)
function backup_conf(){
for ((i=0; i<`echo ${#html_list[*]}`; i++))
	do
	    /bin/tar -zcf /backup/$Ip/config_$Date_${html_list[$i]}.tar.gz >/dev/null 2>&1
	if [ $? -eq 0 ]
		then
			action "${conf_list[$i]}" /bin/true
	else
			action "${conf_list[$i]}" /bin/false
	fi
done
}
function backup_log()
{
for ((i=0; i<`echo ${#log_list[*]}`; i++))
	do
		/bin/tar -zcf /backup/$Ip/config_$Date_${log_list[$i]}.tar.gz >/dev/null 2>&1
	if [ $? -eq 0 ];then
		action "${log_list[$i]}" /bin/true
	else
		action "${log_list[$i]}" /bin/false
	fi
done
}
function backup_html()
{
for ((i=0; i<`echo ${#html_list[*]}`; i++))
	do
	/bin/tar -zcf /backup/$Ip/config_$Date_${html_list[$i]}.tar.gz >/dev/null 2>&1
	if [ $? -eq 0 ];then
		action "${html_list[$i]}" /bin/true
	else
		action "${html_list[$i]}" /bin/false
fi
done
}
function backup_rsync(){
ping -c 1 172.16.1.41 &> /dev/null
if [ $? -eq 0 ]
	then
		/usr/bin/rsync -avz /backup/$Ip rsync_backup@172.16.1.41::backup/ --password-file=/etc/rsync.password  >/dev/null
else 
	echo "rsync BAD!!!  `date +%F-%T`" | mail -s "rsync BAD!!!" s.c.young@hotmail.com
    exit
fi
}
main(){
	backup_conf
	backup_html
	backuu_log
	backup_rsync
}
main

二、定时任务

#backup MySQL tables for databases  by zhangyang at 20170420
00 4 * * * /bin/bash /server/script/nginx_rsync.sh >/dev/null 2>&1


神回复

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。