Backup and Restore Your VPS
Protect your data with regular backups. This comprehensive guide covers multiple backup strategies and restore procedures for your VPS.
Critical: Back Up Your Data
Server failures can happen. Hardware fails, software bugs occur, and human errors are common. The 3-2-1 rule: Keep 3 copies of your data, on 2 different storage types, with 1 copy offsite.
Table of Contents
Backup Methods Overview
File-Level
Backup specific files and directories
- • Fast and selective
- • Small backup size
- • Easy to restore single files
Database
Export database contents
- • Consistent data state
- • Can be automated
- • Portable across servers
Full Server
Complete system image
- • Everything included
- • Fastest disaster recovery
- • Large storage needed
File-Level Backups
Using rsync (Recommended)
Rsync efficiently copies only changed files:
# Install rsync
sudo apt install rsync -y
# Backup to local directory
rsync -avz --progress /var/www/ /backups/www-backup/
# Backup to remote server
rsync -avz --progress /var/www/ user@backup-server:/backups/www/
# Exclude certain files
rsync -avz --exclude 'cache' --exclude '*.log' /var/www/ /backups/www/
Using tar Archives
# Create compressed backup
tar -czf /backups/www-$(date +%Y%m%d).tar.gz /var/www/
# Create backup with progress
tar -czf - /var/www/ | pv > /backups/www-$(date +%Y%m%d).tar.gz
# Extract backup
tar -xzf /backups/www-20241205.tar.gz -C /restore/location/
Important Directories to Backup
-
/var/www/ - Web files
-
/etc/ - Configuration files
-
/home/ - User home directories
-
/opt/ - Optional software
-
/root/ - Root user files
Database Backups
MySQL/MariaDB Backup
# Single database
mysqldump -u root -p database_name > /backups/db-$(date +%Y%m%d).sql
# All databases
mysqldump -u root -p --all-databases > /backups/all-dbs-$(date +%Y%m%d).sql
# With compression
mysqldump -u root -p database_name | gzip > /backups/db-$(date +%Y%m%d).sql.gz
# Exclude certain tables
mysqldump -u root -p database_name --ignore-table=database_name.cache > backup.sql
PostgreSQL Backup
# Single database
pg_dump database_name > /backups/pgdb-$(date +%Y%m%d).sql
# All databases
pg_dumpall > /backups/all-pgdbs-$(date +%Y%m%d).sql
# With compression
pg_dump database_name | gzip > /backups/pgdb-$(date +%Y%m%d).sql.gz
Restore Database
# MySQL restore
mysql -u root -p database_name < /backups/db-20241205.sql
# From compressed file
gunzip < /backups/db-20241205.sql.gz | mysql -u root -p database_name
# PostgreSQL restore
psql database_name < /backups/pgdb-20241205.sql
Important
Always test your database backups by restoring to a test database. A backup you can't restore is worthless!
Full Server Backups
Snapshot Backups (Control Panel)
X-ZoneServers offers snapshot backups via control panel:
- 1. Log into your client area
- 2. Navigate to your VPS
- 3. Click "Snapshots" or "Backups"
- 4. Create new snapshot (takes 5-15 minutes)
- 5. Restore from snapshot with one click
Pro Tip
Create a snapshot before major updates or changes. It's the fastest way to roll back if something goes wrong.
Manual Full System Backup
# Backup entire system (excluding /proc, /sys, /dev, /run)
sudo tar -czpf /backups/full-system-$(date +%Y%m%d).tar.gz \
--exclude=/proc \
--exclude=/sys \
--exclude=/dev \
--exclude=/run \
--exclude=/mnt \
--exclude=/media \
--exclude=/tmp \
--exclude=/backups \
/
# This creates a complete backup but will be large (10-50GB+)
Automated Backup Scripts
Complete Backup Script
sudo nano /usr/local/bin/backup.sh
Add this script:
#!/bin/bash
# Configuration
BACKUP_DIR="/var/backups"
DATE=$(date +%Y%m%d_%H%M%S)
RETENTION_DAYS=7
# Create backup directory
mkdir -p $BACKUP_DIR
# Backup web files
echo "Backing up web files..."
tar -czf $BACKUP_DIR/www-$DATE.tar.gz /var/www/
# Backup databases
echo "Backing up databases..."
mysqldump -u root -p'YOUR_PASSWORD' --all-databases | gzip > $BACKUP_DIR/databases-$DATE.sql.gz
# Backup configurations
echo "Backing up configurations..."
tar -czf $BACKUP_DIR/etc-$DATE.tar.gz /etc/
# Delete old backups (keep last 7 days)
find $BACKUP_DIR -name "*.tar.gz" -mtime +$RETENTION_DAYS -delete
find $BACKUP_DIR -name "*.sql.gz" -mtime +$RETENTION_DAYS -delete
# Log completion
echo "Backup completed: $DATE" >> $BACKUP_DIR/backup.log
# Optional: Upload to remote storage
# rsync -avz $BACKUP_DIR/ user@remote:/backups/
Make Script Executable
sudo chmod +x /usr/local/bin/backup.sh
Schedule with Cron
# Edit crontab
sudo crontab -e
# Run daily at 2 AM
0 2 * * * /usr/local/bin/backup.sh
# Run weekly on Sunday at 3 AM
0 3 * * 0 /usr/local/bin/backup.sh
# Run twice daily (2 AM and 2 PM)
0 2,14 * * * /usr/local/bin/backup.sh
Restore Procedures
Restore Files
# Extract entire backup
tar -xzf /backups/www-20241205.tar.gz -C /
# Extract to specific location
tar -xzf /backups/www-20241205.tar.gz -C /restore/temp/
# Extract single file
tar -xzf /backups/www-20241205.tar.gz /var/www/index.html
Restore from Snapshot
- 1. Log into control panel
- 2. Navigate to Snapshots
- 3. Select snapshot to restore
- 4. Click "Restore" (server will reboot)
- 5. Wait 5-10 minutes for completion
- 6. Verify all services are running
Warning
Restoring a snapshot will overwrite ALL current data on your server. Make sure you have recent backups before restoring!
Disaster Recovery Checklist
-
1
Assess the damage - what data is lost?
-
2
Stop all services to prevent further damage
-
3
Locate your most recent backups
-
4
Test backups in a safe environment first
-
5
Restore data systematically (databases first, then files)
-
6
Verify all services are working
-
7
Document what happened and improve backup strategy
Backup Best Practices
Frequency
- • Critical data: Daily
- • Website files: Daily or weekly
- • Configurations: After changes
- • Full snapshots: Weekly
Storage Location
- • Never store backups only on the same server
- • Use remote servers or cloud storage
- • Keep 3 copies minimum (3-2-1 rule)
- • Offsite location for disasters
Testing
- • Test restores monthly
- • Verify backup integrity
- • Document restore procedures
- • Time how long restores take
Retention
- • Keep daily backups for 7 days
- • Keep weekly backups for 4 weeks
- • Keep monthly backups for 6-12 months
- • Automatic cleanup old backups
Backup Checklist
- ✓ Automated daily backups configured
- ✓ Backups stored offsite
- ✓ Database backups separate from files
- ✓ Tested restore procedure at least once
- ✓ Backup monitoring/alerts enabled
- ✓ Documented recovery process
Was this article helpful?
Need backup assistance?
Contact Support