From 1aef140a9aa3733b13aee0197e93512b515963ab Mon Sep 17 00:00:00 2001 From: goldsquid Date: Sat, 6 Jun 2026 11:36:58 +0700 Subject: [PATCH] if the backup storage is local do not use webdav --- backup-node.sh | 18 ++++++++++++------ restore-volumes.sh | 16 +++++++++++----- volume-utils.sh | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 11 deletions(-) diff --git a/backup-node.sh b/backup-node.sh index 2bbec52a..eab1b320 100755 --- a/backup-node.sh +++ b/backup-node.sh @@ -4,8 +4,14 @@ BASEPATH="$(dirname "$0")" source "$BASEPATH/volume-utils.sh" backup_dir="/backup" -if [[ -n $2 ]]; then - echo "upload backup via webdav to $2" +remote_target="$2" +if [[ -n "$remote_target" ]] && is_local_backup_url "$remote_target"; then + echo "Target URL points to this server, using local /backup instead of $remote_target" + remote_target="" +fi + +if [[ -n "$remote_target" ]]; then + echo "upload backup via webdav to $remote_target" else if [ ! -d "$backup_dir" ]; then echo "Error: /backup directory does not exist" @@ -75,16 +81,16 @@ for key in $keys; do #echo "$target_file" - if [[ -n $2 ]]; then + if [[ -n "$remote_target" ]]; then # Upload volume archive - tar -cf - --dereference "$source_folder" | pv -pterb -s $(du -sb "$source_folder" | awk '{print $1}') | zstd | curl -X PUT --upload-file - "$2/null/uploading-$target_file" - curl -X MOVE -H "Destination: /null/$target_file" "$2/null/uploading-$target_file" + tar -cf - --dereference "$source_folder" | pv -pterb -s $(du -sb "$source_folder" | awk '{print $1}') | zstd | curl -X PUT --upload-file - "$remote_target/null/uploading-$target_file" + curl -X MOVE -H "Destination: /null/$target_file" "$remote_target/null/uploading-$target_file" # Generate and upload metadata file echo "Generating metadata for volume: rpc_$key" temp_metadata="/tmp/$metadata_file_name" generate_volume_metadata "$key" "$source_folder" "$temp_metadata" - curl -X PUT --upload-file "$temp_metadata" "$2/null/$metadata_file_name" + curl -X PUT --upload-file "$temp_metadata" "$remote_target/null/$metadata_file_name" rm -f "$temp_metadata" else # Create volume archive diff --git a/restore-volumes.sh b/restore-volumes.sh index c6c2aec0..d9776dd0 100755 --- a/restore-volumes.sh +++ b/restore-volumes.sh @@ -2,6 +2,12 @@ dir="$(dirname "$0")" source "$dir/volume-utils.sh" +remote_source="$2" +if [[ -n "$remote_source" ]] && is_local_backup_url "$remote_source"; then + echo "Source URL points to this server, using local /backup instead of $remote_source" + remote_source="" +fi + # Path to the backup directory backup_dir="/backup" @@ -25,9 +31,9 @@ while IFS= read -r key; do volume_name="rpc_$key" declare newest_file - if [[ -n $2 ]]; then + if [[ -n "$remote_source" ]]; then volume_name="rpc_$key-20" # needs to be followed by a date 2024 - newest_file=$($dir/list-backups.sh $2 | grep "${volume_name}" | sort | tail -n 1) + newest_file=$($dir/list-backups.sh "$remote_source" | grep "${volume_name}" | sort | tail -n 1) else newest_file=$(ls -1 "$backup_dir"/"${volume_name}"-[0-9]*G.tar.zst 2>/dev/null | sort | tail -n 1) fi @@ -55,12 +61,12 @@ echo "done cleanup" for file in "${restore_files[@]}"; do echo "Processing: $file" - if [[ -n $2 ]]; then + if [[ -n "$remote_source" ]]; then if [ ! -d "$backup_dir" ]; then echo "Error: /backup directory does not exist. download from http and extract directly to /var/lib/docker" - curl --ipv4 -# "${2}${file}" | zstd -d | tar -xvf - --dereference -C / + curl --ipv4 -# "${remote_source}${file}" | zstd -d | tar -xvf - --dereference -C / if [ $? -ne 0 ]; then echo "Error processing $file" @@ -71,7 +77,7 @@ for file in "${restore_files[@]}"; do else echo "have backup dir to cache... $file" if [ ! -e "$backup_dir/$(basename $file)" ]; then - aria2c -c -Z -x8 -j8 -s8 -d "$backup_dir" "${2}${file}" + aria2c -c -Z -x8 -j8 -s8 -d "$backup_dir" "${remote_source}${file}" fi tar -I zstd -xf "$backup_dir/$(basename $file)" --dereference -C / echo "Backup '$file' processed" diff --git a/volume-utils.sh b/volume-utils.sh index 39167379..133ab232 100755 --- a/volume-utils.sh +++ b/volume-utils.sh @@ -38,3 +38,36 @@ get_persistent_volume_keys() { fi done < <(get_volume_keys "$compose_file") } + +# Returns 0 when a backup HTTP/WebDAV URL refers to this machine. +is_local_backup_url() { + local url=$1 + [[ -z "$url" ]] && return 1 + + local host="${url#*://}" + host="${host%%/*}" + host="${host%%:*}" + [[ -z "$host" ]] && return 1 + + case "$host" in + localhost|127.0.0.1|::1|0.0.0.0) return 0 ;; + esac + + local env_file + env_file="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/.env" + if [[ -f "$env_file" ]]; then + local domain ip + domain=$(grep -E '^DOMAIN=' "$env_file" | head -n 1 | cut -d= -f2- | tr -d '"' | tr -d "'") + ip=$(grep -E '^IP=' "$env_file" | head -n 1 | cut -d= -f2- | tr -d '"' | tr -d "'") + [[ -n "$domain" && "$host" == "$domain" ]] && return 0 + [[ -n "$ip" && "$host" == "$ip" ]] && return 0 + fi + + local local_fqdn local_short + local_fqdn=$(hostname -f 2>/dev/null || true) + local_short=$(hostname 2>/dev/null || true) + [[ -n "$local_fqdn" && "$host" == "$local_fqdn" ]] && return 0 + [[ -n "$local_short" && "$host" == "$local_short" ]] && return 0 + + return 1 +}