My Root (/) Tree Monthly Incremental Backup Script using rsync (so far)

I’ve spent much of this past day researching rsync, and deciding just what I wanted my script to do. For starters, I want my script to create a full backup on the first day of the month, and incremental backups on all the other days of the month. I want it to create the /backups directory if it doesn’t exist, and create a full backup if there are no existing backups, regardless the day of the month. Finally, I want my script to use the system notifications to pass messages, and the notify-send command handles that easily. The default command is notify-send “Title text” “Message body”, so since both of my messages can have the same title, but require a slightly different message body, depending on whether a full or incremental backup is running, I used variables for each of three components, the NOTIFICATION_TITLE, NOTIFICATION_MESSAGE1, and NOTIFICATION_MESSAGE2, each of which contains “Backup Status”, “Full backup completed on $DATE_FORMAT”, and “Incremental backup completed on $DATE_FORMAT”, respectively where $DATE_FORMAT is the variable for the date used to name the current backup directory [DATE_FORMAT=$(date +%Y-%m-%d)].

Here’s the script:

------------------------------------------------------------------------------

#!/bin/bash

Note:

# Make sure the destination directory $BACKUP_DIR exists,
# so this script will create it the first time we run a backup

mkdir -p /backups # Creates the backups directory under root if it does not already exist

# the backup destination’s parent directory
BACKUP_DIR=“/backups”
# Variable to name today’s backup destination directory
DATE_FORMAT=$(date +%Y-%m-%d)
# Today’s backup destination directory
CURRENT_LINK_DIR=“$BACKUP_DIR/$DATE_FORMAT”

# Variables for the title and notification messages
NOTIFICATION_TITLE=“Backup Status”
# Full backup completed notification message
NOTIFICATION_MESSAGE1=“Full backup completed on $DATE_FORMAT”
# Incremental backup completed notification message
NOTIFICATION_MESSAGE2=“Incremental backup completed on $DATE_FORMAT”

# AI defined this, but I don’t see it in use, I’m commenting it out as a test!
# PREVIOUS_LINK_DIR=“”

# Find the most recent backup directory for --link-dest
# The most recently dated folder
LATEST_BACKUP=$(ls -td “$BACKUP_DIR”/*/ | head -n 1)

if LATEST_BACKUP=“”; then
    # Full Backup (just copy everything)
    rsync -a --delete / “$CURRENT_LINK_DIR/” --exclude=/proc --exclude=/sys --/ exclude=/dev --exclude=/tmp --exclude=/run --exclude=/mnt --exclude=/media --exclude=/backups --exclude=/home

    # Send the full backup completed message
    notify-send “NOTIFICATION_TITLE” “NOTIFICATION_MESSAGE1”

    # Will delete this in final version
    # echo “Full backup completed on $DATE_FORMAT”
fi

if [ “$(date +%d)” -eq 1 ]; then
    # Full Backup (just copy everything)
    rsync -a --delete / “$CURRENT_LINK_DIR/” --exclude=/proc --exclude=/sys --/exclude=/dev --exclude=/tmp --exclude=/run --exclude=/mnt --exclude=/media --exclude=/backups --exclude=/home

    # Send the full backup completed message
    notify-send “NOTIFICATION_TITLE” “NOTIFICATION_MESSAGE1”

    # Will delete this in final version
    # echo “Full backup completed on $DATE_FORMAT”

else
    # Incremental Backup using --link-dest
    # Creates hard links to unchanged files from the previous backup
    rsync -a --delete --link-dest=“$LATEST_BACKUP” / “$CURRENT_LINK_DIR/” --/exclude=/proc --exclude=/sys --exclude=/dev --exclude=/tmp --exclude=/run --exclude=/mnt --exclude=/media --exclude=/backups --exclude=/home

    # Send the incremental backup completed message
    notify-send “NOTIFICATION_TITLE” “NOTIFICATION_MESSAGE2”
    # Will delete this in final version
    # echo “Incremental backup completed on $DATE_FORMAT”
fi
------------------------------------------------------------------------------


If anyone sees anything I’ve missed, or anything I’ve done wrong, please reply! :slight_smile:

Ernie

4 Likes

You should write a recovery script too. Test it , and have it ready.

You need more options than -a
Minimum would be -aHAXS

3 Likes