Linux 备份文件和数据库发送到邮箱

nuligannima 2011-07-13

今天一个误操作把一个月的文件备份和数据库备份删掉了

做了一个用sendmail发送到邮箱的备份脚本:

#!/bin/bash
# X ,  sendmail hoodbc20117
### Setting path ###
BACKPATH="/data0/mysql_bak"
DATE=`/bin/date "+%Y-%m-%d-%H-%M-%S"`

zip -rq $BACKPATH/erp_$DATE.zip /data/htdocs/www/erp/
echo "erp all backup" | mutt -a $BACKPATH/erp_$DATE.zip -s "erp all backup" [email protected]

find $BACKPATH -ctime +30 -name *.zip -delete

顺便把数据库备份的脚本也记录下来:

#!/bin/bash
# hoodbc20117

### database setting ###
USER='root'
PASSWORD=''
HOST="127.0.0.1"
DATABASES=(erp)

### Setting path ###
MYSQLDUMP=/usr/local/webserver/mysql/bin/mysqldump
BACKPATH="/data0/mysql_bak"

DATE=`/bin/date "+%Y-%m-%d-%H-%M-%S"`
for DATABASE in ${DATABASES[*]}
do
    FILENAME="$DATABASE-$DATE"

    $MYSQLDUMP --add-drop-table --user $USER --password=$PASSWORD $DATABASE --lock-all-tables>> "$BACKPATH/$FILENAME.sql"
done

#tar zcf -r $BACKPATH/erp_$DATE.tar.gz ~/data/htdocs/www/erp
#zip -rq $BACKPATH/erp_$DATE.zip /data/htdocs/www/erp/
zip -rq $BACKPATH/$FILENAME.zip $BACKPATH/$FILENAME.sql
echo "erp db backup" | mutt -a $BACKPATH/$FILENAME.zip -s "erp db backup" [email protected]

find $BACKPATH -ctime +30 -name *.sql -delete

记得加上--lock-all-tables的参数,这样在导出sql文件的时候会锁定表,当然也可以直接把数据库文件打包。最后加到crontab里定时运行脚本。

相关推荐