Last active
May 17, 2025 10:04
-
-
Save ripsnortntear/48f0a5d55448b9eec4a4a4b8d727946c to your computer and use it in GitHub Desktop.
backupserver.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to prompt for input with auto-completion | |
prompt_for_paths() { | |
local prompt_message="$1" | |
local result_var="$2" | |
local input | |
# Enable readline's file name completion | |
read -e -a input -p "$prompt_message: " | |
# Join array into space-separated string | |
local joined_input="${input[@]}" | |
# Set the result variable using indirect reference | |
eval "$result_var=\"\$joined_input\"" | |
} | |
# Prompt for backup directory (with completion) | |
read -e -p "Enter the backup directory: " BACKUP_DIR | |
# Prompt for source directories (with completion, multiple supported) | |
prompt_for_paths "Enter the source directories (space-separated, use TAB to complete)" SOURCE_DIRS | |
# Prompt for files to exclude (with completion, multiple supported) | |
prompt_for_paths "Enter files to exclude (space-separated, use TAB to complete)" EXCLUDE_FILES | |
# Prompt for archive name (no completion needed) | |
read -e -p "Enter the archive name [var_backup]: " ARCHIVE_NAME | |
ARCHIVE_NAME=${ARCHIVE_NAME:-var_backup} | |
# Prompt for maximum size of each archive part (no completion needed) | |
read -e -p "Enter maximum size of each archive part [3G]: " MAX_SIZE | |
MAX_SIZE=${MAX_SIZE:-3G} | |
mkdir -p "$BACKUP_DIR" | |
# Build tar exclude options | |
EXCLUDE_ARGS=() | |
for file in $EXCLUDE_FILES; do | |
EXCLUDE_ARGS+=(--exclude="$file") | |
done | |
# Tar up the source directories | |
tar czf - "${EXCLUDE_ARGS[@]}" $SOURCE_DIRS | \ | |
7z a -v"$MAX_SIZE" -mx=9 -si "$BACKUP_DIR/$ARCHIVE_NAME.tar.7z" | |
if [ $? -eq 0 ]; then | |
echo "Backup completed successfully." | |
else | |
echo "Backup failed." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment