Linux Automatic Backups
Server Management

Automatic Backups on a Linux Server: A tar, rsync and cron Guide

Automatically back up files and databases on your Linux VDS or dedicated server with tar, rsync and cron; delete old backups and copy them to a remote server.

  • MerkezHost Technical Team
  • 5 min read
  • Level: Intermediate
  • Updated:

A disk failure, a mistyped command, a site broken by an update or an attack... The common solution to all of them is regular, tested backups. In this guide we build a system on a Linux VDS or dedicated server that automatically backs up web files and databases with tar, rsync and cron, deletes old backups and copies the backups to a remote server.

The 3-2-1 backup rule

A reliable backup strategy in a nutshell:

  • 3 copies of your data (1 original + 2 backups),
  • on 2 different media,
  • with 1 copy in a different location (another server or cloud storage).
Important

Keeping backups only on the same server doesn't count as a backup. If the disk fails or the server is compromised, the backups are lost too. In this guide we always copy the backups to a second server as well.

Step 1: Prepare the backup folder

sudo mkdir -p /backup/{files,db}
sudo chmod 700 /backup

Step 2: Archive web files with tar

Let's try the command manually first. The following command turns the /var/www directory into a dated, compressed archive:

sudo tar -czf /backup/files/www-$(date +%F).tar.gz -C / var/www

To check the contents of the archive:

tar -tzf /backup/files/www-$(date +%F).tar.gz | head

Step 3: Back up the databases

For database backups, instead of typing the password on the command line, keep it in a file that only root can read:

sudo nano /root/.my.cnf
[client]
user=yedek_kullanici
password=GucluBirSifre
sudo chmod 600 /root/.my.cnf

Back up all databases consistently into a single file:

sudo mysqldump --all-databases --single-transaction --routines --events | gzip > /backup/db/all-$(date +%F).sql.gz

For details on mysqldump options and restoring, see our MySQL backup guide.

Step 4: Create the backup script

Let's combine all the steps into a single script:

sudo nano /usr/local/bin/yedekle.sh
#!/bin/bash
set -euo pipefail

TARIH=$(date +%F)
HEDEF=/backup
SAKLA_GUN=7

# 1) Web files
tar -czf "$HEDEF/files/www-$TARIH.tar.gz" -C / var/www

# 2) Databases
mysqldump --all-databases --single-transaction --routines --events | gzip > "$HEDEF/db/all-$TARIH.sql.gz"

# 3) Delete old backups
find "$HEDEF" -type f -name "*.gz" -mtime +"$SAKLA_GUN" -delete

# 4) Copy to the remote server
rsync -az --delete -e "ssh -p 22" "$HEDEF/" yedek@YEDEK_SUNUCU_IP:/backup/$(hostname)/

echo "[$(date '+%F %T')] Yedekleme tamamlandı"
sudo chmod 700 /usr/local/bin/yedekle.sh

Step 5: Passwordless rsync connection to the remote server

So that no password is requested while the script runs, create an SSH key for the root user and add it to the backup server:

sudo ssh-keygen -t ed25519 -f /root/.ssh/yedek_key -N ""
sudo ssh-copy-id -i /root/.ssh/yedek_key.pub yedek@YEDEK_SUNUCU_IP

Update the rsync line in the script to use this key:

rsync -az --delete -e "ssh -i /root/.ssh/yedek_key -p 22" "$HEDEF/" yedek@YEDEK_SUNUCU_IP:/backup/$(hostname)/
Security tip

Create the yedek (backup) user on the backup server as a user without sudo privileges that can only write to the backup folder. For SSH hardening, see our SSH security guide.

Step 6: Automate with cron

sudo crontab -e

Add the following line to run it every night at 03:30 and log the output:

30 3 * * * /usr/local/bin/yedekle.sh >> /var/log/yedekle.log 2>&1
cron expressionMeaning
30 3 * * *Every day at 03:30
0 */6 * * *Every 6 hours
0 4 * * 0Every Sunday at 04:00
0 2 1 * *On the 1st of every month at 02:00

Without waiting, run the script once manually and confirm there are no errors:

sudo /usr/local/bin/yedekle.sh
ls -lh /backup/files /backup/db

Step 7: Test restoring

An untested backup is not a backup. Once a month, check your backups by extracting them into a separate folder:

mkdir -p /tmp/test-geri-yukleme
tar -xzf /backup/files/www-2026-10-20.tar.gz -C /tmp/test-geri-yukleme
ls /tmp/test-geri-yukleme/var/www

To check the integrity of the database backup:

gunzip -t /backup/db/all-2026-10-20.sql.gz && echo "Arşiv sağlam"

Common mistakes

  • Keeping backups on the same disk with no remote copy.
  • Backups silently failing when the disk fills up: monitor disk space with df -h.
  • Forgetting that cron uses a different environment (PATH): using full command paths in the script prevents problems.
  • Never testing a restore.

Conclusion

With this setup, your server automatically backs up web files and databases every night, cleans up backups older than 7 days and sends a copy to a remote server. You can use our cost-effective VDS servers as a second backup server.

Frequently Asked Questions

Is it enough to keep backups on the same server?

No. In the event of a disk failure or an attack, the backups can be lost too. Keep at least one copy on a different server or storage service.

How often should I take backups?

Daily backups are recommended for frequently changing sites and databases. Test at regular intervals that your backups can actually be restored.

What is the difference between rsync and scp?

rsync transfers only changed files, resumes interrupted transfers where they left off and can synchronise deletions. For regular backups, it is much more efficient than scp.

  • #backups
  • #backup
  • #rsync
  • #cron
  • #tar
  • #linux
Share:
Read More

Related Guides