Issue exporting a database using wp-cli from local development to remote staging server

I’ve been playing with WP-CLI aliases as per: Leveraging WP-CLI Aliases in Your WordPress Development Workflow. It’s great being able to run WP-CLI commands on remote servers and within vagrant right from the local site directory.

I also found the shell script @ben posted up made loads of sense. I tweaked it a bit for my liking and I use it for pushing the database and uploads from dev up to staging:

read -r -p "Would you really like to overwrite the staging database with 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.sql
    wp @staging db reset --yes

    wp @staging db import sql-dump-development.sql
    wp @staging search-replace https://example.dev https://staging.example.com

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

    read -r -p "Did everything run smoothly? Remove the temp backup sql file? [y/N] " response
    if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
    then
        ssh web@staging.example.com 'rm /srv/www/example.com/current/just-in-case.sql'
    fi

    read -r -p "Sync uploads from dev to staging? [y/N] " response
    if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
    then
        scp -r web/app/uploads/ web@staging.example.com:/srv/www/example.com/shared
    fi
else
    exit 0
fi

Could be improved I’m sure but works for me at the moment!

1 Like