The Paranoia Protocol: Automating Linux Server Backups Before Disaster Strikes
I have a simple rule for every junior sysadmin that joins my team: If the backup isn't automated, it doesn't exist.
Last month, a client came to me with a corrupted RAID array on a legacy server hosted in a bargain-bin data center in Germany. They swore they had backups. They did—on the same physical drive that just melted. We spent 72 hours scraping data off platters. It cost them a fortune, and it cost me my weekend.
Hardware failure is not a probability; it is a certainty. Whether you are running a high-traffic e-commerce site on Magento or a critical email gateway, the only thing standing between you and total insolvency is a robust, automated backup script. Today, we are going to write one that works, focusing on the standard tools available on any robust Linux system: Bash, Rsync, and Cron.
The Anatomy of a Bulletproof Backup
In the Norwegian hosting market, we have strict requirements. It’s not just about uptime; it's about the Personopplysningsloven (Personal Data Act). Losing customer data isn't just embarrassing; it draws the attention of Datatilsynet. To satisfy both technical and legal requirements, we adhere to the 3-2-1 Rule:
- 3 copies of your data.
- 2 different media types (e.g., fast SSD on the live server, magnetic storage on the backup node).
- 1 copy offsite (physically separated from your primary data center).
Step 1: Database Consistency is Key
Most novice scripts just copy the raw /var/lib/mysql directory. This is useless if the database is running. You end up with corrupted tables. You need a proper dump. For MySQL 5.5 (standard on CentOS 6 and Ubuntu 12.04), you must use --single-transaction for InnoDB tables to ensure you don't lock up your website while the backup runs.
The Database Dump Script
Create a file named backup_db.sh:
#!/bin/bash
# Configuration
DB_USER="root"
DB_PASS="StrongPassword123!"
BACKUP_DIR="/backup/mysql"
DATE=$(date +%Y-%m-%d_%H%M)
# Ensure backup directory exists
mkdir -p $BACKUP_DIR
# Dump all databases
# We use --single-transaction to avoid locking InnoDB tables
# We use --quick to retrieve rows one by one instead of buffering the whole set
/usr/bin/mysqldump -u$DB_USER -p$DB_PASS --all-databases --single-transaction --quick --events | gzip > $BACKUP_DIR/full_dump_$DATE.sql.gz
# Delete backups older than 7 days to save space
find $BACKUP_DIR -type f -name "*.sql.gz" -mtime +7 -exec rm {} \;
# Log the result
echo "Database backup completed for $DATE" >> /var/log/backup_ops.log
Pro Tip: On CoolVDS instances, we recommend utilizing the secondary virtual disk for your local backup staging area. This segregates I/O operations. If your primary OS disk thrashes during a high-load event, your backup partition remains accessible.
Step 2: Filesystem Snapshots with Rsync
Tarballs are great, but they are heavy. If you have 50GB of user data, creating a new tarball every night consumes massive CPU resources. A better approach for the file system is an incremental sync.
We use rsync. It creates a mirror of your data. However, for a true history, we can use rsync with hard links, or simply rotate tarballs for the critical configurations (like /etc, /var/www).
Here is a robust file backup command that excludes garbage data:
tar -czpf /backup/files/web_files_$(date +%F).tar.gz \
--exclude='/var/www/vhosts/*/tmp' \
--exclude='/var/www/vhosts/*/logs' \
/var/www/vhosts /etc/nginx /etc/httpd /etc/php.ini
Step 3: The Offsite Transport
Storing backups locally on your VPS is arguably worse than no backups at all—it gives you a false sense of security. If the hypervisor node catches fire or the filesystem corrupts, you lose both the live site and the backup.
You need to push this data out. Since we are dealing with sensitive Norwegian business data, we cannot just FTP it in plain text. We use rsync over SSH.
#!/bin/bash
REMOTE_USER="backupuser"
REMOTE_HOST="backup.coolvds-storage.no"
LOCAL_DIR="/backup/"
REMOTE_DIR="/home/backupuser/incoming/"
# Sync to remote server securely
rsync -avz -e "ssh -p 2222" --delete $LOCAL_DIR $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR
This command uses the -z flag for compression, which is vital when you are paying for bandwidth or dealing with limited throughput locally.
Step 4: Encryption (The Compliance Layer)
Before any data leaves your server, it should be encrypted. If you are uploading to a cloud storage bucket or a remote FTP, you cannot trust the medium. We use GPG (GnuPG) for this.
# Encrypt the tarball before transfer
gpg --yes --batch --passphrase="YourSecretKey" -c /backup/files/web_files_$(date +%F).tar.gz
This produces a .gpg file. Even if someone intercepts your transmission, the data remains unreadable.
Why Infrastructure Matters
You can write the best scripts in the world, but the underlying architecture limits you. In 2012, we are seeing a massive shift from standard HDD to SSD caching and pure SSD setups.
When you run `mysqldump` on a standard SATA drive, the I/O wait can skyrocket, causing your website to load slowly for users during the backup window. This is known as the "backup freeze."
This is where CoolVDS differs from the budget providers. By utilizing KVM virtualization (Kernel-based Virtual Machine) instead of the older OpenVZ containers, we ensure that your I/O resources are dedicated. Furthermore, our storage arrays utilize enterprise-grade SSD caching. This means your nightly backups complete in minutes, not hours, drastically reducing the window of vulnerability.
Comparison: Backup Impact on Latency
| Storage Type | Backup Size | Time to Dump | Site Latency Impact |
|---|---|---|---|
| Standard SATA (7.2k RPM) | 10 GB | ~14 mins | High (Slow page loads) |
| CoolVDS (SSD Cached) | 10 GB | ~3 mins | Negligible |
Automation with Cron
Finally, tie it all together. Edit your crontab using crontab -e.
# Run full backup at 3:00 AM Norway time when traffic is low
0 3 * * * /root/scripts/full_backup_sequence.sh > /dev/null 2>&1
Conclusion
Data loss is an extinction event for modern businesses. Don't rely on manual processes or the hope that hardware lasts forever. By scripting your persistence layer with standard tools, you retain control.
If you are looking for a host that supports custom kernels, high-speed encrypted connections, and low-latency access to Oslo, you need a serious platform. Don't let slow I/O kill your restoration time.
Deploy your disaster recovery plan on a high-performance CoolVDS instance today.