Copying files to another server with rsync, safely

rsync copies folders between two computers over SSH and, on later runs, sends only what changed. It is the most used tool for taking a copy of your VPS to somewhere else, which is the golden rule of any backup: a copy on the same disk is lost with it (backing up a VPS: snapshots or files). The typical command is rsync -avz /var/www/ user@other-server:/backups/www/. On an unmanaged VPS these copies are yours.

Step by step

1 Check rsync is on both sides. rsync --version. On Debian and Ubuntu you install it with sudo apt install rsync. You also need SSH access to the destination (first steps with your VPS).
2 Rehearse first. rsync -avzn /var/www/ user@other-server:/backups/www/. The n (dry-run) shows what would be copied, without copying anything.
3 Do the real copy by dropping the n: rsync -avz /var/www/ user@other-server:/backups/www/. If the destination uses another SSH port: rsync -avz -e "ssh -p 2222" ... (2222 is an example).
4 Check the result at the destination: ls -l and the size with du -sh. A copy counts only after you have restored it once: making your own backup and testing it.
5 Automate it. To run without a password, use an SSH key meant only for the backup and schedule it with cron (automatic backups with cron). Keep a record of the output with --log-file=/var/log/rsync-backup.log.

The options you will use

Option What it does
-a Archive mode: keeps permissions, owners, dates and links, and goes into subfolders.
-v Shows what it is doing.
-z Compresses while sending. Saves bandwidth, costs processor; for files already compressed (images, video) it does little.
-n / --dry-run Rehearses without copying. Always use it before a dangerous option.
--exclude='cache/' Leaves out what is not worth copying (caches, temporary files).
--delete Deletes at the destination what no longer exists at the source. It makes a mirror, and mirrors the deletions too.
-P Shows progress and lets you resume an interrupted large file.
The slash at the end of the path changes the result. /var/www/ (with a slash) copies the folder’s contents; /var/www (without) copies the whole folder into the destination. Getting it wrong creates an extra folder, or mixes files where they should not be. Rehearse with -n.
--delete propagates mistakes. If you deleted a file by accident at the source, the mirror deletes it at the destination on the next run, and the copy no longer has it. For a copy that protects you from deletions, keep several older copies (dated folders) rather than a single mirror.
Databases are not copied with rsync while they are working. Export them first (mysqldump and pg_dump) and copy the exported file. Use a user only for backups, instead of root, and limit what it can do at the destination.

Want to know what your VPS plan includes for backups? Tell us the service name and we will confirm.

Open a support ticket

SEE ALSO

Backing up a VPS: snapshots or files

Automatic backups of your own application

Backing up with mysqldump and pg_dump

Making your own backup and testing it

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from 5.940,00 Kz/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?