所有网站都需要备份,我就写了一份简单地网站备份计划脚本。
vim /etc/bak.sh
#!/bin/bash
# Delete expire file before 11 days.
find /home/backup_data -name ecshop* -mtime +11 -exec rm {} \;
# (1) set up all the mysqldump variables
USER=root
PASSWORD="******"
OUTPUT="/home/backup_data"
DBSERVER=127.0.0.1
databases=`mysql --user=$USER --password=$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database | grep ecshop`
# (3) use this command for a database server on localhost. add other options if need be.
for db in $databases; do
# if [[ "$db" != "information_schema" ]] && [[ "$db" != _* ]] ; then
if [ "$db" != "information_schema" ] ; then
echo "Dumping database: $db"
mysqldump --force --opt --user=$USER --password=$PASSWORD --databases $db > $OUTPUT/`date +%Y%m%d`.$db.sql
gzip $OUTPUT/`date +%Y%m%d`.$db.sql
fi
done
#mysqldump --opt --user=${USER} --password=${PASS} ${DATABASE} > ${FILE}
# (4) tar the website file
tar -zcvf /home/backup_data/`date +%Y%m%d`ecshop67.tar /var/www/html/ecshop67/
tar -zcvf /home/backup_data/`date +%Y%m%d`ecshop68.tar /var/www/html/ecshop68/
保存退出;
这样数据库及网站文件都可以备份了。并且删除11天之前的备份文件。所有文件都是压缩文件。
如下是解压缩命令:
tar zxvf 20151210ecshop68.tar
gzip -d 20151210.ecshop68.sql.gz
任务计划:(每五天备份一次)
00 2 1/5 * * sh /home/backup_script/bak.sh
阅读(1999) | 评论(0) | 转发(0) |