1

Is there a way to change what the database is named when using the wp db export command?

When I type wp db export on one of my WordPress sites, the database gets named {dbname}-{Y-m-d}-{random-hash}.sql, which is correct according to WP-CLI Commands. When I run the same command on one of my other sites, it names the db dbname.sql (which I prefer).

Is there a way I can have wp db export name the exported db dbname.sql?

I know I can type wp db export dbname.sql but I just wanted to see if the default when typing wp db export could be modified.

Adam Gonzales
  • 829
  • 7
  • 11

1 Answers1

1

You are probably using different versions of WP-CLI on both machines. Up until version 1.2.0 the default was just {dbname}.sql. Since version 1.2.1 it defaults to {dbname}-{Y-m-d}-{random-hash}.sql.

Verify the WP-CLI versions with

wp --version

There is no way to override the default itself other than you just did by passing it a filename. But there still is a way to get the database name dynamically and then name the file after it by using the --porcelain option to only get the filename and then chain two wp db export commands together like that:

# Exports the database named '{dbname}.sql'.
wp db export $(wp db export --porcelain | cut -f1 -d "-").sql

cut -f1 -d "-" cuts the porcelain output with the first -. Since databases can't have dashes in their name, this seems a pretty save method to be used on your system with the newer WP-CLI version.


To have one command to be used on both your machines, you may need to find a way to check for the existence of - characters first, and cut only if a - exists. Or install a WP-CLI lower than version 1.2.1 on both machines.

leymannx
  • 5,138
  • 5
  • 45
  • 48