The worst-case scenario on a server is data loss - hardware failure, a wrong command, ransomware or a simple human error. The only real protection against these disasters is regular and tested backups. This guide explains the fundamentals of server and website backup and setting up a practical backup with rsync.
Related reading: Cron jobs on Linux · Essential Linux server commands · Connecting to a Server with SSH: Key-Based Authentication
Why Is Backup Critical?
Servers fail, disks die, commands get run incorrectly. This is not a possibility but, over time, a certainty. Data without a backup cannot be recovered once lost. Backup is not a cost item - it is the insurance of your business.
The 3-2-1 Backup Rule
The gold standard of the backup world is the 3-2-1 rule:
- 3 copies: Keep at least three copies of the data (one original, two backups).
- 2 different media: Backups should be on at least two different media (for example a local disk and a remote server).
- 1 copy in a different location: At least one backup should be physically elsewhere - against fire, theft or a regional outage.
A backup kept on the same server is useless if the server crashes entirely. That is why the backup being elsewhere is essential.
File Backup with rsync
rsync is a powerful tool that copies files and directories efficiently. Its most important feature is that it works incrementally: it transfers only changed files, which makes backup fast and low-cost.
# Back up a directory to another location locally
rsync -av /var/www/site /backup/
# Back up to a remote server over SSH
rsync -avz -e ssh /var/www/site user@backup-server:/backups/
# Also delete files from the destination that were deleted at the source (--delete)
rsync -avz --delete /var/www/site user@backup-server:/backups/-a (archive) preserves permissions and timestamps, -v gives verbose output, -z compresses the transfer. The --delete option mirrors the destination to the source exactly - use it carefully.
Automatic Backup with Cron
A backup taken by hand is a forgotten backup. Automating backup with cron is the key to a sustainable backup routine:
# Back up to a remote server every night at 02:30 (inside crontab -e)
30 2 * * * rsync -avz --delete /var/www user@backup-server:/backups/ >> /var/log/backup.log 2>&1Back Up the Database Separately
A file backup alone is not enough - on dynamic sites the real data is in the database. You need to additionally export the database with tools like mysqldump or pg_dump. We cover database backup strategies in depth in a separate guide.
Frequently Asked Questions
How often should I take backups?
It depends on how often the data changes. For a site updated daily, a daily backup makes sense; critical and frequently-changing data may need shorter intervals. The question is: how much data loss can you afford?
rsync or a snapshot?
The two complement each other. A provider's server snapshot offers fast full recovery; rsync offers a file-level, flexible and remote backup. The ideal setup uses both.
Should I encrypt backups?
If the backup contains sensitive data and is stored in a different location, yes. Encryption protects your data if the backup falls into the wrong hands.