![mysql 数据库转储脚本添加功能以跳过转储的数据库](https://linux22.com/image/49537/mysql%20%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BD%AC%E5%82%A8%E8%84%9A%E6%9C%AC%E6%B7%BB%E5%8A%A0%E5%8A%9F%E8%83%BD%E4%BB%A5%E8%B7%B3%E8%BF%87%E8%BD%AC%E5%82%A8%E7%9A%84%E6%95%B0%E6%8D%AE%E5%BA%93.png)
我正在使用以下脚本来转储我的数据库。效果很好。我想添加功能,它首先检查数据库表是否已转储,如果存在转储,则应跳过它并移至下一个表。
DB_host=127.0.0.1
DB_user=root
DB=mydb
DB_pass=mydbpassword
DIR=/
[ -n "$DIR" ] || DIR=.
test -d $DIR || mkdir -p $DIR
echo "Dumping tables into separate SQL command files for database '$DB' into dir=$DIR"
tbl_count=0
for t in $(mysql -NBA -h $DB_host -u $DB_user -p$DB_pass -D $DB -e 'show tables')
do
echo "DUMPING TABLE: $t"
mysqldump -h $DB_host -u $DB_user -p$DB_pass $DB $t > $DIR/$t.sql
(( tbl_count++ ))
done
echo "$tbl_count tables dumped from database '$DB' into dir=$DIR"
答案1
更改此代码块:
echo "DUMPING TABLE: $t"
mysqldump -h $DB_host -u $DB_user -p$DB_pass $DB $t > $DIR/$t.sql
(( tbl_count++ ))
对此:
echo "DUMPING TABLE: $t"
if [ -e "$DIR/$t.sql" ]; then
echo "TABLE $DIR/$t.sql exists, skipping..."
else
mysqldump -h $DB_host -u $DB_user -p$DB_pass $DB $t > $DIR/$t.sql
fi
(( tbl_count++ ))