#!/bin/sh
# 数据库名数组
if [ $# -lt 1 ]; then
echo please provide database name
exit 1
fi
# 备份目录
backupdir=~/works/db
username=root
pwd=root
timestamp=`date +%Y%m%d_%H%M%S_%Z`
today=`date +%Y%m%d`
if [ ! -d $today ]; then
mkdir $backupdir/daily/$today
fi
find $backupdir/daily/ -name "*.gz" -mtime +7 -exec rm {} \;
for dbname in $@ ; do
mysqldump -u $username -p$pwd $dbname | gzip > $backupdir/daily/$today/$dbname-$timestamp.sql.gz
done
授予权限
sudo chmod +x backup.sh
增加定时任务
#每日2点执行数据库备份
0 2 * * * ~/works/backupdb.sh databaseName