shell实践百例计划(1)---备份MYSQL并发邮件通知

 给自己一个目标,分析100个收集的SHELL有用实例进行学习 
shell实践百例计划(1)---备份MYSQL并发邮件通知

#!/bin/bash
# 用途 :mysql数据库备份脚本。
# 表述:备份指定文件到用户主目录,使用tar命令压缩并发送邮件
# 用法: ./mybackup
 
FILE=~/.mybackup
NOW=`date +"%d-%m-%Y"`
OUT="`echo $USER.$HOSTNAME`.$NOW.tar.gz"
TAR=`which tar`
 
# mail setup
MTO="[email protected]"
MSUB="Backup (`echo $USER @ $HOSTNAME`) as on `date`"
MES=~/tmp/mybackup.txt
MATT=~/tmp/$OUT
 
# make sure we put backup in our own tmp and not in /tmp
[ ! -d ~/tmp ] && mkdir ~/tmp || :
if [ -f $FILE ]; then
    IN="`cat $FILE | grep -E -v "^#"`"
else
    echo "File $FILE does not exists"
    exit 3
fi
 
if [ "$IN" == "" ]; then
    echo "$FILE is empty, please add list of files/directories to backup"
    echo "Use mybackupadd script"
     exit 2
fi
 
$TAR -zcf ~/tmp/$OUT $IN >/dev/null
# create message for mail
echo "Backup successfully done. Please see attached file." > $MES
echo "" >> $MES
echo "Backup file: $OUT" >> $MES
echo "" >> $MES
 
# bug fix, we can't send email with attachment if mutt is not installed
which mutt > /dev/null
if [ $? -eq 0 ]; then
    # now mail backup file with this attachment
    mutt -s "$MSUB" -a "$MATT" $MTO < $MES
else
    echo "Command mutt not found, cannot send an email with attachment"
fi
 
# clean up
/bin/rm -f $MATT
/bin/rm -f $MES

你可能感兴趣的:(数据库,mysql,shell,邮件通知,备份MySQL)