From a6ceb5c75866cd81f2bdcb1b0d5d348770b7f763 Mon Sep 17 00:00:00 2001 From: Sebastian <379651+czarly@users.noreply.github.com> Date: Thu, 21 Mar 2024 09:09:28 +0100 Subject: [PATCH] fiddle backups --- backup-node.sh | 14 +++++++++++- restore-node.sh | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 restore-node.sh diff --git a/backup-node.sh b/backup-node.sh index a41cafdd..409a4b4d 100755 --- a/backup-node.sh +++ b/backup-node.sh @@ -13,7 +13,19 @@ for key in $keys; do echo "Executing command with key: /var/lib/docker/volumes/rpc_$key/_data" source_folder="/var/lib/docker/volumes/rpc_$key/_data" - folder_size=$(du -sh "$source_folder" | awk '{print $1}') + folder_size=$(du -sh "$source_folder" | awk '{ + size = $1 + sub(/[Kk]$/, "", size) # Remove 'K' suffix if present + sub(/[Mm]$/, "", size) # Remove 'M' suffix if present + sub(/[Gg]$/, "", size) # Remove 'G' suffix if present + if ($1 ~ /[Kk]$/) { + size *= 0.001 # Convert kilobytes to gigabytes + } else if ($1 ~ /[Mm]$/) { + size *= 0.001 # Convert megabytes to gigabytes + } + print size "G" + }') + target_file="/backup/rpc_$key-$(date +'%Y-%m-%d-%H-%M-%S')-$folder_size.tar.zst" tar -cf - "$source_folder" | pv -pterb -s $(du -sb "$source_folder" | awk '{print $1}') | zstd -o "$target_file" diff --git a/restore-node.sh b/restore-node.sh new file mode 100644 index 00000000..0524357f --- /dev/null +++ b/restore-node.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +# Path to the backup directory +backup_dir="/backup" + +# Path to the volume directory +volume_dir="/var/lib/docker/volume" + +if [ ! -d "$backup_dir" ]; then + echo "Error: /backup directory does not exist" + exit 1 +fi + +# Function to calculate the required disk space +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//') + size_bytes=$(( size_bytes * 1024 * 1024 * 1024 )) # Convert GB to bytes + + # Calculate 10% of the size and add it to the required space + ten_percent=$(( size_bytes / 10 )) + required_space=$(( size_bytes + ten_percent )) + + echo "$required_space" +} + +# Read the JSON input and extract the list of keys +keys=$(cat /root/rpc/$1.yml | yaml2json - | jq '.volumes' | jq -r 'keys[]') + +# Iterate over the list of keys +for key in $keys; do + echo "Executing command with key: /var/lib/docker/volumes/rpc_$key/_data" + volume_name="rpc_$volume_name" + + newest_file=$(ls -1 "$backup_dir"/"$volume_name"* | sort | tail -n 1) + + if [ -z "$newest_file" ]; then + echo "Error: No backup found for volume '$volume_name'" + exit 1 + fi + + required_space=$(calculate_required_space "$(basename "$newest_file")") + + available_space=$(df --output=avail -B1 "$volume_dir" | tail -n 1) + + if [ "$available_space" -lt "$required_space" ]; then + echo "Error: Not enough free space in $volume_dir" + exit 1 + fi + + tar -I zstd -xf "$newest_file" -C / + + echo "Backup '$newest_file' restored" +done + + +