From ecb099d5be01998810b096c9211c141f340ec0c8 Mon Sep 17 00:00:00 2001 From: Rod Wright Date: Wed, 11 Feb 2026 09:42:54 -0500 Subject: [PATCH] Initial commit --- etc/ribs/exclude_list | 7 ++ etc/ribs/ribs.conf | 28 +++++++ usr/local/bin/ribs.sh | 188 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 223 insertions(+) create mode 100644 etc/ribs/exclude_list create mode 100644 etc/ribs/ribs.conf create mode 100644 usr/local/bin/ribs.sh diff --git a/etc/ribs/exclude_list b/etc/ribs/exclude_list new file mode 100644 index 0000000..94e5999 --- /dev/null +++ b/etc/ribs/exclude_list @@ -0,0 +1,7 @@ +/archive/ +/backups/rescuebru-backups/ +/old_VirtualBox_files/ +/RescueBRU/ +/VirtualBox_VMs/ +/VMwareBackups/ +/VMwareDatastore/ diff --git a/etc/ribs/ribs.conf b/etc/ribs/ribs.conf new file mode 100644 index 0000000..c139a40 --- /dev/null +++ b/etc/ribs/ribs.conf @@ -0,0 +1,28 @@ +#!/bin/bash +# Configuration file for ribs.sh + +# Dry run option. Set to 0 for normal mode or 1 for testing. +DRY_RUN=0 + +# Remount option. Set to 0 to not remount or 1 to remount read-only. +REMOUNT=1 + +# Mount point of backup drive +BACKUP_ROOT="/mnt/incremental_backup_drive" + +# Backup device +BACKUP_DEV="/dev/sde" +BACKUP_PART=$BACKUP_DEV"1" + +# Backup device serial number (as reported by smartctl -a) +BACKUP_DEV_SN=W4Z1490W + +# File containing patterns to exclude +EXC_FILE="/etc/ribs/exclude_list" + +#Number of days of backups to keep +DAY_LMT=60 + +# source directory +SRC="/mnt/raid/" + diff --git a/usr/local/bin/ribs.sh b/usr/local/bin/ribs.sh new file mode 100644 index 0000000..97a8fd2 --- /dev/null +++ b/usr/local/bin/ribs.sh @@ -0,0 +1,188 @@ +#!/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