Skip to content

Instantly share code, notes, and snippets.

@sunapi386
Created December 19, 2023 00:16
Show Gist options
  • Save sunapi386/351969dedab178c993e877fbdfc01a60 to your computer and use it in GitHub Desktop.
Save sunapi386/351969dedab178c993e877fbdfc01a60 to your computer and use it in GitHub Desktop.
Compressing the Cardano node database to reduce storage space, for systems with limited storage capacity.
#!/bin/bash
# This script compresses the Cardano node's 'immutable' directory to reduce storage usage.
# It creates directories for archives, indexes, and a write-overlay within the db path.
# The script groups files by their name prefixes, compresses them using pixz, and checks for existing archives to avoid reprocessing.
# After compression, original files are deleted, and new files are moved to the write-overlay.
# Ratarmount is used to mount a virtual directory combining the read-only archives and the write-overlay for operational continuity.
# Variables
CARDANO_DB_PATH=~/cnode/db # Replace with your actual Cardano db path
RATAR_PATH="$CARDANO_DB_PATH/ratar"
IMMUTABLE_PATH="$CARDANO_DB_PATH/immutable"
ARCHIVE_PATH="$RATAR_PATH/archive"
INDEXES_PATH="$RATAR_PATH/indexes"
WRITEOVERLAY_PATH="$RATAR_PATH/writeoverlay"
# Create necessary directories
mkdir -p "$ARCHIVE_PATH" "$INDEXES_PATH" "$WRITEOVERLAY_PATH"
# Function to compress files
compress_files() {
local prefix=$1
local archive_file="$ARCHIVE_PATH/$prefix.tar.xz"
# Check if the archive already exists
if [ -f "$archive_file" ]; then
echo "Archive $archive_file already exists, skipping."
return
fi
echo "Compressing files starting with $prefix"
tar -c --use-compress-program='pixz -9 -p 6' --file="$archive_file" "${prefix}"*.*
# Check if tar command was successful
if [ $? -eq 0 ]; then
echo "Compression successful. Removing original files."
# Delete the original files that have been archived
rm "${prefix}"*.*
else
echo "Error occurred during compression. Original files not removed."
fi
}
# Navigate to the immutable directory
cd "$IMMUTABLE_PATH" || exit
# Compress files grouped by the first two digits of their names
for i in {00..99}; do
if compgen -G "${i}"*.* > /dev/null; then
compress_files "$i"
fi
done
# Move remaining files to writeoverlay
mv *.* "$WRITEOVERLAY_PATH" 2>/dev/null
# Delete the now-empty immutable folder and recreate it for ratarmount
cd "$CARDANO_DB_PATH" || exit
rm -rf "$IMMUTABLE_PATH"
mkdir "$IMMUTABLE_PATH"
# Start ratarmount to create the virtual directory
ratarmount --index-folder "$INDEXES_PATH" "$ARCHIVE_PATH" "$IMMUTABLE_PATH"
echo "Cardano DB compression completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment