Permission error on msql dump when using wp-cli aliases

Hi,

When using this script
sync-dev-to-prod-FOR-REAL.sh
Referenced here: Leveraging WP-CLI Aliases in Your WordPress Development Workflow - #2 by benword

When the alias tries to save a backup (just-in-case.sql) I have a permissions problem. Any idea how to fix this?

In my test, I was actually using a staging alias like this:

   @staging:
      ssh: admin@staging.example.com/srv/www/example.com/current

mysqldump: Canā€™t create/write to file ā€˜just-in-case.sqlā€™ (Errcode: 13 ā€œPermission deniedā€)

1 Like

Where does your script currently reside? (In a Trellis/Bedrock setup, it should go in the ā€˜siteā€™ folder).

And where are you running the script? You should be running it from the same directory where the script exists (again the ā€˜siteā€™ folder if youā€™re using Bedrock).

1 Like

I put my .sh script in ā€œtrellis/binā€ because thatā€™s where the other .sh script was. Not sure where to put it.

Iā€™m running the script from within the ā€œsiteā€ folder. The aliases (wp-cli.yml) are in ā€œsiteā€.

Can you try putting it in the ā€˜siteā€™ folder and reporting back?

same permissions error
mysqldump: Canā€™t create/write to file ā€˜just-in-case.sqlā€™ (Errcode: 13 ā€œPermission deniedā€)

This works now.

What I did.

changed the user in the alias to ā€œwebā€:

From

 @staging:
      ssh: admin@staging.example.com/srv/www/example.com/current

To

 @staging:
      ssh: web@staging.domain.com/srv/www/domain.com/current

Removed ā€œ->ā€ (not sure if this is needed, couldnā€™t find it in the wp-cli documentation)

From

wp @development db export - > sql-dump-development.sql

To

wp @development db export sql-dump-development.sql


Added a timestamp to the backup (unrelated to fix).

wp @staging db export just-in-case-$(date +%Y-%m-%d_%T).sql


Removed the ā€œsql-dump-development.sqlā€ file at the end (unrelated to fix).

ssh web@staging.example.com 'rm /srv/www/example.com/current/sql-dump-development.sql'

Complete version

read -r -p "Would you really like to reset THE STAGING DATABASE and send up the latest from dev? [y/N] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then
    wp @development db export sql-dump-development.sql
    scp sql-dump-development.sql web@staging.example.com:/srv/www/example.com/current

    wp @staging db export just-in-case-$(date +%Y-%m-%d_%T).sql
    wp @staging db reset --yes

    wp @staging db import sql-dump-development.sql
    wp @staging search-replace http://example.dev https://staging.example.com
    scp -r web/app/uploads/ web@staging.example.com:/srv/www/example.com/shared
    ssh vagrant@example.dev 'rm /srv/www/example.com/current/sql-dump-development.sql'
    ssh web@staging.example.com 'rm /srv/www/example.com/current/sql-dump-development.sql'
else
    exit 0
fi

I would appreciate it if someone more knowledgeable than me can look this over and let me know if anything is totally wacko.

2 Likes

Iā€™ve been looking about to make myself a similar script. Have you figured it out yet?