#!/usr/bin/env bash # -------------------------------------------------------- # Backup Upload Script # # This script grabs the latest file in /var/lib/backup/firemon # and sends it to the specified Nextcloud/Owncloud upload URL. # # Usage: ./sendbackup.sh # Example: ./sendbackup.sh https://supportfiles.firemon.com/s/1a2b3c4g5d6g # # Author: Adam Gunderson # Adam.Gunderson@firemon.com # -------------------------------------------------------- CS_VERSION="0.2.0" VERBOSE="" INSECURE="" PASSWORD="" PUBSUFFIX="public.php/webdav" # Restored the original header which is required for this specific Nextcloud implementation HEADER='X-Requested-With: XMLHttpRequest' log() { [ "$VERBOSE" == " -s" ] || printf "%s\n" "$1" } printVersion() { printf "%s\n" "Backup Uploader v$CS_VERSION" } initError() { printVersion >&2 printf "%s\n" "Error! $1" >&2 printf "%s\n" "Try: $0 --help" >&2 exit 1 } usage() { printVersion printf "\n%s%s\n" "Parameters:" " -h | --help Print this help and exits -q | --quiet Be quiet -V | --version Prints version and exits -k | --insecure Uses curl with -k option (https insecure) -p | --password Uses env var \$SUPPORT_FILES_PASSWORD as share password You can 'export SUPPORT_FILES_PASSWORD' at your system, or set it at the call. Please remember to also call -p to use the password set." printf "\n%s\n%s\n" "Use:" " $0 " printf "\n%s\n%s\n" "Example:" " $0 'https://supportfiles.firemon.com/s/1a2b3c4d5e6g'" } ########################## # Process parameters if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then usage exit 0 fi if [ "$1" = "-V" ] || [ "$1" = "--version" ]; then printVersion exit 0 fi if [ "$1" = "-q" ] || [ "$1" = "--quiet" ]; then VERBOSE=" -s" shift fi if [ "$1" = "-k" ] || [ "$1" = "--insecure" ]; then INSECURE=' -k' log " > Insecure mode ON" shift fi if [ "$1" = "-p" ] || [ "$1" = "--password" ]; then PASSWORD=${SUPPORT_FILES_PASSWORD} log " > Using password from env" shift fi ########################## # Validate input UPLOADURL="$1" if [ -z "$UPLOADURL" ]; then initError "Empty URL! Nowhere to send..." fi # Extract cloud URL and folder token # Remove /s/token from the end of the URL to get the base URL CLOUDURL="${UPLOADURL%/s/*}" # Extract token from the URL FOLDERTOKEN="${UPLOADURL##*/s/}" if [ -z "$CLOUDURL" ]; then initError "Empty URL! Nowhere to send..." fi if [ -z "$FOLDERTOKEN" ]; then initError "Empty Folder Token! Nowhere to send..." fi log " > Cloud URL: $CLOUDURL" log " > Folder Token: $FOLDERTOKEN" ########################## # Find latest file in /var/lib/backup/firemon BACKUP_DIR="/var/lib/backup/firemon" if [ ! -d "$BACKUP_DIR" ]; then initError "Backup directory not found: $BACKUP_DIR" fi # Find the latest file in the backup directory cd "$BACKUP_DIR" || initError "Cannot access backup directory: $BACKUP_DIR" LATEST_FILE="" for file in *; do if [ -f "$file" ]; then if [ -z "$LATEST_FILE" ] || [ "$file" -nt "$LATEST_FILE" ]; then LATEST_FILE="$file" fi fi done if [ -z "$LATEST_FILE" ]; then initError "No files found in backup directory: $BACKUP_DIR" fi LATEST_FILE="$BACKUP_DIR/$LATEST_FILE" log " > Latest backup file: $LATEST_FILE" ########################## # Extract base filename BFILENAME=$(basename "$LATEST_FILE") ########################## # Get file size in human-readable format # Get file size in bytes FILE_SIZE_BYTES=$(stat -c %s "$LATEST_FILE" 2>/dev/null) if [ $? -ne 0 ]; then # Fallback if stat doesn't work FILE_SIZE_BYTES=$(wc -c < "$LATEST_FILE" 2>/dev/null) fi # Convert to human-readable format if [ $FILE_SIZE_BYTES -lt 1024 ]; then FILE_SIZE="${FILE_SIZE_BYTES} bytes" elif [ $FILE_SIZE_BYTES -lt 1048576 ]; then FILE_SIZE="$(echo "scale=2; $FILE_SIZE_BYTES/1024" | bc) KB" elif [ $FILE_SIZE_BYTES -lt 1073741824 ]; then FILE_SIZE="$(echo "scale=2; $FILE_SIZE_BYTES/1048576" | bc) MB" else FILE_SIZE="$(echo "scale=2; $FILE_SIZE_BYTES/1073741824" | bc) GB" fi log " > File size: $FILE_SIZE ($FILE_SIZE_BYTES bytes)" ########################## # Check for curl CURLBIN=$(which curl 2>/dev/null) if [ ! -x "$CURLBIN" ]; then initError "No curl found on system!" fi ########################## # Send file log " > Uploading $BFILENAME to $CLOUDURL" # Create temporary file to capture response RESPONSE_FILE=$(mktemp) # Run curl with response capture to temp file "$CURLBIN"$INSECURE$VERBOSE -T "$LATEST_FILE" -u "$FOLDERTOKEN":"$PASSWORD" -H "$HEADER" "$CLOUDURL/$PUBSUFFIX/$BFILENAME" -o "$RESPONSE_FILE" CURL_STATUS=$? # Check if upload was successful if [ $CURL_STATUS -eq 0 ]; then # Check if response contains error message (XML with exception) if grep -q "" "$RESPONSE_FILE" || grep -q " Upload failed! Server returned an error:" cat "$RESPONSE_FILE" # Check for specific authentication errors if grep -q "NotAuthenticated\|Cannot authenticate" "$RESPONSE_FILE"; then log " > Authentication failed. Please check your password and try again." fi rm -f "$RESPONSE_FILE" exit 1 else log " > Upload successful!" fi else log " > Upload failed! (Exit code: $CURL_STATUS)" if [ -s "$RESPONSE_FILE" ]; then log " > Server response:" cat "$RESPONSE_FILE" fi rm -f "$RESPONSE_FILE" exit 1 fi # Clean up rm -f "$RESPONSE_FILE"