Introducing how to export and backup WordPress databases from the command line via SSH connection using the wp db export command.
You can easily export and backup with the wp db export command.
$ wp db export
Success: Exported to 'yourblogdb-2021-11-03-40c3bc8.sql'.
Check the file.
$ file yourblogdb-2021-11-03-40c3bc8.sql
yourblogdb-2021-11-03-40c3bc8.sql: UTF-8 Unicode text, with very long lines
In this case, the file size was about 100MB.
$ du -h yourblogdb-2021-11-03-40c3bc8.sql
74M yourblogdb-2021-11-03-40c3bc8.sql
As a side note, you would SSH into your rental server or VPS, navigate to the WordPress directory, and then run commands like the above. However, since the output data is placed in the public directory, there is a risk that hackers or third parties could download the file if the file path becomes known. Exported files may contain sensitive data (such as unpublished posts), so itโs a good idea to move the exported file to a location that is not a public directory.
$ mv yourblogdb-2021-11-03-40c3bc8.sql ~/data/
This wp command simply passes configuration file properties to the mysqldump utility and executes it, so similar things can likely be done with the mysqldump command.
Runs mysqldump utility using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php Accepts any valid mysqldump flags [wp db export | WP-CLI Command | WordPress Developer Resources](https://developer.wordpress.org/cli/commands/db/export/)To import what you've exported to another WordPress site, use wp db import.
wp db import | WP-CLI Command | WordPress Developer Resources