wp-cli / db-command

Performs basic database operations using credentials stored in wp-config.php.
MIT License
71 stars 59 forks source link

wp db export to STDOUT #212

Closed Ontario7 closed 2 years ago

Ontario7 commented 3 years ago

Im try to export to file /public_html$ /usr/local/bin/wp db export - > 1.sql

Bu the file is empty

-rw-r--r-- 1 admin admin 0 Nov 1 10:21 1.sql

However if I do this without '-' sign all going good and file saving (but I then can not use gzip in the pipe)

kanlukasz commented 2 years ago

You can do it this way:

Ontario7 commented 2 years ago

Thanks for idea. Trying this I found another problem:

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65011744 bytes) in phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/utils.php on line 546

134 MB? Serious? 65011744 is less then 134217728

However I have 256:

define('WP_MEMORY_LIMIT', '256M' ); define('WP_MAX_MEMORY_LIMIT', '256M' );

kanlukasz commented 2 years ago

I'm not sure what you want to achieve overall or what your entire script looks like, but there is a better way to take a sql dump and gzip it.

I can see two ways for you:

  1. If you have to use wp-cli, you can just use wp db export first and then you can compress this file with gzip EXAMPLE: wp db export data.sql ; gzip data.sql

  2. If using wp-cli is not a must, you can just use mysqldump EXAMPLE 1: mysqldump -u <DB_USER> -p <DB_NAME> | gzip > data.sql.gz or EXAMPLE 2: mysqldump -u <DB_USER> -p <DB_NAME> > data.sql ; gzip data.sql

Ontario7 commented 2 years ago

Thank you. Before I always use method 2 /usr/bin/mysqldump --opt -u"${MYDBUSER}" -p"${MYDBPASS}" ${MYDBTODUMP} --add-drop-table | gzip > ~/tmp/${DOMAINNAME}.${DATE_TIME_NOW}.sql.gz;

now we migrate some scripts to wp-cli and try to avoid passwords in scripts also as temp files

oxyc commented 2 years ago

Are you using wp-cli v2.6.0? Memory usage was a bug that's been fixed https://github.com/wp-cli/db-command/issues/195

jcatello commented 2 years ago

If you want to use wp cli to export and gzip with one command you can use

wp db export - | gzip > dbname.sql.gz

As @oxyc mentioned there is a memory bug in wp cli 2.5 so make sure to upgrade to 2.6:

wp cli update --yes

If you still have memory issues, you want to increase it via php.ini

Ontario7 commented 2 years ago

wp db export - | gzip > dbname.sql.gz

As @oxyc mentioned there is a memory bug in wp cli 2.5 so make sure to upgrade to 2.6:

Thanks, finally it works. Thanks Bro!

going to change all my backup scripts...