Capistrano::WPCLI for Trellis and Bedrock db push/pull permission error

I set up a test project similar to the roots example project (GitHub - roots/roots-example-project.com: Example Roots stack project (Trellis, Bedrock, Sage)). I was able to deploy to a bitbucket droplet set up as suggested in the trellis docs. So everything is working great.

Now I need to be able to push/pull databases to/from local/staging/production environments. Ruby and capistrano is brand new to me, but I did watch the screencast for deploying bedrock with capistrano, and read the documentation at capistrano, so I have some understanding about how it works.

I set up Capistrano::WPCLI for Trellis and Bedrock ruby gem and itā€™s mostly working. All tasks provided by the gem work successfully, except for wpcli:db:push and wpcli:db:pull. From looking at the output, it looks like a permission problem, but I canā€™t figure out what needs to be done to fix it.

Hereā€™s what I get when I run the task:

LAITHs-iMac:site laithsinawi$ bundle exec cap staging wpcli:db:push --trace
** Invoke staging (first_time)
** Execute staging
** Invoke load:defaults (first_time)
** Execute load:defaults
** Invoke wpcli:db:push (first_time)
** Invoke wpcli:db:backup:remote (first_time)
** Invoke wpcli:db:backup:create_backup_dir (first_time)
** Execute wpcli:db:backup:create_backup_dir
** Execute wpcli:db:backup:remote
00:00 wpcli:db:backup:remote
01 wp db export - | gzip > /tmp/wpcli_database.sql.gz
:heavy_check_mark: 01 web@bedrock.sinawiwebdesign.com 0.427s
Downloading _backup_database/db_example.com_staging_2017_01_17_12_08.sql.gz 100.0%
02 rm /tmp/wpcli_database.sql.gz
:heavy_check_mark: 02 web@bedrock.sinawiwebdesign.com 0.089s
** Execute wpcli:db:push
00:00 wpcli:db:push
01 wp db export - | gzip > /tmp/wpcli_database.sql.gz
:heavy_check_mark: 01 vagrant@example.dev 0.198s
cap aborted!
Errno::EACCES: Permission denied @ rb_sysopen - /tmp/wpcli_database.sql.gz
ā€¦

And hereā€™s the cap error log:
INFO ---------------------------------------------------------------------------
INFO START 2017-01-17 12:08:57 -0800 cap staging wpcli:db:push
INFO ---------------------------------------------------------------------------
DEBUG [e5f4b18a] Running [ -d _backup_database ] as laithsinawi@localhost
DEBUG [e5f4b18a] Command: [ -d _backup_database ]
DEBUG [e5f4b18a] Finished in 0.004 seconds with exit status 0 (successful).
DEBUG [cacf2018] Running if test ! -d /srv/www/example.com/current; then echo ā€œDirectory does not exist ā€˜/srv/www/example.com/currentā€™ā€ 1>&2; false; fi as web@bedrock.sinawiwebdesign.com
DEBUG [cacf2018] Command: if test ! -d /srv/www/example.com/current; then echo ā€œDirectory does not exist ā€˜/srv/www/example.com/currentā€™ā€ 1>&2; false; fi
DEBUG [cacf2018] Finished in 0.311 seconds with exit status 0 (successful).
INFO [6c455615] Running /usr/bin/env wp db export - | gzip > /tmp/wpcli_database.sql.gz as web@bedrock.sinawiwebdesign.com
DEBUG [6c455615] Command: cd /srv/www/example.com/current && /usr/bin/env wp db export - | gzip > /tmp/wpcli_database.sql.gz
INFO [6c455615] Finished in 0.427 seconds with exit status 0 (successful).
DEBUG Downloading _backup_database/db_example.com_staging_2017_01_17_12_08.sql.gz 0.0%
INFO Downloading _backup_database/db_example.com_staging_2017_01_17_12_08.sql.gz 100.0%
INFO [4c958710] Running /usr/bin/env rm /tmp/wpcli_database.sql.gz as web@bedrock.sinawiwebdesign.com
DEBUG [4c958710] Command: cd /srv/www/example.com/current && /usr/bin/env rm /tmp/wpcli_database.sql.gz
INFO [4c958710] Finished in 0.089 seconds with exit status 0 (successful).
DEBUG [09a572d2] Running if test ! -d /srv/www/example.com/current; then echo ā€œDirectory does not exist ā€˜/srv/www/example.com/currentā€™ā€ 1>&2; false; fi as vagrant@example.dev
DEBUG [09a572d2] Command: if test ! -d /srv/www/example.com/current; then echo ā€œDirectory does not exist ā€˜/srv/www/example.com/currentā€™ā€ 1>&2; false; fi
DEBUG [09a572d2] Finished in 0.107 seconds with exit status 0 (successful).
INFO [dd909bc2] Running /usr/bin/env wp db export - | gzip > /tmp/wpcli_database.sql.gz as vagrant@example.dev
DEBUG [dd909bc2] Command: cd /srv/www/example.com/current && /usr/bin/env wp db export - | gzip > /tmp/wpcli_database.sql.gz
INFO [dd909bc2] Finished in 0.198 seconds with exit status 0 (successful).

Also, just wanted to add when I log into the local VM, permissions look correct:

vagrant@example:/$ ls -l /tmp
total 12
drwx------ 2 vagrant vagrant 4096 Jan 17 23:57 ssh-ZKdVIuEmrt
-rw-rw-r-- 1 vagrant vagrant 7384 Jan 17 23:59 wpcli_database.sql.gz

And remote server (this was owned by root, so I changed permissions to web:www-data) :

root@roots:/srv/www/example.com/current# ls -l /tmp
total 5364
-rwxrwxrwt 1 web www-data 305368 Jan 17 17:03 composer-installer.php
drwxrwxrwt 3 web www-data 4096 Jan 17 16:50 systemd-private-ded0486961d041ae9740d3932b58a2c7-systemd-timesyncd.service-1zxFd3
-rwxrwxrwt 1 web www-data 5176480 Jan 17 17:04 wp-cli-1.0.0.phar
-rwxrwxrwt 1 web www-data 495 Jan 17 17:04 wp-completion-1.0.0.bash

Thanks so much for your help,
Laith

This seems a little beyond the scope of this forum but maybe someone else has some experience with this.

Regardless, permission errors can be frustrating but the solution is obviously to make sure the user running those commands/script has the necessary permissions. So Iā€™d verify which users those commands are being run under on both local and remote.

Thanks for the response. I just noticed that it actually works when I run it with sudo. Iā€™m not sure if thatā€™s going to be an issue, but as long as it works, Iā€™m happy.

This works!
sudo bundle exec cap staging wpcli:db:push
sudo bundle exec cap staging wpcli:db:pull

1 Like

Update: just wanted to say that the permission issue and having to use sudo resolved itself somehow, probably from installing updates via brew or something. Anyway, everything works perfectly now (multiple environments, deploys, push/push DB and media files, etc). Iā€™ve set up other projects using the roots suite, and capistrano wpcli (https://github.com/itsensoul/capistrano-trellis-bedrock-wpcli), and everything works without a hitch, thanks to great document.

Thanks so much!

1 Like