Favorite rsync Commands for Copying WordPress Sites

Copying, moving, cloning WordPress is easiest and fastest by the command line using SSH – especially for large sites it is the best and often essential! You can use this method on any Linux system which contains rsync like CentOS (cPanel), Plesk, Ubuntu and debian systems on VPS, dedicated servers or even containers like docker or LXD and LXC.

Easy rsync Commands to Copy WordPress Sites via SSH

We are going to cover a few different ways to use rsync to copy files from one VPS or dedicated server to another.

  • Copy local files to remote
  • Copy remote files to local path
  • Specifying an SSH key for rsync in case you are copying from cPanel
  • Specifying a non-standard SSH Port
  • Specifying an SSH key and non-standard SSH port

Copy Local to Remote

The basic syntax for the rsync command is this

rsync -avz /source/path/ /destination/path/ --info=progress2

Working example

rsync -avz /var/www/vhosts/staging.engineering.com/httpdocs/ root@207.148.12.5:/var/www/engineer.wpbullet.me/ --info=progress2

Note that if you do not keep the trailing slash in the source directory, it will copy the httpdocs directory so it ends up in /var/www/engineer.wpbullet.me/httpdocs instead of all of the contents of the httpdocs folder ending up in /var/www/engineer.wpbullet.me. Generally you want to keep the trailing slashes to avoid confusion.

If you want to copy the symlinks you can use the --copy-link option and exclusions can be done with the --exclude flag.

This example is actually for a local to local copy!

rsync -avz ./ /var/www/lottosocial.hatchster.com/ --copy-links --exclude=wp-config.php --info=progress2

Copy Remote to Local

If you want to copy remote files to your local Linux server

rsync -avz user@IP-address:/absolute/path/on/remote/ /local/destination/path/ --info=progress2

Working example

rsync -avz root@207.148.12.5:/var/www/engineer.wpbullet.me/ /var/www/vhosts/staging.engineeringexpress.com/httpdocs/ --info=progress2

Copy Local Path to Remote Path Using Specific Ports

If the remote SSH server daemon is not running on the standard port 22 you can change that with the -e flag that passes different SSH command argument to rsync.

Copy remote path public_html using port 2222 and copy it to /tmp/live

rsync -avz -e 'ssh -p 2222' abcmaj@abcmallorca.de:~/public_html/ /tmp/live/ --info=progress2

Copy Local Files to Remote Server Using Specific SSH Key

Basic idea is to use the -e flag again to pass special modifications to the rsync command.

rsync -avz -e 'ssh -i sshkey' user@host:path/ /target/path/ --info=progress2

If specifying a key use this command, it is copying the remote path folder public_html to the local folder /tmp/live/

rsync -avz -e 'ssh -i key' abcmaj@abcmallorca.de:~/public_html/ /tmp/live/ --info=progress2

Now we can combine the SSH key and non-standard SSH port

Specific Private SSH Key and Custom SSH Port

Plenty of hosts change the standard SSH port to something else for security through obscurity since so many bots just attempt the standard SSH port 22.

For example WPEngine use port 2222, we can combine the key and port customizations inside of the same -e flag!

So in this working example we use an SSH key called key located in the current folder with SSH port 2222 on remote server

rsync -avz -e 'ssh -i key -p 2222' abcmaj@abcmallorca.de:~/public_html/ /tmp/live/ --info=progress2

In this example we are using Siteground and their custom port 18765.

rsync -avz -e 'ssh -i key -p 18765' abcmaj@abcmallorca.de:~/public_html/ /tmp/live/ --info=progress2

Troubleshooting rsync Errors

Common error is that the permissions are too liberal for rsync to proceed.

ECDSA key fingerprint is SHA256:5IQref44tLXy8FeTRFDUwdA6bXotmTB79flkG8CGnXc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[146.66.112.160]:18765' (ECDSA) to the list of known hosts.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for 'key' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "key": bad permissions
Permission denied (publickey).
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: unexplained error (code 255) at io.c(226) [Receiver=3.1.1]

You can usually fix this error by making permissions exclusive for your user

chmod 600 key

rsync versions have to match! Check your rsync version with this command

rsync --version

Make sure the version matches on both the local and remote server, here it is version 3.1.2

rsync  version 3.1.2  protocol version 31
Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
    64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
    append, ACLs, xattrs, iconv, symtimes, prealloc

rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you
are welcome to redistribute it under certain conditions.  See the GNU
General Public Licence for details.

Hopefully this helps you copy your WordPress sites much more efficiently!

This last command is here because sometimes I forget a trailing slash

Copy all files * from current directory to directory above ../

rsync * ../ -avzh --info=progress2

Sources for this tutorial below as always 🙂

Sources

rsync Manual
SCP Permission Denied
rsync and Symbolic Links
Using SSH Key with rsync

2 thoughts on “Favorite rsync Commands for Copying WordPress Sites”

  1. Great resource, thank you. I use rysnc every day, it is such a useful tool.

    A tip for when your ssh key has changed and trying to rsync to a server that you have previously connected to, you get:

    @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
    Offending RSA key in .ssh/known_hosts: 34

    You can use sed to remove the offending line, in this case line 34:

    sed -i -e ’34d’ ~/.ssh/known_hosts

    and then you can try the rsync again with the new key.

Comments are closed.