Full Tutorial: How to Set Up Automatic Backups on Linux Server (Files + Database)
1. ✅ Why Automatic Backups Are Important
-
Servers can crash — hardware failures, power loss, bugs.
-
Human error — mistakes happen (wrong command, delete by accident).
-
Updates or migrations sometimes go wrong.
-
Automatic backups = safety net. You sleep better knowing data is safe.
If you manage a website, application data, database — backups must be part of your routine.
2. 🛠️ Tools You Need (Nothing Fancy)
-
A Linux server (Ubuntu / Debian / similar).
-
Access to the terminal (SSH).
-
Basic permissions (should allow creating and scheduling scripts).
-
Enough disk space or external backup storage (local folder, remote server, cloud, etc.).
-
Ability to schedule jobs — we’ll use
cron.
No paid software needed — everything uses open-source tools built into Linux.
3. 💾 Backup Files — Create a Backup Script
Create a folder for your backup scripts, e.g.:
Create a file named backup_files.sh:
Save the file and make it executable:
📄 Explanation (Beginner-friendly):
-
tar -czf ...→ compress and archive your folder into a.tar.gz. -
date +...→ stamps the backup file with current date/time (so every backup is unique). -
find ... -mtime +30 -exec rm {}→ deletes backup files older than 30 days (so you don’t run out of space).
This simple script is all you need to backup a folder properly.
4. 🗄️ Backup Database (e.g. MySQL/MariaDB) — Script Example
If your server uses MySQL or MariaDB:
Create another script backup_db.sh:
Make executable:
📝 Explanation:
-
mysqldumpexports the entire database to a text SQL file. -
Piped to
gzipto compress (saves disk space). -
Backup file named with timestamp for uniqueness.
-
Old backups automatically removed after 14 days.
5. 📅 Automate with Cron — Schedule Backups
Open your crontab:
Then add lines (for example):
This will run the file‐backup at 02:00, and the database backup at 03:00, every day.
✅ Why this matters:
-
You don’t need to remember — backups run automatically.
-
Even if server crashes at night — your data is safe.
-
You get regular snapshots you can restore from.
6. 🔐 (Optional but Recommended) — Store Backups Off-site or Remote
Local backups are good — but what if your server disk fails?
Better: copy backups to another server or cloud storage.
Example using rsync → remote server:
You can add this to the end of your backup script.
Or use cloud storage (S3, Dropbox, etc.) — but that’s advanced (for another tutorial).
7. 🛠️ How to Restore from Backup (Files & Database)
Restore files:
Restore database (MySQL example):
Always test restores on a staging server first — to ensure backups are valid.
✅ Best Practices & Tips
-
Always store backups outside original folder (don’t backup inside server root).
-
Use timestamps to avoid overwriting old backups.
-
Rotate and delete old backups — avoid disk full.
-
Secure backup folder (permissions, maybe encryption) if data sensitive.
-
Test backups regularly — a backup that can't be restored is useless.
-
Write logs (optional) — useful to track what happened and when.