From 5e578142056e41ff9200347a014f4dda5aeccdac Mon Sep 17 00:00:00 2001 From: thoralmighty Date: Wed, 28 Feb 2024 11:04:57 +0100 Subject: [PATCH] Added snippets and backup scripts --- backup/all-subdirs.sh | 41 +++++++++++++++ backup/backup2cloud.sh | 93 +++++++++++++++++++++++++++++++++ snippets/human_readable_size.sh | 15 ++++++ snippets/sizetest.sh | 9 ++++ 4 files changed, 158 insertions(+) create mode 100755 backup/all-subdirs.sh create mode 100755 backup/backup2cloud.sh create mode 100755 snippets/human_readable_size.sh create mode 100755 snippets/sizetest.sh diff --git a/backup/all-subdirs.sh b/backup/all-subdirs.sh new file mode 100755 index 0000000..452c5bc --- /dev/null +++ b/backup/all-subdirs.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +if [ "$#" -eq 0 ]; then + echo "Error: Please provide the source directory as an argument." + exit 1 +fi + +basedir="${1%/}" + +echo "Found these directories:" +for dir in $basedir/*; do + if [ -d "$dir" ]; then + echo " - $dir" + fi +done +read -n1 -p "Back them up? (y/n): " answer +echo + +if [ "$answer" != "y" ]; then + echo "Exiting." + exit 1 +fi + +for dir in $basedir/*; do + if [ -d "$dir" ]; then + echo -e "\e[1;36mProcessing folder: $dir\e[0m" + ./backup2cloud.sh $dir + + if [ $? -ne 0 ]; then + echo -en "\e[1;31mLast run failed. Do you want to continue? (y/n): \e[0m" + read -n1 answer + echo + if [ "$answer" != "y" ]; then + echo "Exiting." + exit 1 + fi + fi + fi +done + +echo -e "\e[1;32mCompleted\e[0m" diff --git a/backup/backup2cloud.sh b/backup/backup2cloud.sh new file mode 100755 index 0000000..5473677 --- /dev/null +++ b/backup/backup2cloud.sh @@ -0,0 +1,93 @@ +#!/bin/bash + +# ensure prerequisites are installed: rclone p7zip-full +# run `rclone config` to set up your target, put the name you choose as REMOTE_NAME +# edit .env and add an ENC_PASSWORD used for encryption, can also put variable overrides there + +# the directory name will be used as archive name by default +# usage: script.sh [path] [archive name override] + +# Default values +SOURCE_DIR="$1" +CURRENT_DATE_TIME=$(date +"%Y%m%d_%H%M%S_%s") +REMOTE_NAME="onedrive" +REMOTE_DIR="Backups/" + +# Load environment variables from .env file +source .env + +# Check if TEMP_DIR is set +if [ -z "$TEMP_DIR" ]; then + echo "Error: TEMP_DIR environment variable is not set." + exit 1 +else + echo "Temporary directory: $TEMP_DIR" + sleep 1 +fi + +DESTINATION_DIR=$(mktemp -d --tmpdir=$TEMP_DIR) + +# Check if ENC_PASSWORD is set +if [ -z "$ENC_PASSWORD" ]; then + echo "Error: ENC_PASSWORD environment variable is not set." + exit 1 +fi + +# Check if source directory is provided as an argument +if [ "$#" -eq 0 ]; then + echo "Error: Please provide the source directory as an argument." + exit 1 +fi + +# Check if source directory exists +if [ ! -d "$SOURCE_DIR" ]; then + echo "Error: Source directory '$SOURCE_DIR' does not exist." + exit 1 +fi + +if [ "$SOURCE_DIR" == "." ]; then + SOURCE_DIR=$(pwd) +fi + +SOURCE_DIR_NAME=$(basename "$SOURCE_DIR") +ARCHIVE_NAME="${SOURCE_DIR_NAME}" + +# Use the provided archive name if the second parameter is present +if [ "$#" -eq 2 ]; then + ARCHIVE_NAME="$2" +fi + +ARCHIVE_NAME="$(hostname)_${ARCHIVE_NAME}_${CURRENT_DATE_TIME}" +ARCHIVE_FILE="$DESTINATION_DIR/${ARCHIVE_NAME}.7z" + +# Function to clean up temporary files on exit +cleanup() { + echo "Cleaning up at: $DESTINATION_DIR" + rm -rf "$DESTINATION_DIR" +} + +echo "Temporary directory: $DESTINATION_DIR" + +trap cleanup EXIT + +# Archive, compress, and encrypt the directory +7z a -t7z -m0=lzma2 -mx=9 -mhe=on -p"$ENC_PASSWORD" "$ARCHIVE_FILE" "$SOURCE_DIR" + +if [ $? -eq 0 ]; then + echo "Archiving successful. Beginning upload." +else + echo "Error ($?): Archiving failed. Check path permissions." + exit 1 +fi + +# Upload to OneDrive using rclone +rclone copy "$ARCHIVE_FILE" "$REMOTE_NAME:$REMOTE_DIR" -P + +# Check if the upload was successful +if [ $? -eq 0 ]; then + echo "Upload successful. Cleaning up temporary files." + cleanup +else + echo "Error: Upload failed. Check your internet connection or OneDrive configuration." + exit 1 +fi diff --git a/snippets/human_readable_size.sh b/snippets/human_readable_size.sh new file mode 100755 index 0000000..22c4c01 --- /dev/null +++ b/snippets/human_readable_size.sh @@ -0,0 +1,15 @@ +human_readable_size() { + local bytes="$1" + local sizes=("B" "KB" "MB" "GB" "TB" "PB" "EB" "ZB" "YB") + + local i=0 + while ((bytes > 1024)); do + ((bytes /= 1024)) + ((i++)) + done + + printf "%d%s" "$bytes" "${sizes[i]}" +} + +human_readable_size $1 +echo "" diff --git a/snippets/sizetest.sh b/snippets/sizetest.sh new file mode 100755 index 0000000..9910c02 --- /dev/null +++ b/snippets/sizetest.sh @@ -0,0 +1,9 @@ +folder_size=$(du -s /mnt/data/storage/ | awk '{print $1}') +available_space=$(df /tmp | awk 'NR==2 {print $4}') +echo $folder_size +echo $available_space +if [[ "$folder_size" -gt "$available_space" ]]; then + echo "Error: Folder size exceeds available space in $(realpath $DESTINATION_DIR/)." +else + echo "Folder size is within available space in $(realpath $DESTINATION_DIR/)." +fi