Console Login

Automated Backups: The line between a minor hiccup and bankruptcy

Automated Backups: The line between a minor hiccup and bankruptcy

I once watched a junior developer type rm -rf / var/www/html instead of rm -rf /var/www/html (note the space). It took exactly 0.4 seconds to wipe out three years of work on the production server. The silence that followed in the server room was heavier than a fully loaded 42U rack.

Because we had a rigorous automated backup strategy, that disaster turned into a 15-minute restoration drill. If we hadn't? The company would have folded. It is that simple. In the world of VPS Norway hosting, hardware can be replaced, but data is the only asset that is truly irreplaceable.

Too many CTOs rely on their host's RAID arrays and call it a day. Let me be clear: RAID is not a backup. RAID 10 protects you from a disk failure; it does not protect you from file corruption, malicious deletion, or a DROP TABLE command executed at 3 AM. Today, we are going to build a backup system that actually works, using tools that have stood the test of time.

The "3-2-1" Rule Adapted for 2012

The concept is standard, but the implementation requires discipline:

  • 3 copies of your data.
  • 2 different media types (e.g., your live SSD production disk and a secondary magnetic storage server).
  • 1 copy offsite (preferably in a different datacenter).

For those of us hosting in the Nordics, there is an added layer: Data Sovereignty. With the Data Protection Directive (95/46/EC) and Norway's strict Personopplysningsloven, you cannot just dump your backups onto an unverified US server without risking legal exposure. We keep our offsite backups strictly within Norwegian or EEA borders.

The Meat: Scripting the Solution

Forget expensive enterprise bloatware. The most robust backups are built on standard Linux tools: rsync, mysqldump, and cron. They are transparent, auditable, and free.

1. Database Consistency is King

If you just copy the /var/lib/mysql folder while the database is running, you will get a corrupted mess. You need a logical dump. For InnoDB tables (which you should be using over MyISAM for crash recovery), the --single-transaction flag is critical. It ensures a consistent snapshot without locking the tables.

mysqldump -u root -p'YourComplexPassword' --all-databases --single-transaction --quick --lock-tables=false | gzip > /backups/mysql/db_$(date +%F).sql.gz
Pro Tip: Do not put your password in the command line if you have multi-user shell access; it shows up in `ps aux`. Create a `.my.cnf` file in your home directory with chmod 600 permissions.

2. The Filesystem Sync

Once the database is dumped, we sync the filesystem. We use rsync because it's bandwidth efficient—it only transfers the deltas (changes). This is crucial if you are pushing gigabytes of data over the wire.

Here is a robust script structure I use on CoolVDS instances:

#!/bin/bash

# Configuration
BACKUP_DIR="/var/backups/local"
REMOTE_USER="backup_user"
REMOTE_HOST="storage.coolvds.no"
REMOTE_DIR="/home/backup_user/storage"
DATE=$(date +%F)

# 1. Dump MySQL
echo "Starting DB Dump..."
mysqldump --defaults-extra-file=/root/.my.cnf --all-databases --single-transaction | gzip > $BACKUP_DIR/db_$DATE.sql.gz

# 2. Encrypt the Archive (Security First!)
# We use GPG because sending unencrypted customer data across the wire is negligent.
gpg --yes --batch --passphrase='YourSecretPassphrase' -c $BACKUP_DIR/db_$DATE.sql.gz
rm $BACKUP_DIR/db_$DATE.sql.gz # Remove unencrypted file

# 3. Rsync to Offsite Storage
# We use a custom SSH port (2222) for slight obscurity and keeping logs clean
echo "Syncing to remote..."
rsync -avz --delete -e "ssh -p 2222 -i /root/.ssh/id_rsa_backup" $BACKUP_DIR/ $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/

# 4. Cleanup old local files (keep last 7 days)
find $BACKUP_DIR -name "*.gpg" -mtime +7 -delete

echo "Backup Complete."

3. Automating with Cron

A script you have to run manually is a script you will forget. Add it to the root crontab. Run it during off-peak hours (usually 03:00 - 04:00 AM CET for Norwegian traffic).

0 3 * * * /bin/bash /root/scripts/nightly_backup.sh >> /var/log/backup.log 2>&1

The Impact of I/O on Backups

This is where hardware choice matters. Running mysqldump and rsync is I/O intensive. On traditional spinning HDDs (even SAS 15k drives), a backup job can cause "I/O Wait" to spike, making your web server sluggish for users. This is the "noisy neighbor" effect often seen in budget hosting.

This is why we architect CoolVDS on pure SSD storage. The random read/write speeds of Solid State Drives mean that backups can run in the background without degrading the performance of your Apache or Nginx processes. While some providers are still talking about caching layers to hide slow disks, we believe in raw throughput. When you have low latency storage, your backup window shrinks from hours to minutes.

Verification: The Step Everyone Skips

A backup is Schrödinger's file until you restore it. You do not know if it is alive or dead. Once a month, you must perform a fire drill:

  1. Spin up a fresh VPS (CoolVDS instances provision in about 55 seconds, which is perfect for this).
  2. Pull your backup file from the remote storage.
  3. Decrypt and import the database.
  4. Check if the application loads.

If you don't test your backups, you don't have backups; you have hope. And hope is not a strategy.

Network Considerations in Norway

When transferring data offsite, latency and routing matter. If your backup server is in Germany and your main server is in Oslo, you are traversing multiple hops. Ideally, you want your offsite backup to be geographically separated (to avoid a datacenter-wide fire issue) but network-adjacent. Using a provider with strong peering at NIX (Norwegian Internet Exchange) ensures that your bandwidth stays high and your transfer times stay low.

Feature Traditional Hosting CoolVDS Architecture
Storage Medium SATA / SAS HDD (High Latency) Enterprise SSD (Instant I/O)
Network Throughput 100 Mbps Shared 1 Gbps Port (Fast Sync)
Backup Impact High Load / Site Slowdown Negligible Impact

Conclusion

Disasters are inevitable. Hardware fails, power supplies explode, and humans make mistakes. The difference between a professional and an amateur is the recovery time.

Don't let slow I/O or weak scripts threaten your uptime. Ensure your infrastructure can handle the load of nightly backups without breaking a sweat. If you need a sandbox to test your recovery scripts, deploy a high-performance SSD instance on CoolVDS today and sleep better tonight.