Skip to content

Ubuntu Server Backups⚓︎

Summary⚓︎

This article contains the backup script and the cleanup script for my Xenlab server.

Backup Script⚓︎

The backup script is located at /usr/local/bin/backup.sh.

/usr/local/bin/backup.sh

#!/bin/bash
####################################
#
# Backup to NFS mount script.
#
####################################

# What to backup.
backup_files="/home /var/spool/cron /etc/apt"

# Where to backup to.
dest="/mnt/Backup/Ubuntu-Server"

# Create archive filename.
hostname=$(hostname -s)
timestamp=$(date +'%Y-%m-%d')
archive_file="$hostname-$timestamp.tgz"

# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo

# Backup the files using tar.
tar czf $dest/$archive_file $backup_files

# Print end status message.
echo
echo "Backup finished"
date

# Long listing of files in $dest to check file sizes.
ls -lh $dest

Crontab⚓︎

sudo crontab -e

## Weekly Backup
0 11 * * 0 bash /usr/local/bin/backup.sh && curl -fsS -m 10 --retry 5 -o /dev/null https://hc-ping.com/65b29ce8-7ee4-45a9-a10a-d0a18635a74e

## Monthly Backup Cleanup
30 3 1 * * find /mnt/Backup/Ubuntu-Server -name "*tgz" -type f -mtime +30 -delete && curl -fsS -m 10 --retry 5 -o /dev/null https://hc-ping.com/245f537e-9aa8-4411-b6b0-551bc4cd6446

Specifics⚓︎

  • The backup script can be found on my Xenlab server at /usr/local/bin/backup.sh.
  • It runs weekly on Sunday at 11am.
  • Backups are sent to the /mnt/Backup/Ubuntu-Server directory, which corresponds to the /volume2/Files/Ubuntu-Server directory on my NAS.
  • Backups are retained for 30 days.
  • A cleanup script is run monthly on the 1st day of each month at 3:30am to purge any archive files over 30 days old.