Can I get a dump of all my databases *except one* using mysqldump?

坚强是说给别人听的谎言 提交于 2019-12-02 20:21:26

mysql ... -N -e "show databases like '%';" |grep-v -F databaseidontwant |xargsmysqldump ... --databases > out.sql

agus nurhadi
echo 'show databases;' | mysql -uroot -proot | grep -v ^Database$ | grep -v ^information_schema$ | grep -v ^mysql$ | grep -v -F db1 | xargs mysqldump -uroot -proot  --databases > all.sql

dumps all databases except: mysql, information_schema, mysql and db1.

Or if you'd like to review the list before dumping:

  1. echo 'show databases;' | mysql -uroot -proot > databases.txt
  2. edit databases.txt and remove any you don't want to dump
  3. cat databases.txt | xargs mysqldump -uroot -proot --databases > all.sql

What about

--ignore-table=db_name.tbl_name

Do not dump the given table, which must be specified using both the database and table names. To ignore multiple tables, use this option multiple times.

Maybe you'll need to specify a few to completely ignore the big database.

I created the following one line solution avoiding multiple grep commands.

mysql -e "show databases;" | grep -Ev "Database|DatabaseToExclude1|DatabaseToExclude2" | xargs mysqldump --databases >mysql_dump_filename.sql

The -E in grep enables extended regex support which allowed to provide different matches separated by the pipe symbol "|". More options can be added to the mysqldump command. But only before the "--databases" parameter.

Little side note, i like to define the filename for the dump like this ...

... >mysql_dump_$(hostname)_$(date +%Y-%m-%d_%H-%M).sql

This will automatically ad the host name, date and time to the filename. :)

Create a backup user and only grant that user access to the databases that you want to backup.

You still need to remember to explicitly grant the privileges but that can be done in the database and doesn't require a file to be edited.

Seeing as your using Windows you should have PowerShell available to use.
Here is a short PowerShell script to get a list of all Databases, remove unwanted ones from the list & then use mysqldump to backup the others.

$MySQLPath = "."
$Hostname = "localhost"
$Username = "root"
$Password = ""

# Get list of Databases
$Databases = [System.Collections.Generic.List[String]] (
    & $MySQLPath\mysql.exe -h"$Hostname" -u"$Username" -p"$Password" -B -N -e"show databases;"
)

# Remove databases from list we don't want
[void]$Databases.Remove("information_schema")
[void]$Databases.Remove("mysql")

# Dump database to .SQL file
& $MySQLPath\mysqldump.exe -h"$HostName" -u"$Username" -p"$Password" -B $($Databases) | Out-File "DBBackup.sql"

It took me a lot of finagling to come up with this but I've used it for a few years now and it works well...

mysql -hServerName -uUserName -pPassword -e "SELECT CONCAT('\nmysqldump -hServerName -uUserName -pPassword --set-gtid-purged=OFF --max_allowed_packet=2048M --single-transaction --add-drop-database --opt --routines --databases ',DBList,' | mysql -hServerName2 -uUserName2 -pPAssword2 ' ) AS Cmd FROM (SELECT GROUP_CONCAT(schema_name SEPARATOR ' ') AS DBList FROM information_schema.SCHEMATA WHERE LEFT(schema_name, 8) <> 'cclegacy' AND schema_name NOT IN ('mysql','information_schema','performance_schema','test','external','othertoskip')) a \G" | cmd

Instead of the pipe over to mysql where I'm moving from serverName to Servername2 you could redirect to a file but this allows me to tailor what I move. Sometimes i even OR the list so I can say LIKE 'Prefix%' etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!