Sync WordPress database and uploads

Now I have this WordPress website running, I can create lots of content like pages, blog posts, uploads and more and more… Sometimes, however, I’m in need of having this content also locally on my Mac when I’m working on it. I found myself still using FTP to download the uploaded images or PHPMyAdmin to export the database and import it locally using another DB client… Not very convenient.

So, since I can use SSH to my webserver I can use rsync as well. Nice! But how to do that? Well, I spent some time on that and figured out it is very easy. With just a couple of commands you can sync your WordPress database and uploads over SSH using rsync from your production webserver to your local machine. Below you’ll find the commands I used with some variables. Obviously you’ll need to replace these variables with your environment variables.

Database sync

Make sure you’re in the project’s root on your development machine. In the commands below use;

  • your SSH user and host,
  • the path of your WordPress installation (in my case that’s /www),
  • your full production domain,
  • your full local development domain.
ssh user@ssh.host.nl "cd /www && wp db export dbexport.sql"
rsync -avze ssh user@ssh.host.nl:/www/dbexport.sql . -P
ssh user@ssh.host.nl "cd /www && rm dbexport.sql"
wp db import dbexport.sql
wp search-replace 'https://www.host.nl' 'http://www.localhost.test'
wp cache flush
rm dbexport.sql

Uploads sync

Just a one liner, but also make sure you’re in the project’s root folder.

rsync -avze ssh user@ssh.host.nl:/www/web/app/uploads ./web/app -P

Leave a comment

Your email address will not be published. Required fields are marked *