This commit is contained in:
Sebastian
2025-01-30 12:27:40 +01:00
parent 9d491cbc9e
commit 1b7ddcbb50

View File

@@ -12,117 +12,73 @@ if [ ! -d "$volume_dir" ]; then
exit 1 exit 1
fi fi
if [[ -n $2 ]]; then
echo "download backup from http"
$dir/clone-backup.sh $2 $1
else
if [ ! -d "$backup_dir" ]; then
echo "Error: /backup directory does not exist"
exit 1
fi
fi
calculate_required_space() {
# Extract the size from the filename
size=$(echo "$1" | grep -oE '[0-9]+G')
# Remove 'G' from the size and convert it to bytes
size_bytes=$(echo "$size" | sed 's/G//')
if [[ $size_bytes == 0* ]]; then
# this happens when the size is smalleer than 1 GB
size_bytes=1
fi
size_bytes=$(echo "$size_bytes * 1024 * 1024 * 1024" | bc)
# Calculate 10% of the size and add it to the required space
ten_percent=$(echo "$size_bytes / 10" | bc)
required_space=$(echo "$size_bytes + $ten_percent" | bc)
#required_space=$(( size_bytes + ten_percent ))
echo "$required_space"
}
# Read the JSON input and extract the list of keys # Read the JSON input and extract the list of keys
keys=$(cat $dir/$1.yml | yaml2json - | jq '.volumes' | jq -r 'keys[]' | grep -E '^["'\'']?[0-9a-z]') keys=$(cat $dir/$1.yml | yaml2json - | jq '.volumes' | jq -r 'keys[]' | grep -E '^["'\'']?[0-9a-z]')
# Iterate over the list of keys
total_space=0
cleanup_space=0
restore_files=() restore_files=()
cleanup_folders=() cleanup_folders=()
for key in $keys; do echo "$keys"
while IFS= read -r key; do
volume_name="rpc_$key" volume_name="rpc_$key"
declare newest_file
if [[ -n $2 ]]; 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)
else
newest_file=$(ls -1 "$backup_dir"/"${volume_name}"-[0-9]*G.tar.zst 2>/dev/null | sort | tail -n 1) newest_file=$(ls -1 "$backup_dir"/"${volume_name}"-[0-9]*G.tar.zst 2>/dev/null | sort | tail -n 1)
directory="$volume_dir/rpc_$key/_data/" fi
[ -d "$directory" ] && existing_size=$(du -sb "$directory" | awk '{ total += $1 } END { print total }') || existing_size=0
#echo "$newest_file" directory="$volume_dir/rpc_$key/_data/"
#echo "$directory: $existing_size"
if [ -z "$newest_file" ]; then if [ -z "$newest_file" ]; then
if [[ "$2" = "--print-size-only" && $existing_size -gt 0 ]]; then
# I only want to have a theoretical file size
status=$($dir/sync-status.sh "$1")
if [ $? -eq 0 ]; then
# 0 means it's synced
required_space=$existing_size
else
echo "Error: No backup and unfisnished syncing '$volume_name'"
exit 1
fi
#GB=$(echo "$existing_size / 1024 / 1024 / 1024" | bc )
#echo "$GB"
#exit 0
else
echo "Error: No backup found for volume '$volume_name'" echo "Error: No backup found for volume '$volume_name'"
exit 1 exit 1
fi
else else
restore_files+=("$newest_file") restore_files+=("$newest_file")
cleanup_folders+=("$directory") cleanup_folders+=("$directory")
#echo "bleb"
required_space=$(calculate_required_space "$(basename "$newest_file")")
fi fi
done <<< "$keys"
#echo "$volume_name: $required_space" echo "${cleanup_folders[@]}"
#echo "blab: $total_space + $required_space"
total_space=$(echo "$total_space + $required_space" | bc) for folder in "${cleanup_folders[@]}"; do
#echo "blub" echo "delete '$folder'"
cleanup_space=$(echo "$cleanup_space + existing_size" | bc) [ -d "$folder" ] && rm -rf "$folder"/*
#echo "blob"
done done
if [ "$2" = "--print-size-only" ]; then echo "done cleanup"
GB=$(echo "$total_space / 1024 / 1024 / 1024" | bc )
#echo "blib"
echo "$GB"
exit 0
fi
available_space=$(df --output=avail -B1 "$volume_dir" | tail -n 1) for file in "${restore_files[@]}"; do
available_space=$((available_space + cleanup_space)) echo "Processing: $file"
if [ "$available_space" -lt "$total_space" ]; then if [[ -n $2 ]]; then
echo "Error: Not enough free space in $volume_dir" if [ ! -d "$backup_dir" ]; then
echo "Error: /backup directory does not exist. download from http and extract directly to /var/lib/docker"
curl -# "${2}${file}" | zstd -d | tar -xvf - --dereference -C /
if [ $? -ne 0 ]; then
echo "Error processing $file"
exit 1 exit 1
else
echo "$file successfully processed."
fi fi
else
for folder in $cleanup_folders; do if [ -e "$file" ]; then
echo "delete $folder" aria2c -c -Z -x8 -j8 -s8 -d "$backup_dir" "${2}${file}"
[ -d "$folder" ] && rm -rf "$folder/*" fi
done tar -I zstd -xf "$file" --dereference -C /
echo "Backup '$file' processed"
for file in $restore_files; do fi
else
tar -I zstd -xf "$file" --dereference -C / tar -I zstd -xf "$file" --dereference -C /
echo "Backup '$file' restored" echo "Backup '$file' restored"
fi
done done
echo "node $1 restored." echo "node $1 restored."