Using rsync to Migrate WordPress Files

Contents

Introduction

WordPress powers over 40% of the web, making efficient and reliable migrations a top priority for developers and administrators. rsync is a powerful command-line tool ideal for synchronizing files and directories between two locations—locally or over a network. This article provides a comprehensive guide to using rsync for migrating WordPress installations, from preparation through testing and automation.

Why Choose rsync

  • Incremental Transfers: Only changed files are copied, saving time and bandwidth.
  • Resumable: Interrupted transfers can resume without starting over.
  • Preserves Attributes: Maintains ownership, permissions, timestamps and symlinks.
  • Secure: Can be used over SSH for encrypted transfers.

For official documentation, see the rsync manual and the WordPress guide on Moving WordPress.

Prerequisites

  • SSH access to source and destination servers.
  • rsync installed on both ends (sudo apt-get install rsync on Debian/Ubuntu).
  • Proper database export/import plan (this article focuses on files).
  • Sufficient disk space and backup of existing sites.

Step 1: Prepare Source and Destination

  1. Create a backup: tar czf wp-backup-before-rsync.tar.gz /var/www/html/wordpress.
  2. Ensure the destination directory exists: mkdir -p /var/www/html/wordpress.
  3. Set correct ownership/permissions placeholder: chown www-data:www-data /var/www/html/wordpress.

Step 2: Basic rsync Syntax

The core command looks like this:

rsync -avz -e ssh /path/to/source/ user@destination:/path/to/target/

Explanation of flags:

Flag Description
-a Archive mode (recurses, preserves permissions, timestamps, symlinks).
-v Verbose output.
-z Compress data during transfer.
-e ssh Use SSH for secure transport.

Step 3: Advanced Options

  • --delete: Remove files on destination that no longer exist on source.
  • --progress: Show detailed progress during transfer.
  • --partial and --partial-dir=.rsync-partial: Keep partially transferred files.
  • -h: Human-readable numbers (e.g. 1K, 234M).

Example with advanced flags

rsync -azh --delete --progress -e ssh -p 22 /var/www/html/wordpress/ user@dest:/var/www/html/wordpress/

Step 4: Excluding Unnecessary Files

WordPress sites often contain cache, local logs, and node_modules that you may not wish to migrate:

rsync -avz --exclude=wp-content/cache --exclude=node_modules --exclude=.log ...

Or define an exclude.txt and pass it:

rsync -avz --exclude-from=exclude.txt ...

Step 5: Handling Permissions and Ownership

After syncing, verify that www-data (or your web server user) owns files:

ssh user@dest chown -R www-data:www-data /var/www/html/wordpress/

Ensure directories have 755 and files 644 permissions:

ssh user@dest find /var/www/html/wordpress/ -type d -exec chmod 755 {} nssh user@dest find /var/www/html/wordpress/ -type f -exec chmod 644 {} 

Step 6: Testing and Verification

  • Verify file count and sizes: rsync --dry-run -avz ....
  • Check site functionality on a staging domain or hosts override.
  • Ensure plugins, themes and uploads appear correctly.

Step 7: Automating with Scripts or Cron

You can create a shell script (wp-migrate.sh) and schedule it:

#!/bin/bashnSOURCE=/var/www/html/wordpress/nDEST=user@dest:/var/www/html/wordpress/nEXCLUDES=--exclude=wp-content/cache --exclude=.lognrsync -azh --delete EXCLUDES -e ssh -p 22 SOURCE DEST

Then add to crontab -e:

0 2    /usr/local/bin/wp-migrate.sh >> /var/log/wp-migrate.log 2>1

Troubleshooting and Best Practices

  • SSH Key Authentication: Use keys to avoid password prompts in automation.
  • Bandwidth Limits: Use --bwlimit=KBPS to throttle transfer.
  • Dry Runs: Always test with --dry-run before real sync.
  • Logging: Capture output to log files for auditing.
  • Version Control: Consider storing wp-content/themes in Git for finer control.

Conclusion

Using rsync for migrating WordPress files offers speed, reliability and flexibility. By carefully crafting your rsync commands, excluding unnecessary data, and automating with scripts, you can streamline deployments and keep staging and production sites in sync. Always complement file syncs with proper database migration and testing for a bulletproof migration strategy.



Acepto donaciones de BAT's mediante el navegador Brave 🙂



Leave a Reply

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