LogoAirnode Hosting
Linux VPS

How to Back Up Your VPS - Keep Your Data Safe

Why Backups Matter

Backups are like insurance for your data. If something goes wrong, you can restore everything quickly.

Imagine losing all your websites, databases, and files because of a server crash or hack. Backups prevent this nightmare!


What Should You Back Up?

  • Website files - All your HTML, CSS, JavaScript, and images
  • Databases - MySQL, PostgreSQL, or other databases
  • Configuration files - Server settings and app configs
  • User data - Files uploaded by users
  • SSL certificates - Your security certificates

Important

Always test your backups! A backup that doesn't work is as bad as no backup at all.


Types of Backups

1. Full Backup

  • What it is: Complete copy of everything
  • When to use: Weekly or monthly
  • Pros: Easy to restore everything
  • Cons: Takes more time and space

2. Incremental Backup

  • What it is: Only backs up changed files since last backup
  • When to use: Daily
  • Pros: Fast and uses less space
  • Cons: More complex to restore

3. Database Backup

  • What it is: Just your databases
  • When to use: Multiple times per day
  • Pros: Small and fast
  • Cons: Doesn't include files

Simple Backup Script for Files

Create a backup script that copies your important files:

#!/bin/bash
# Create a backup script

# Set variables
BACKUP_DIR="/home/backups"
DATE=$(date +%Y%m%d_%H%M%S)
WEBSITE_DIR="/var/www"
CONFIG_DIR="/etc"

# Create backup directory
mkdir -p $BACKUP_DIR

# Backup website files
tar -czf $BACKUP_DIR/website_backup_$DATE.tar.gz $WEBSITE_DIR

# Backup configuration files
tar -czf $BACKUP_DIR/config_backup_$DATE.tar.gz $CONFIG_DIR

# Keep only last 7 backups (delete old ones)
find $BACKUP_DIR -name "*.tar.gz" -mtime +7 -delete

echo "Backup completed: $DATE"

Save this as /home/backup_script.sh and make it executable:

chmod +x /home/backup_script.sh

Database Backup Script

For MySQL/MariaDB databases:

#!/bin/bash
# Database backup script

BACKUP_DIR="/home/backups"
DATE=$(date +%Y%m%d_%H%M%S)
DB_USER="your_username"
DB_PASS="your_password"
DB_NAME="your_database"

# Create backup directory
mkdir -p $BACKUP_DIR

# Backup database
mysqldump -u $DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/database_backup_$DATE.sql

# Compress the backup
gzip $BACKUP_DIR/database_backup_$DATE.sql

# Keep only last 30 database backups
find $BACKUP_DIR -name "database_backup_*.sql.gz" -mtime +30 -delete

echo "Database backup completed: $DATE"

For PostgreSQL:

pg_dump your_database > $BACKUP_DIR/database_backup_$DATE.sql

Setting Up Automatic Backups

Using Cron (Linux Task Scheduler)

# Open crontab editor
crontab -e

Add these lines to run backups automatically:

# Daily file backup at 2 AM
0 2 * * * /home/backup_script.sh

# Database backup every 6 hours
0 */6 * * * /home/database_backup_script.sh

# Weekly full backup on Sunday at 3 AM
0 3 * * 0 /home/full_backup_script.sh

Cron Schedule Explained

FieldValuesExample
Minute0-590 = at minute 0
Hour0-232 = at 2 AM
Day1-31* = every day
Month1-12* = every month
Weekday0-70 = Sunday

Cloud Backup Options

1. Google Drive (Free 15GB)

# Install rclone
curl https://rclone.org/install.sh | sudo bash

# Configure Google Drive
rclone config

# Sync backups to Google Drive
rclone sync /home/backups remote:backups

2. AWS S3 (Pay per use)

# Install AWS CLI
sudo apt install awscli

# Configure AWS
aws configure

# Upload backup
aws s3 cp /home/backups/website_backup.tar.gz s3://your-bucket/backups/

3. Dropbox (Free 2GB)

# Install Dropbox uploader
wget https://raw.githubusercontent.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh
chmod +x dropbox_uploader.sh

# Upload backup
./dropbox_uploader.sh upload /home/backups/website_backup.tar.gz backups/

Testing Your Backups

Always test that your backups work:

Test File Backup

# Extract backup to test location
mkdir /tmp/test_restore
tar -xzf /home/backups/website_backup_20241201_020000.tar.gz -C /tmp/test_restore

# Check if files are there
ls -la /tmp/test_restore/var/www/

Test Database Backup

# Create test database
mysql -u root -p -e "CREATE DATABASE test_restore;"

# Restore backup to test database
mysql -u root -p test_restore < /home/backups/database_backup_20241201_020000.sql

# Check if data is there
mysql -u root -p -e "USE test_restore; SHOW TABLES;"

# Clean up test database
mysql -u root -p -e "DROP DATABASE test_restore;"

Backup Best Practices

1. 3-2-1 Rule

  • 3 copies of your data
  • 2 different types of storage (local + cloud)
  • 1 off-site backup (cloud or different location)

2. Regular Testing

  • Test restore process monthly
  • Verify backup files are not corrupted
  • Check that all important data is included

3. Monitor Your Backups

# Check if backup script ran today
ls -la /home/backups/ | grep $(date +%Y%m%d)

# Check backup sizes
du -sh /home/backups/*

# Check available disk space
df -h

4. Secure Your Backups

# Encrypt sensitive backups
gpg -c /home/backups/database_backup.sql

# Set proper permissions
chmod 600 /home/backups/*

Disaster Recovery Plan

Have a plan for when things go wrong:

  1. Stop the bleeding - Stop any ongoing issues
  2. Assess damage - What data is lost?
  3. Choose backup - Pick the right backup to restore
  4. Restore data - Restore from backup
  5. Test everything - Make sure it all works
  6. Document lessons - Learn from the experience

Quick Commands Reference

# Create backup directory
mkdir -p /home/backups

# Backup files with tar
tar -czf backup.tar.gz /path/to/files

# Backup MySQL database
mysqldump -u user -p database > backup.sql

# Backup PostgreSQL database
pg_dump database > backup.sql

# Schedule daily backup at 2 AM
echo "0 2 * * * /home/backup_script.sh" | crontab -

# Check cron jobs
crontab -l

# Test backup extraction
tar -tzf backup.tar.gz

You're Protected!

With regular backups, you can sleep soundly knowing your data is safe. Remember: it's not if something will go wrong, but when!

On this page