fiddle backups

This commit is contained in:
Sebastian
2024-03-21 09:09:28 +01:00
parent 60d3d5681e
commit a6ceb5c758
2 changed files with 73 additions and 1 deletions

View File

@@ -13,7 +13,19 @@ 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"
source_folder="/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" 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" tar -cf - "$source_folder" | pv -pterb -s $(du -sb "$source_folder" | awk '{print $1}') | zstd -o "$target_file"

60
restore-node.sh Normal file
View File

@@ -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