前辈写的一个备份脚本,一直在用,今日闲暇温故而知新一下
基本功能:定时打包网站代码文件及mysql数据库,并按周期清理历史备份。
亮点功能:可以排除网站中部分文件夹并单独对其打包(比如图片文件夹)。
配置文件如下:
#set tar www and dump db and rsync DOWWW=true DODB=true DORSYNC=true #base info WWWNAME=www.test.com SRCIP=123.123.123.123 #frequence TARSET=daily #clean frequence OLDSET=3 days ago #db setting DBNAME=testdb DBPORT=3306 DBSOCKET=/tmp/mysql.sock #tar setting TAREXTRA=--exclude=img/p/* --exclude=upload/p_img/* DEDICATEDIR=/htdocs/upload/image DEDICATETAR=upload_image
tarback.sh程序代码如下:(做了细微调整,对导出数据库进行了压缩。)
#!/bin/bash # By Chen Zhidong # http://sillydong.com # Email:njutczd+gmail.com PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin export PATH #Begin #====================================================== ROOTDIR="/home/www" LOGDIR="/home/logs" TARDIR="/home/backup/www" SQLDIR="/home/backup/mysql" BINDIR="/usr/local/mysql/bin" EXCLUDE=".tar.gz .tar.bz2 .zip" DAY_WEEK="5" DAY_MONTH="26" DB_USER="root" DB_PASS="password" DB_HOST="localhost" TAG=$(date +%Y%m%d) #====================================================== readset(){ #readset WWWNAME tarsetfile grep $1 $2 | grep -v "^#" | sed "s/$1=//" } exclude(){ #exclude directory for ex in $EXCLUDE do exc=`ls $1 | grep $ex` if [ $exc ];then echo ${1}/${exc} >> exclude.tmp >> $LOGDIR/tarback_$TAG.log fi done } clean(){ #clean name filedir frequency tarset oldset=`readset OLDSET $4` case $3 in daily) FILELIST=`ls -l $2 | awk '{print $9}' | egrep "^$1_.*"` for file in $FILELIST do old=$(date +%Y%m%d --date="$oldset") mold=$(date +%m) tim=`echo "$file" | sed "s/.*_\([0-9]*\).*/\1/"` day=`expr substr $tim 7 2` month=`expr substr $tim 5 2` if [ $tim -le $old ];then if [ "$day" = "01" -a "$month" = "$mold" ];then echo "$file was left as No.1!" >> $LOGDIR/tarback_$TAG.log else echo "$file to be removed!" >> $LOGDIR/tarback_$TAG.log rm -f $2/$file fi else echo "$file was left!" >> $LOGDIR/tarback_$TAG.log fi done ;; weekly) FILELIST=`ls -l $2 | awk '{print $9}' | grep $1` for file in $FILELIST do old=$(date +%Y%m%d --date=$oldset) tim=`echo "$file" | sed "s/.*${1}_\([0-9]*\)..*/\1/"` if [ $tim -le $old ];then echo -e "$2/$file to be removed!" >> $LOGDIR/tarback_$TAG.log rm -f $2/$file else echo "$file will be kept!" >> $LOGDIR/tarback_$TAG.log fi done ;; monthly) FILELIST=`ls -l $2 | awk '{print $9}' | grep $1` for file in $FILELIST do old=$(date +%Y%m%d --date=$oldset) tim=`echo "$file" | sed "s/.*${1}_\([0-9]*\)..*/\1/"` if [ $tim -le $old ];then echo -e "$2/$file to be removed!" >> $LOGDIR/tarback_$TAG.log rm -f $2/$file else echo "$file will be kept!" >> $LOGDIR/tarback_$TAG.log fi done ;; *) echo "***Wrong Frepquency was sent to clean()!***" >> $LOGDIR/tarback_$TAG.log ;; esac } dump(){ #dump tarsetfile ip frequency dbname if [ -n $1 ];then dbport=`readset DBPORT $1` dbsocket=`readset DBSOCKET $1` echo "Dumping ${4} ${dbport} ${dbsocket} ..." >> $LOGDIR/tarback_$TAG.log $BINDIR/mysqldump --opt $4 --user=$DB_USER --host=$DB_HOST --password=$DB_PASS -P $dbport -S $dbsocket | gzip > $SQLDIR/$2/${4}_${TAG}.sql if [ "$?" = "0" ];then echo "Dumping ${4} done!" >> $LOGDIR/tarback_$TAG.log sqldir=$SQLDIR/$2 echo "cleanning sql $4 ..." >> $LOGDIR/tarback_$TAG.log clean $4 $sqldir $3 $1 echo "done!" >> $LOGDIR/tarback_$TAG.log else echo "***Dumping ${3} failed!***" >> $LOGDIR/tarback_$TAG.log fi else echo "***$1 has no database!***" >> $LOGDIR/tarback_$TAG.log fi } tarfile(){ #tarfile tarset ip frequency $dbname srcdir=`echo "$1" | sed "s/\/tar.set$//"` destfile=$TARDIR/$2/$srcdir echo "tar $srcdir to ${destfile}_$TAG.tar.bz2" >> $LOGDIR/tarback_$TAG.log exclude $srcdir tarextra=`readset TAREXTRA $1` echo "using extra command ${tarextra}" >> $LOGDIR/tarback_$TAG.log if [ -e $srcdir -a -d $srcdir ];then if [ -e $ROOTDIR/exclude.tmp ];then tar $tarextra --exclude-from="$ROOTDIR/exclude.tmp" --ignore-failed-read -jcPf ${destfile}_$TAG.tar.bz2 $srcdir rm -f $ROOTDIR/exclude.tmp else tar $tarextra --ignore-failed-read -jcPf ${destfile}_$TAG.tar.bz2 $srcdir fi echo "done!" >> $LOGDIR/tarback_$TAG.log else echo "***$srcdir is not a existing folder!***" >> $LOGDIR/tarback_$TAG.log fi dedicatedir=(`readset DEDICATEDIR $1`) dedicatetar=(`readset DEDICATETAR $1`) if [ "$dedicatedir" -a "$dedicatetar" ];then len=${#dedicatedir[@]} i=0 while [ $i -lt $len ];do dir=${srcdir}${dedicatedir[$i]} file=${destfile}_${dedicatetar[$i]} echo "tar $dir to ${file}_$TAG.tar.bz2" >> $LOGDIR/tarback_$TAG.log if [ -e $dir -a -d $dir ];then tar --ignore-failed-read -jcPf ${file}_$TAG.tar.bz2 $dir echo "done!" >> $LOGDIR/tarback_$TAG.log else echo "***$dir is not a existing folder!***" >> $LOGDIR/tarback_$TAG.log fi let i++ done fi tardir=$TARDIR/$2 wwwname=`readset WWWNAME $1` echo "cleanning www $wwwname ..." >> $LOGDIR/tarback_$TAG.log clean $wwwname $tardir $3 $1 echo "done!" >> $LOGDIR/tarback_$TAG.log } #================================================================= if [ `pwd` != "$ROOTDIR" ];then cd $ROOTDIR fi if [ -e $LOGDIR/tarback_$(date +%Y%m%d --date="5 days ago").log ];then rm -f $LOGDIR/tarback_$(date +%Y%m%d --date="5 days ago").log fi echo -e "Tarback work begin at $(date)!" >> $LOGDIR/tarback_$TAG.log #list all *.set files in rsync directory LIST=`find ./ -maxdepth 3 -name tar.set` for setfile in $LIST do echo -e "-----------\nUsing $setfile..." >> $LOGDIR/tarback_$TAG.log ip=`readset SRCIP $setfile` frequency=`readset TARSET $setfile` if [ "`readset DODB $setfile`" == "true" ];then dbname=`readset DBNAME $setfile` dump $setfile $ip $frequency $dbname else echo -e "skip dumpdb" #>> $LOGDIR/tarback_$TAG.log fi if [ "`readset DOWWW $setfile`" == "true" ];then case $frequency in daily) #echo "doing daily tarback" tarfile $setfile $ip $frequency $dbname ;; weekly) if [ $(date +%w) == $DAY_WEEK ];then #echo "do weekly tarback" tarfile $setfile $ip $frequency $dbname else echo -e "TARSET is $frequency, $(date +%w) is not $DAY_WEEK.\n" >> $LOGDIR/tarback_$TAG.log fi ;; monthly) if [ $(date +%d) == $DAY_MONTH ];then #echo "do monthly tarback" tarfile $setfile $ip $frequency $dbname else echo -e "TARSET is $frequency, $(date +%d) is not $DAY_MONTH.\n" >> $LOGDIR/tarback_$TAG.log fi ;; *) echo -e "***Wrong frequency set $setfile***\n" >> $LOGDIR/tarback_$TAG.log ;; esac else echo -e "skip www" >> $LOGDIR/tarback_$TAG.log fi done echo -e "-----------\nTarback work finished at $(date)!" >> $LOGDIR/tarback_$TAG.log exit 0 #End
前辈博客文章地址:https://sillydong.com/mysa/myserver/server-backup-tarback.html
评论列表:
今日新闻
评论于2020-05-18 21:22:27
文章非常好超喜欢 - 回复该评论
心灵博客
评论于2020-07-27 23:24:01
看起来不错的样子 - 回复该评论
bg
评论于2020-08-09 13:32:24
京东专用快递网站 快递单号 空包代发www.5adanhao.cn - 回复该评论
头条新闻
评论于2020-10-10 01:41:31
文章不错非常喜欢 - 回复该评论
mnbmr
评论于2020-10-25 10:55:35
免费试用单号网 免费试用单号 快递单号 各大快递代发www.88danhw.com - 回复该评论
招投标
评论于2020-12-30 17:43:05
Linux站点备份打包脚本方法很好用 - 回复该评论
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
相关文章
txt字符串指定长度换行2023-05-13
转:如何让 Windows 命令行程序像 Linux & 命令那样在后台运行2023-02-24
Python3串口16进制读写通讯2017-10-13
域账号弱口令批量检查-PowerShell2016-09-22
Bat选择性拷贝文件夹里文件2016-09-13
Bat脚本转换成EXE可执行文件2016-08-29
Windows目标服务器端口监测脚本微信通知2016-08-25
Windows批量建文件夹和寻找目录下空文件夹2016-08-13
bat脚本设置开机启动并去除黑框2015-04-25
windows搜索文件内容并拷贝文件到指定目录2015-04-12