Use the --no-create-db
option of mysqldump
.
From man mysqldump
:
--no-create-db, -n
This option suppresses the CREATE DATABASE statements that are otherwise included in the output if the --databases or --all-databases option is given.
题
Here's my full bash script:
#!/bin/bash
logs="$HOME/sitedb_backups/log"
mysql_user="user"
mysql_password="pass"
mysql=/usr/bin/mysql
mysqldump=/usr/bin/mysqldump
tbackups="$HOME/sitedb_backups/today"
ybackups="$HOME/sitedb_backups/yesterday"
echo "`date`" > $logs/backups.log
rm $ybackups/* >> $logs/backups.log
mv $tbackups/* $ybackups/ >> $logs/backups.log
databases=`$mysql --user=$mysql_user -p$mysql_password -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema)"`
for db in $databases ; do
$mysqldump --force --opt --user=$mysql_user -p$mysql_password --databases $db | gzip > "$tbackups/$db.gz"
echo -e "\r\nBackup of $db successfull" >> $logs/backups.log
done
mail -s "Your DB backups is ready!" yourmail@gmail.com <<< "Today: "`date`"
DB backups of every site is ready."
exit 0
Problem is when i try to import it with mysql i am gettint error 1044 error connecting to oldname_db. When i opened sql file i have noticed on the first line CREATE
command so it tries to create that database with the old name. How can i solve that problem?
SOLVED.
Using --databases
parameter in my case is not necessary and because of --databases
it was generating CREATE and USE action in the beginning of the sql file, hope it helps somebody else.
解决方案
Use the --no-create-db
option of mysqldump
.
From man mysqldump
:
--no-create-db, -n
This option suppresses the CREATE DATABASE statements that are otherwise included in the output if the --databases or --all-databases option is given.