Linux系统关键文件备份脚本

来源:互联网 发布:美发店收银软件 编辑:程序博客网 时间:2024/05/18 08:27

原文地址:http://reeddeer.blog.163.com/blog/static/11817104020128255434163/


# 说明:备份系统配置除了通用之外,还有针对不同业务进行参数备份
# 1.备份整个/etc目录
# 2.备份crontab
# 3.备份路由表route
# 4.备份rc.local
# 5.备份iptables
# 6.备份应用配置(如apache/lvs/heartbeat)
# 7.备份系统日志
# 8.备份各个系统上的脚本
# 待补充...

# 参数说明
basedir=/usr/SysBackUpByWeekly#备份总存放处
BakDir=/usr/SysBackUpByWeekly$(date+'%Y%m%d')#备份存放处,根据日期生成
bak_etc=$BakDir/bak_etc#备份etc目录
bak_sysfile=$BakDir/bak_sysfile#备份系统配置(如crontab/route/rc.local)
bak_syslog=$BakDir/bak_syslog#备份系统日志/var/log
bak_shell=$BakDir/bak_shell#备份自定义shell脚本
bak_app=$BakDir/bak_app#备份应用配置
bak_boot=$BakDir/bak_boot#备份引导配置

# 建备份目录
func_MkdirS()
{
[!-d"$basedir"] && mkdir $basedir
[!-d"$BakDir"] && mkdir $BakDir
for dirsin $bak_etc $bak_sysfile $bak_syslog $bak_shell $bak_app $bak_boot;do
[!-d"$dirs"] && mkdir-p $dirs
done
}

# 备份apache配置
func_apache()
{
apache_dir=/usr/apache/conf
if[-d $apache_dir];then
mkdir -p $bak_app/httpd
cp -a $apache_dir $bak_app/httpd
fi
}

# 备份前置/企业E卡/增值平台/客户端前置应用重要目录
func_AllWebp()
{
webp_path=/home/$1
if[-d $webp_path];then#如果系统存在这些目录则建立对应的目录存放备份
mkdir -p $bak_app/$1
case $1in
'webp')# 前置程序
cp -a $webp_path/.bash_profile $bak_app/$1
cp -a $webp_path/{rec,simu,transMonitor} $bak_app/$1;;
'ser-qyyk')# 企业E卡程序
cp -a $webp_path/.bash_profile $bak_app/$1
cp -a $webp_path/{rec,src,cmmLibs,cmmSrc,setup,xmlLibs} $bak_app/$1;;
'ga-hgwang')# 增值平台
cp -a $webp_path/.bash_profile $bak_app/$1
cp -a $webp_path/{rec,src,cmmLibs,cmmSrc,setup,xmlLibs,xml} $bak_app/$1;;
'client')# 客户端前置
cp -a $webp_path/.bash_profile $bak_app/$1
cp -a $webp_path/{src,update} $bak_app/$1;;
esac
fi
}

# 备份转账通系统2个程序/Transfer和/repair
func_zhuanZT()
{
zhuanZT_path=/$1
if [ -d $zhuanZT_path ];then #如果系统存在这些目录则建立对应的目录存放备份
mkdir -p $bak_app/$1
case $1in
'Transfer')# 转账通主程序
cp -a $zhuanZT_path $bak_app/$1;;
'repair')# 转账通副程序
cp -a $zhuanZT_path $bak_app/$1;;
esac
fi
}


# 备份shell脚本
func_shell()
{
# 这个目录几乎系统都有
shell_path1=/root/script
if[-d $shell_path1];then
find $shell_path1 -name"*.sh"-exec cp \-a{} $bak_shell \;
fi
# 这个目录前置系统有
shell_path2=/root/clearingSystem
if[-d $shell_path2];then
mkdir -p $bak_shell/clearingSystem
find $shell_path2 -name"*.sh"-exec cp \-a{} $bak_shell/clearingSystem \;
fi
# 重点是汇卡数据库上的脚本
shell_path3=/usr/BACKUP
if[-d $shell_path3];then
mkdir -p $bak_shell/BACKUP
find $shell_path3 -name"*.sh"-exec cp \-a{} $bak_shell/BACKUP \;
fi
}

# 通用备份
func_CommBackUp()
{
cp -a/etc $bak_etc
cp -a/etc/crontab $bak_sysfile
cp -a/var/spool/cron $bak_sysfile
cp -a/etc/rc.d/rc.local $bak_sysfile
route -n> $bak_sysfile/route.txt
cp -a/var/log/ $bak_syslog
cp -a/etc/sysconfig/iptables $bak_sysfile
cp -a/boot $bak_boot
}

# 执行主函数
func_main()
{
func_MkdirS
func_apache
func_AllWebp webp
func_AllWebp ser-qyyk
func_AllWebp ga-hgwang
func_AllWebp client
func_zhuanZT Transfer
func_zhuanZT repair
func_shell
func_CommBackUp
cd $(dirname $BakDir)
tar -zcvf $(basename $BakDir).tar.gz $(basename $BakDir)>/dev/null
if[ $?-eq0];then
rm -rf $(basename $BakDir)
mv $(basename $BakDir).tar.gz $basedir
fi
}
func_main