Backup Notification after finish


Recommended Posts

Hi there

 

I'm looking for a notification code to add in my backup script which I found on this site. By the way it works like a charm. I really recommend it to anybody who looks for a backup solution like apples time machine.

 

Would like to have to messages:

 

1. after finished without problems

2. if there is a problem, with information about the problem

 

Sorry, I'm not a coder. Can anyone help me please?

 

Thanks in advance.

greg

#!/bin/bash
#
# Backup mimicking Time Machine from Mac OS X using rsync
# --- Variables --- #

OS=$(uname -s)
HOST=$(hostname)
DATE_FORMAT=$(date "+%Y-%m-%d-%H%M%S")
CURRENT_YEAR=$(date +%Y)
CURRENT_MONTH=$(date +%m)
RSYNC_OPTIONS="--archive --partial --progress --human-readable"

# Use absolute paths. Relative paths tend to break the hard linking advantage of rsync.
# Paths can include spaces as long as variable contents are double quoted
SOURCE="/mnt/user/googledrive_resilio_sync/Sounds"
DESTINATION="/mnt/disks/backup_SAM_2876_sounds/Sounds"

# --- Main Program --- #

# Create destination if it does not exist
if [[ ! -d "$DESTINATION" ]] ; then
  mkdir -p "$DESTINATION"
fi

# Make inital backup if Latest does not exist, otherwise only copy what has changed
# and hard link to files that are the same
if [[ ! -L "$DESTINATION"/Latest ]] ; then
  rsync $RSYNC_OPTIONS \
                --delete \
                "$SOURCE" "$DESTINATION"/$DATE_FORMAT
else
  rsync $RSYNC_OPTIONS \
               --delete \
               --delete-excluded \
               --link-dest="$DESTINATION"/Latest \
               "$SOURCE" "$DESTINATION"/$DATE_FORMAT
fi

# Remove symlink to previous Latest backup
rm -f "$DESTINATION"/Latest

# Create symlink to latest backup
ln -s $DATE_FORMAT "$DESTINATION"/Latest



# --- Remove old backups --- #

# BSD date in OS X has a different syntax than GNU date in Linux
if [[ $OS == "Darwin" || $OS == "FreeBSD" ]]; then

  # Return YYYY one year ago from today
  LAST_YEAR=$(date -v -1y "+%Y")

elif [[ $OS == "Linux" ]]; then

  # Return YYYY one year ago from today
  LAST_YEAR=$(date -d "last year" "+%Y")

fi


# Keep monthly backups for one year
for (( month = 1 ; month < $CURRENT_MONTH ; month++ )); do
  # List latest backup from each month of current year
  # Use printf to pad the single digit months with a 0
  LATEST_BACKUP=$(find "$DESTINATION" -mindepth 1 -maxdepth 1 -name ${CURRENT_YEAR}-$(printf "%02d" $month)-* | sort | tail -n 1)
  find "$DESTINATION" -mindepth 1 -maxdepth 1 -name ${CURRENT_YEAR}-$(printf "%02d" $month)-* | grep -v "$LATEST_BACKUP" | xargs -I {} rm -rf {}
done

for (( month = $CURRENT_MONTH ; month <= 12 ; month++ )); do
  # List latest backup from each month of current year
  # Use printf to pad the single digit months with a 0
  LATEST_BACKUP=$(find "$DESTINATION" -mindepth 1 -maxdepth 1 -name ${LAST_YEAR}-$(printf "%02d" $month)-* | sort | tail -n 1)
  find "$DESTINATION" -mindepth 1 -maxdepth 1 -name ${LAST_YEAR}-$(printf "%02d" $month)-* | grep -v "$LATEST_BACKUP" | xargs -I {} rm -rf {}
done


# Remove backups older than one year
for (( month = 1 ; month < $CURRENT_MONTH ; month++ )); do
  find "$DESTINATION" -mindepth 1 -maxdepth 1 -type d -name "$LAST_YEAR-$(printf "%02d" $month)-*" | xargs -I {} rm -rf {}
done

find "$DESTINATION" -mindepth 1 -maxdepth 1 -type d ! -name "$CURRENT_YEAR-*" | grep -v "$LAST_YEAR-*" | xargs -I {} rm -rf {}

 

 

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.