QR CookingNotes

CookingNotes

Your Personal Recipe Book

Get it on Google Play
QR FiNoteMe

FiNoteMe

Smart Finance Tracker

Get it on Google Play
database Losing important data because of server crashes, accidental deletions, or corruption is one of the worst fears for anyone hosting a website or service. The best protection? Setting up automatic backups — both for your files and databases — on your Linux server. In this guide, you'll learn how to configure backup scripts + cron jobs so backups run automatically, plus how to store backups securely, rotate them, and restore data when needed.

Full Tutorial: How to Set Up Automatic Backups on Linux Server (Files + Database)

5 Min Read Verified Content

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.:

mkdir -p ~/backup-scripts cd ~/backup-scripts

Create a file named backup_files.sh:

#!/bin/bash # Variables SRC="/var/www/html" # folder you want to backup DEST="/home/username/backups" # backup destination folder DATE=$(date +%Y-%m-%d_%H-%M-%S) FILENAME="backup-files-$DATE.tar.gz" # Create backup tar -czf "$DEST/$FILENAME" "$SRC" # Optional: delete backups older than 30 days find "$DEST" -type f -name "backup-files-*.tar.gz" -mtime +30 -exec rm {} \;

Save the file and make it executable:

chmod +x backup_files.sh

📄 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:

#!/bin/bash # Variables DB_NAME="myapp" DB_USER="root" DB_PASS="yourpassword" DEST="/home/username/backups" DATE=$(date +%Y-%m-%d_%H-%M-%S) FILENAME="backup-db-$DB_NAME-$DATE.sql.gz" # Dump database and compress mysqldump -u $DB_USER -p$DB_PASS $DB_NAME | gzip > "$DEST/$FILENAME" # Optional: delete backups older than 14 days find "$DEST" -type f -name "backup-db-*.sql.gz" -mtime +14 -exec rm {} \;

Make executable:

chmod +x backup_db.sh

📝 Explanation:

  • mysqldump exports the entire database to a text SQL file.

  • Piped to gzip to 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:

crontab -e

Then add lines (for example):

# backup files every day at 02:00 0 2 * * * /home/username/backup-scripts/backup_files.sh # backup database every day at 03:00 0 3 * * * /home/username/backup-scripts/backup_db.sh

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:

rsync -avz /home/username/backups/ user@remote-server:/remote-backups/

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:

tar -xzf backup-files-2025-12-08_02-00-00.tar.gz -C /var/www/html

Restore database (MySQL example):

gunzip < backup-db-myapp-2025-12-08_03-00-00.sql.gz | mysql -u root -p myapp

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.

Advertisement
Back to Database