Files
RIBS/usr/local/bin/ribs.sh
2026-02-11 09:42:54 -05:00

189 lines
4.6 KiB
Bash

#!/bin/bash
# Full path of configuration file
CONF_FILE="/etc/incremental_backup/incremental_backup.conf"
echo "----------------- RIBS ----------------"
echo "--- Rod's Incremental Backup System ---"
echo ""
# Read the configuration file
echo -n "Reading configuration file... "
source $CONF_FILE
if [ $? -ne 0 ]; then
echo -e "\e[31mFAILED\e[0m"
echo "Aborting."
exit 1
else
echo -e "\e[32mOK\e[0m"
fi
# Today's date
DAY0=`date -I`
echo "Today's date: $DAY0"
# Previous backup's date
DAY1=`ls $BACKUP_ROOT | grep -v lost+found | tail -n1`
if [ "$DAY1" == "" ]; then
DAY1=`date -I -d "yesterday"`
echo "No previous backup date. Using yesterday's date."
elif [ "$DAY1" == "$DAY0" ]; then
DAY1=`ls $BACKUP_ROOT | grep -v lost+found | tail -n1 | head -n1`
echo "Previous backup date: $DAY1"
else
echo "Previous backup date: $DAY1"
fi
# target directory
TRG="$BACKUP_ROOT/$DAY0"
# link destination directory:
LNK="$BACKUP_ROOT/$DAY1"
# rsync options
OPT="-ah --delete --link-dest=$LNK --exclude-from=$EXC_FILE"
if [ "$DRY_RUN" -eq 1 ]; then
OPT="$OPT -n"
fi
echo -n "Run started "
date
echo ""
# -------- Start backup -------
echo "Starting incremental backup."
if [ "$DRY_RUN" -eq 1 ]; then
echo "This will be a dry run. Nothing will actually be backed up."
sleep 10
fi
# Check to see if the backup drive is connected
echo -n "Checking if device connected... "
if [ ! -b "$BACKUP_DEV" ]; then
echo -e "\e[31mFAILED\e[0m"
echo "The backup device is not present."
echo "Check to make sure it's connected and powered on and that the"
echo "device name in $CONF_FILE is correct."
echo "Aborting."
exit 1
else
echo -e "\e[32mOK\e[0m"
fi
# Check that the backup drive serial number is correct
echo -n "Checking device serial number... "
dev_sn=`smartctl -a $BACKUP_DEV|grep "Serial Number:"|cut -d: -f2| sed 's/[[:space:]]//g'`
if [ "$dev_sn" != "$BACKUP_DEV_SN" ]; then
echo -e "\e[31mFAILED\e[0m"
echo "The backup device that is attached does not have the correct"
echo "serial number. Make sure the correct device is connected and"
echo "check that the serial number in $CONF_FILE"
echo "matches the serial number of the device as reported by"
echo "smartctl -a $BACKUP_DEV"
echo "Aborting."
exit 1
else
echo -e "\e[32mOK\e[0m"
fi
# Mount the backup drive
# First, check to see that drive is mounted
mount|grep -q $BACKUP_PART
if [ $? -ne 0 ]; then
echo -n "Backup device partition not mounted. Mounting read-only... "
mount $BACKUP_PART $BACKUP_ROOT -o ro
if [ $? -ne 0 ]; then
echo -e "\e[31mFAILED\e[0m"
echo "An error was encountered while attempting to mount"
echo "$BACKUP_PART on $BACKUP_ROOT"
echo "Aborting."
exit 1
else
echo -e "\e[32mOK\e[0m"
fi
fi
# Next, if it's mounted ro, remount it rw
mount|grep $BACKUP_PART |grep -q "(rw)"
if [ $? -ne 0 ]; then
echo "Backup device partition mounted read-only."
echo -n " Remounting read-write... "
mount $BACKUP_PART $BACKUP_ROOT -o remount,rw
if [ $? -ne 0 ]; then
echo -e "\e[31mFAILED\e[0m"
echo "An error was encountered while attempting to mount"
echo "$BACKUP_PART on $BACKUP_ROOT in read-write mode."
echo "Aborting."
exit 1
else
echo -e "\e[32mOK\e[0m"
fi
else
echo "Backup device partition already mounted read-write. Continuing"
fi
# Execute the backup
echo -n "running rsync backup... "
rsync $OPT $SRC $TRG
if [ $? -ne 0 ]; then
echo -e "\e[31mFAILED\e[0m"
echo "An error was encountered while attempting to backup. No old"
echo "backups will be removed."
if [ "$REMOUNT" -eq 1 ]; then
mount $BACKUP_PART $BACKUP_ROOT -o remount,ro
else
umount $BACKUP_ROOT
fi
exit 1
else
echo -e "\e[32mOK\e[0m"
fi
# Date of $DAY_LMT
DAY_TO_REMOVE=`date -I -d "$DAY_LMT days ago"`
# Delete the specified backup, if it exists
if [ "$DRY_RUN" -eq 1 ]; then
echo "This is a dry run, so no old backups will be removed."
else
if [ -d $BACKUP_ROOT/$DAY_TO_REMOVE ]; then
echo -n "Removing oldest backup... "
rm -r $BACKUP_ROOT/$DAY_TO_REMOVE
if [ $? -ne 0 ];then
echo -e "\e[31mFAILED\e[0m"
else
echo -e "\e[32mOK\e[0m"
fi
fi
fi
# Unmount the backup drive and exit
echo -n "Syncing disks... "
sync
if [ $? -ne 0 ];then
echo -e "\e[31mFAILED\e[0m"
else
echo -e "\e[32mOK\e[0m"
fi
if [ "$REMOUNT" -eq 1 ]; then
echo -n "Remounting backup device read-only... "
mount $BACKUP_PART $BACKUP_ROOT -o remount,ro
if [ $? -ne 0 ]; then
echo -e "\e[31mFAILED\e[0m"
else
echo -e "\e[32mOK\e[0m"
fi
else
echo -n "Unmounting backup device... "
umount $BACKUP_PART
if [ $? -ne 0 ]; then
echo -e "\e[31mFAILED\e[0m"
else
echo -e "\e[32mOK\e[0m"
fi
fi
echo "Backup complete."
echo ""
echo -n "Run ended "
date
echo ""
exit 0