You've set up your VPS. Your website is running. But what happens if something breaks?
A server can crash. A hard drive can fail. You can accidentally delete files. Without backups, you lose everything.
This guide shows you how to set up automatic backups on your VPS. No expensive software required.
What You Need to Back Up
Before setting up backups, know what needs backing up.
Website files. Your WordPress files, HTML pages, PHP scripts, uploaded images. Usually located in /var/www/ or your home directory.
Database. If you run WordPress or any dynamic site, the database contains your posts, pages, comments, and settings.
Configuration files. Nginx or Apache configs, PHP settings, firewall rules. These are harder to rebuild from scratch.
Option 1: Simple Backups with rsync (Files Only)
rsync is a command-line tool that copies files to another location. It only copies changed files, so it's efficient.
To back up your website files to a remote server:
rsync -avz -e ssh /var/www/ username@backupserver.com:/backups/
To make this automatic, add it to crontab.
crontab -e
Add this line to run daily at 2 AM:
0 2 * * * rsync -avz -e ssh /var/www/ username@backupserver.com:/backups/
Option 2: Database Backups with mysqldump (MySQL/MariaDB)
First, create a backup script.
sudo nano /usr/local/bin/backup_db.sh
Add this content (replace with your database credentials):
#!/bin/bash
DATE=$(date +%Y%m%d)
mysqldump -u username -ppassword database_name > /backups/db_$DATE.sql
gzip /backups/db_$DATE.sql
find /backups -type f -name "*.gz" -mtime +30 -delete
Make it executable:
sudo chmod +x /usr/local/bin/backup_db.sh
Add to crontab for daily backups:
0 3 * * * /usr/local/bin/backup_db.sh
The script creates a dated SQL file, compresses it, and deletes backups older than 30 days.
Option 3: Complete VPS Backup with tar (All Files)
If you want to back up everything (files + configs), use tar to create an archive.
sudo tar -czpf /backups/server_backup_$(date +%Y%m%d).tar.gz /var/www /etc/nginx /etc/apache2 /etc/php
This creates a compressed archive of your website files and configurations.
Option 4: Off-site Backups with rclone (Cloud Storage)
Local backups are good. But if your server fails completely, you need off-site backups.
Install rclone:
sudo apt install rclone -y
Configure rclone for Google Drive, Dropbox, or Backblaze B2:
rclone config
Follow the prompts to set up your cloud storage.
Then create a script to sync your backups to the cloud:
#!/bin/bash
rclone sync /backups/ remote:backup-bucket/
Add to crontab to run after your local backups:
0 4 * * * /usr/local/bin/sync_cloud.sh
Recommended Backup Strategy for Small Websites
Here's a solid strategy that's easy to implement.
Daily database backup. Use mysqldump. Keep 30 days of backups.
Weekly file backup. Use rsync or tar. Keep 4 weeks of backups.
Off-site storage. Use rclone to copy backups to Backblaze B2 or Google Drive. Backblaze B2 costs about $0.006 per GB per month – incredibly cheap.
Monthly snapshot. Most VPS providers offer snapshot features. Take a full system snapshot once a month. Costs a few dollars.
Testing Your Backups (Critical)
A backup that can't be restored is useless. Test regularly.
Test file restoration. Pick a random file from your backup and restore it to a different location. Verify it works.
Test database restoration. Create a test database and import your backup:
mysql -u username -p test_database < backup.sql
Test full restoration. Once a month, spin up a test VPS and restore everything from scratch. This ensures your backup process actually works.
Vendor-Specific Backup Options
Most VPS providers offer backup features. Here's what they include.
| Provider | Backup Option | Cost |
|---|---|---|
| DigitalOcean | Weekly backups (5 days retention) | 20% of Droplet cost |
| Vultr | Weekly backups (4 weeks retention) | $1-2 per month |
| Linode | Daily backups (7 days retention) | $2-5 per month |
| Hetzner | Snapshot + Volume backups | €0.002/GB per day |
| UpCloud | Daily backups (30 days retention) | 20% of server cost |
Using provider backups is convenient, but don't rely solely on them. Keep your own off-site backups too.
Quick Setup Script (All-in-One)
Here's a complete backup script. Save it as /usr/local/bin/complete_backup.sh
#!/bin/bash
# Complete backup script for VPS
DATE=$(date +%Y%m%d)
BACKUP_DIR="/backups"
# Create backup directory if it doesn't exist
mkdir -p $BACKUP_DIR
# Backup database
mysqldump -u YOUR_USER -pYOUR_PASS YOUR_DB > $BACKUP_DIR/db_$DATE.sql
gzip $BACKUP_DIR/db_$DATE.sql
# Backup website files and configs
tar -czf $BACKUP_DIR/files_$DATE.tar.gz /var/www /etc/nginx /etc/apache2 2>/dev/null
# Delete backups older than 30 days
find $BACKUP_DIR -type f -mtime +30 -delete
# Optional: sync to cloud (configure rclone first)
# rclone sync $BACKUP_DIR remote:backup-bucket/
echo "Backup completed on $DATE"
Make it executable and add to crontab:
sudo chmod +x /usr/local/bin/complete_backup.sh
sudo crontab -e
Add this line for daily backup at 2 AM:
0 2 * * * /usr/local/bin/complete_backup.sh
Next Steps
Set up your backups today. It takes 30 minutes and saves you from disaster later.
Need a VPS to practice on? Check our recommended VPS providers.