我有一个简单的shell脚本,每周备份一次整个www
目录和单个数据库。它工作得很好..现在我想扩展它一点点,使它备份所有数据库,而不仅仅是一个。我怎么能这样做?
这是当前的脚本
#!/bin/bash
NOW=$(date +"%Y-%m-%d-%H%M")
FILE="backup.$NOW.tar"
BACKUP_DIR="/home/user/backups/"
WWW_DIR="/var/www/"
DB_USER="dbuser"
DB_PASS="dbpass"
DB_NAME="dbName"
DB_FILE="backup.$NOW.sql"
WWW_TRANSFORM='s,^home/user/www,'
DB_TRANSFORM='s,^home/user/backups,database,'
tar -cf $BACKUP_DIR/$FILE --transform $WWW_TRANSFORM $WWW_DIR
mysqldump -u$DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/$DB_FILE
tar --append --file=$BACKUP_DIR/$FILE --transform $DB_TRANSFORM $BACKUP_DIR/$DB_FILE
rm $BACKUP_DIR/$DB_FILE
gzip -9 $BACKUP_DIR/$FILE
echo 'backup finished', $FILE
答案 0 :(得分:1)
您可以简单地使用show databases
命令。像这样读取for循环中的所有数据库并调用mysqldump
命令
databases=`mysql --user=$DB_USER --password=$DB_PASS -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
for db in $databases; do
echo "Dumping database: $db"
mysqldump --force --opt --user=$DB_USER--password=$DB_PASS --databases $db > $BACKUP_DIR/`date +%Y%m%d`.$db.sql
done
答案 1 :(得分:1)
#!/bin/bash
USER="root"
PASSWORD="PASSWord"
OUTPUT="/root"
rm "$OUTPUT/*gz" > /dev/null 2>&1
databases=`mysql --user=$USER --password=$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
for db in $databases; do
if [[ "$db" != "information_schema" ]] && [[ "$db" != _* ]] ; then
now="$(date +'%d_%m_%Y_%H_%M_%S')"
echo "Dumping database: $db"
mysqldump --user=$USER --password=$PASSWORD --default-character-set=utf8 $db | gzip > $OUTPUT/$db.`date +%Y%m%d`.gz
fi
done