#!/bin/bash ###说明 #本脚本使用tar压缩方式,备份1个或多个指定路径的所有文件,存放到指定的路径 #然后,补充备份主机配置信息,并删除10天前的旧备份 #每次备份生成3个文件,即:应用备份、主机配置、备份日志 # IP地址+主机名+时间.tar.gz # IP地址+主机名+时间.hostconf # IP地址+主机名+时间.log #使用方法: backup_app.sh bak_path app_path1 [... app_pathn] #第1个参数:备份存放路径 #第2-n个参数:应用全路径【每个参数以反斜杠开头,最后不要跟反斜杠】 #将脚本加上x属性: chmod +x backup_app.sh #配置crontab定时任务示例: 15 01 * * * /root/backup_app.sh /root /root/app_path1 /root/app_path2 /root/app_path3 >/dev/null 2>&1 & #建议此处载入本机备份用户的环境变量 . /etc/profile . ~/.bash_profile #备份文件存放路径 bak_path=$1 if ! test -d ${bak_path} then mkdir -p ${bak_path} fi #要备份的应用全路径,以空格分隔 app_paths=echo $@|awk -F" " '{for(i=2;i<=NF;i++)name[i-1]=$i} {for(i=1;i<NF;i++)print name[i]}'
#备份保留时间,单位:天 days='10' localip=/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"|head -1
hostname=hostname
baktime=date "+%Y-%m-%d_%H.%M.%S"
bakname=${localip}.${hostname}.${baktime} #备份所选路径 echo "开始备份:"date "+%Y-%m-%d_%H.%M.%S"
>> $bak_path/${bakname}.log cd $bak_path tar zcvf ${bakname}.tar.gz --exclude=*.tar --exclude=*.out --exclude=*.log --exclude=*.zip --exclude=*.csv --exclude=*.gz --exclude=*.dat $app_paths #同时备份主机配置信息:hosts文件、文件系统挂载配置、IP配置、环境变量配置;用户crontab任务、磁盘使用情况、文件系统mount现状 echo -e "======= etc_profile =======\n" > ${bakname}.hostconf cat /etc/profile >> ${bakname}.hostconf echo -e "\n======= etc_hosts =======\n" >> ${bakname}.hostconf cat /etc/hosts >> ${bakname}.hostconf echo -e "\n======= etc_fstab =======\n" >> ${bakname}.hostconf cat /etc/fstab >> ${bakname}.hostconf echo -e "\n======= ifconfig =======\n" >> ${bakname}.hostconf ifconfig >> ${bakname}.hostconf echo -e "\n======= bash_profile =======\n" >> ${bakname}.hostconf cat ~/.bash_profile >> ${bakname}.hostconf echo -e "\n======= crontab =======\n" >> ${bakname}.hostconf crontab -l >> ${bakname}.hostconf echo -e "\n======= df -h =======\n" >> ${bakname}.hostconf df -h >> ${bakname}.hostconf echo -e "\n======= mount =======\n" >> ${bakname}.hostconf mount >> ${bakname}.hostconf #删除X天之前的备份 find ${bak_path} -name "${localip}.${hostname}.\*" -mtime +${days} -exec rm -f "{} \;" echo "本次备份得到的备份文件名:" >> $bak_path/${bakname}.log ls -l ${bakname}.tar.gz ${bakname}.hostconf >> $bak_path/${bakname}.log echo "完成备份:"date "+%Y-%m-%d-%H:%M:%S"
>> $bak_path/${bakname}.log
文章评论