Skip to content

Commit

Permalink
Merge pull request #6 from CookieCrumbs19212/update-03-09
Browse files Browse the repository at this point in the history
Updated Scribe script and implemented new features.
  • Loading branch information
n44h authored Sep 8, 2022
2 parents ab434e1 + c65d0c5 commit a992ed8
Show file tree
Hide file tree
Showing 11 changed files with 850 additions and 292 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
scripts/test.sh
NOTE.txt
test.sh
conf/
test2.sh
.shellcheckrc
31 changes: 0 additions & 31 deletions config/configurations.conf

This file was deleted.

27 changes: 27 additions & 0 deletions config/scribe.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Scribe Config File

###
# User-set variables:

# Number of backup that can be stored at a time. If limit is exceeded, oldest backup is deleted.
BACKUP_LIMIT=3

# Custom prefix to prepend to the backup filenames. e.g. your username.
FILENAME_PREFIX=""


###
# Action control variable:

# When true, logs the verbose outputs of the tar command.
LOG_TAR_VERBOSE=false

# Exclude the script files from the backup (recommended).
EXCLUDE_SCRIPT_FILES=true


###
# Storage Location for Backups:

# Destination where the backup file will be saved.
BACKUP_LOC=""
File renamed without changes.
Empty file removed logs/tar_errors.log
Empty file.
241 changes: 241 additions & 0 deletions scribe.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
#! /bin/bash

# shellcheck source=scripts/utils.sh
# shellcheck source=scripts/setters.sh
# shellcheck source=scripts/backup.sh
# shellcheck source=config/scribe.conf


# Paths to directories.
CONFIG_DIR="config"
LOG_DIR="logs"
SCRIPT_DIR="scripts"

# Critical file locations.
CONFIG_FILE="${CONFIG_DIR}/scribe.conf"
BACKUP_LIST="${CONFIG_DIR}/backup_list.conf"
EXCLUDE_LIST="${CONFIG_DIR}/exclude_list.conf"

# Log file locations.
MAIN_LOG="${LOG_DIR}/backups.log"
TAR_VERBOSE_LOG="${LOG_DIR}/tar_verbose.log"
TAR_ERROR_LOG="${LOG_DIR}/tar_error.log"

# Helper Scripts locations.
UTILS="${SCRIPT_DIR}/utils.sh"
SETTERS="${SCRIPT_DIR}/setters.sh"
BACKUP_FUNCS="${SCRIPT_DIR}/backup.sh"


# Source the utils.sh script.
. "$UTILS"


# If the logs folder does not exist, create it.
if [[ ! -d $LOG_DIR ]]; then
# Making the logs directory.
mkdir $LOG_DIR
fi

# Creating an array of the files inside the logs/ directory.
LOG_FILES_ARRAY=("$MAIN_LOG" "$TAR_VERBOSE_LOG" "$TAR_ERROR_LOG")
# Running a loop to create the log files if they do not exist.
for file in "${LOG_FILES_ARRAY[@]}"
do
# If the log file does not exist, create it.
if [[ ! -f $file ]]; then
# Create the log file.
touch "$file"
fi
done


# Start logging from this point.
log -b


# If the config folder does not exist, create it.
if [[ ! -d $CONFIG_DIR ]]; then
log -e "config/ directory not found"

# Making the config directory.
mkdir $CONFIG_DIR && log -d "Created config/ directory"
fi

# Creating an array of the files inside the config/ directory.
CONFIG_FILES_ARRAY=("$CONFIG_FILE" "$BACKUP_LIST" "$EXCLUDE_LIST")
# Running a loop to create the config files if they do not exist.
for file in "${CONFIG_FILES_ARRAY[@]}"
do
# If the config file does not exist, create it.
if [[ ! -f $file ]]; then
log -e "${file} file is missing"

# Create the config file.
touch "$file" && log -d "Created ${file}"
fi
done

# If the config file is empty, set defaults.
if [[ ! -s $CONFIG_FILE ]]; then
log -e "scribe.conf file is empty"

# Setting to defaults.
reset_config_defaults

log -d "Reset configurations to default"

# Writing to the config file.
write_to_config_file
fi


# Source the config file.
. "$CONFIG_FILE"

# Source the setters.sh script.
. "$SETTERS"

# Source the backup.sh script.
. "$BACKUP_FUNCS"


# Analyzing command.
case "$1" in

# Set the limit for maximum number of backups.
set-limit)
set_backup_limit "$2"
;;

# Set the filename prefix that will be prepended to the backup file's name.
set-prefix)
set_filename_prefix "$2"
;;

# Set the backup destination, i.e. the location where the backup files will be stored.
set-loc)
set_backup_location "$2"
;;


# Turn on tar verbose logging.
tar-verbose-on)
LOG_TAR_VERBOSE=true
write_to_config_file
;;

# Turn off tar verbose logging.
tar-verbose-off)
LOG_TAR_VERBOSE=false
write_to_config_file
;;


# Exclude the Scribe script files from the backup.
exclude-script)
EXCLUDE_SCRIPT_FILES=true
write_to_config_file
;;

# Include the Scribe script files in the backup.
include-script)
EXCLUDE_SCRIPT_FILES=false
write_to_config_file
;;



# Add a path to the backup list.
add | include)
add_to_backup_list "$2"
;;

# Remove a path from the backup list.
remove | remove-backup)
remove_path_from_backup_list "$2"
;;


# Add a path to the exclude list.
exclude)
add_to_exclude_list "$2"
;;

# Remove a path from the exclude list.
remove-exclude)
remove_path_from_exclude_list "$2"
;;


# Show backup location.
--backup-loc)
if [[ "$BACKUP_LOC" == "" ]]; then
echo "Backup location has not been set. Set Backup location using \"set-loc <path>\""
else
echo "Backup location: $BACKUP_LOC"
fi
;;

# Show backup limit.
--backup-lim)
echo "Backup limit is: $BACKUP_LIMIT"
;;


# List the paths in the backup list.
ls-backup)
print_backup_list
;;

# List the paths in the exclude list.
ls-exclude)
print_exclude_list
;;


# Clear the backup list.
clr-backup)
# Confirmation prompt.
read -r -p "Are you sure you want to clear the backup list? [N/y]: " response
if [[ "$response" =~ ^[Yy](es)?$ ]]; then
# Clear the BACKUP_LIST
truncate -s 0 "$BACKUP_LIST" && log -i "Cleared backup list"
fi
;;

# Clear the exclude list.
clr-exclude)
# Confirmation prompt.
read -r -p "Are you sure you want to clear the exclude list? [N/y]: " response
if [[ "$response" =~ ^[Yy](es)?$ ]]; then
# Clear the BACKUP_LIST
truncate -s 0 "$EXCLUDE_LIST" && log -i "Cleared exclude list"
fi
;;

# Clear all the log files.
clr-logs)
clear_logs
;;


# Create a backup.
backup)
perform_pre_backup_checks
create_backup
;;


# Reset the configurations to default values.
reset)
reset_config_defaults
clear_logs
;;


# Invalid commands.
*)
echo "Invalid command"
;;
esac
Loading

0 comments on commit a992ed8

Please sign in to comment.