more sophisticated

This commit is contained in:
Sebastian
2024-03-24 04:51:34 +01:00
parent 61fe5ecf87
commit fa09bd4748

View File

@@ -35,6 +35,13 @@ calculate_required_space() {
keys=$(cat /root/rpc/$1.yml | yaml2json - | jq '.volumes' | jq -r 'keys[]') keys=$(cat /root/rpc/$1.yml | yaml2json - | jq '.volumes' | jq -r 'keys[]')
# Iterate over the list of keys # Iterate over the list of keys
total_space=0
cleanup_space=0
restore_files=()
cleanup_folders=()
for key in $keys; do for key in $keys; do
echo "Executing command with key: /var/lib/docker/volumes/rpc_$key/_data" echo "Executing command with key: /var/lib/docker/volumes/rpc_$key/_data"
volume_name="rpc_$key" volume_name="rpc_$key"
@@ -49,21 +56,39 @@ for key in $keys; do
fi fi
fi fi
required_space=$(calculate_required_space "$(basename "$newest_file")") directory="$volume_dir/rpc_$key/_data/"
restore_files+=("$newest_file")
rm -rf "/var/lib/docker/volumes/rpc_$key/_data/*" cleanup_folders+=("$directory")
available_space=$(df --output=avail -B1 "$volume_dir" | tail -n 1) required_space=$(calculate_required_space "$(basename "$newest_file")")
total_space=$((total_space + required_space))
if [ "$available_space" -lt "$required_space" ]; then [ -d "$directory" ] && existing_size=$(du -sb "$directory" | awk '{ total += $1 } END { print total }') || existing_size=0
echo "Error: Not enough free space in $volume_dir" cleanup_space=$((cleanup_space + existing_size))
exit 1
fi
tar -I zstd -xf "$newest_file" -C /
echo "Backup '$newest_file' restored"
done done
if [ "$2" = "--print-size-only" ]; then
GB=$(( $total_space / 1024 / 1024 / 1024 ))
echo "$GB"
exit 0
fi
available_space=$(df --output=avail -B1 "$volume_dir" | tail -n 1)
available_space=$((available_space + cleanup_space))
if [ "$available_space" -lt "$total_space" ]; then
echo "Error: Not enough free space in $volume_dir"
exit 1
fi
for folder in $cleanup_folders; do
[ -d "$folder" ] && rm -rf "$folder/*"
done
for file in $restore_files; do
tar -I zstd -xf "$file" -C /
echo "Backup '$file' restored"
done
echo "node $1 restored."