分类: Maintaining, Programming, Database, WordPress
根据howlym的自动备份数据库文章修改了一下,下面这个可以自动备份数据库和整个网站(前提当然是你的网站支持Shell了)
- 增加了邮件内容提示(这里直接将脚本包含了,注释内容就是提示。也可以另用一个文件。);
- 增加了一个附件,是整个网站的打包;
- 针对网站数据库默认是latin1来修改了dump 的编码,恢复的时候应使用相应的编码调整;
- 修改模式为700,避免他人可以看到密码;
- 备份存档。
#!/bin/bash
#
# To restore to another unicode databaase from this backup
# Please add SET NAMES ‘utf8′; to the top of the SQL backup file.
# Tune SET CHARACTER_SET_XXX = ‘XXX’; yourself pls.
#
DATE=`date +%Y%m%d`
HOST=db.samuelchen.net
USER=user
PASS=123456
DB=database
EMAIL=”email@yourhost.com”
DB_FILE=”$DB-$DATE”
SITE_FILE=”samuelchen-$DATE.tar.gz”
SITE_PATH=samuelchen.net/
TIL=”Explore.Dream.Discover.Database-Dump-AutoBackup”
#
# To restore to another unicode databaase from this backup
# Please add SET NAMES ‘utf8′; to the top of the SQL backup file.
# Tune SET CHARACTER_SET_XXX = ‘XXX’; yourself pls.
#
DATE=`date +%Y%m%d`
HOST=db.samuelchen.net
USER=user
PASS=123456
DB=database
EMAIL=”email@yourhost.com”
DB_FILE=”$DB-$DATE”
SITE_FILE=”samuelchen-$DATE.tar.gz”
SITE_PATH=samuelchen.net/
TIL=”Explore.Dream.Discover.Database-Dump-AutoBackup”
# clean cache
rm /home/yourhome/blog/wp-content/cache/* -r >/dev/null
# dump & pack database
mysqldump –default-character-set=latin1 –set-charset=utf8 –skip-opt -u$USER -p$PASS -h $HOST $DB > $DB_FILE.sql
gzip $DB_FILE.sql -f
# pack whole site
tar zvcf $SITE_FILE $SITE_PATH >/dev/null
# send email
mv $DB_FILE.sql.gz $DB_FILE.gz | mutt $EMAIL -s $TIL -a $DB_FILE.gz -a $SITE_FILE -i backup.sh
# clean & keep archives
mv $DB_FILE.gz /home/yourhome/backup/
mv $SITE_FILE /home/yourhome/backup/
存成backup.sh后,运行
$chmod 700 autobackup
$crontab -e
在出现的界面中输入:
0 0 * * * /home/yourhome/backup.sh
路径请修改成你的文件路径
(如果是nano)按ctrl+o 保存,请注意按回车确认,ctrl+x退出.
这样每天晚上12点,你就可以在你的邮箱中收到系统打包好的数据库文件了。