Skip to content

Instantly share code, notes, and snippets.

@ddyykk
Last active November 12, 2025 10:24
Show Gist options
  • Select an option

  • Save ddyykk/84bc588d6de1d346fbf473af49c34cf0 to your computer and use it in GitHub Desktop.

Select an option

Save ddyykk/84bc588d6de1d346fbf473af49c34cf0 to your computer and use it in GitHub Desktop.
The scripts used for backup and restore data in Firefly III docker containers. Steps are in the .md file.

Usage Instructions:

  1. Backup Script (backup_firefly_docker.sh):

    • Run the script: ./backup_firefly_docker.sh
    • Enter the database password when prompted.
    • The script will create a backup of the database and uploads directory, compress it, and verify its contents. The files will be in the directory of your current path. (The path when you run the script)
    • The backup file and its checksum will be saved in the firefly_backups directory.
  2. Restore Script (restore_firefly_docker.sh):

    • Run the script with the path to the backup file: ./restore_firefly_docker.sh path/to/backup.tar.gz
    • Enter the database password when prompted.
    • The script will extract the backup, verify its contents, restore the database and uploads directory, and restart the Firefly III container.

Both scripts ensure the integrity and validity of the backup and restore processes, providing error messages and cleanup steps in case of failures.

#!/bin/bash
# Prompt for database password
read -sp "Enter database password: " DB_PASSWORD
echo "" # Add newline after password input
# Validate password by trying to list databases
if ! docker exec -i firefly_iii_db mariadb -u firefly -p"${DB_PASSWORD}" -e "SHOW DATABASES;" > /dev/null 2>&1; then
echo "Error: Invalid database password"
exit 1
fi
# Create backup directory
BACKUP_DIR="firefly_backups/$(date +%Y%m%d)"
mkdir -p $BACKUP_DIR
# Database backup
echo "Creating database backup..."
docker exec firefly_iii_db mariadb-dump -u firefly -p"${DB_PASSWORD}" firefly > "$BACKUP_DIR/database.sql"
# Upload directory backup
echo "Creating uploads backup..."
docker cp firefly_iii_core:/var/www/html/storage/upload "$BACKUP_DIR/uploads"
# Compress the backup
echo "Compressing backup..."
tar -czf "$BACKUP_DIR.tar.gz" "$BACKUP_DIR"
# Verify the backup
echo "Verifying backup..."
# Create temp directory for verification
VERIFY_DIR="verify_temp"
mkdir -p $VERIFY_DIR
# Extract the backup
tar -xzf "$BACKUP_DIR.tar.gz" -C $VERIFY_DIR
# Verify database dump
if [ ! -s "$VERIFY_DIR/$BACKUP_DIR/database.sql" ]; then
echo "ERROR: Database dump is empty or missing!"
exit 1
fi
# Check for essential database tables
echo "Checking database content..."
REQUIRED_TABLES=("users" "accounts" "transactions")
for table in "${REQUIRED_TABLES[@]}"; do
if ! grep -q "CREATE TABLE \`$table\`" "$VERIFY_DIR/$BACKUP_DIR/database.sql"; then
echo "ERROR: Required table '$table' not found in database dump!"
exit 1
fi
done
# Verify uploads directory
if [ ! -d "$VERIFY_DIR/$BACKUP_DIR/uploads" ]; then
echo "ERROR: Uploads directory is missing!"
exit 1
fi
# Calculate and store checksums
echo "Calculating checksums..."
sha256sum "$BACKUP_DIR.tar.gz" > "$BACKUP_DIR.tar.gz.sha256"
# Clean up verification directory
rm -rf $VERIFY_DIR
# Remove the uncompressed backup directory
rm -rf "$BACKUP_DIR"
echo "Backup verification completed successfully!"
echo "Backup file: $BACKUP_DIR.tar.gz"
echo "Checksum file: $BACKUP_DIR.tar.gz.sha256"
echo ""
echo "Backup contents verified:"
echo "- Database dump present and contains required tables"
echo "- Uploads directory present"
echo "- Backup integrity verified via checksum"
#!/bin/bash
# Check if backup file is provided
if [ -z "$1" ]; then
echo "Usage: ./restore_firefly_docker.sh path/to/backup.tar.gz"
exit 1
fi
# Prompt for database password
read -sp "Enter database password: " DB_PASSWORD
echo "" # Add newline after password input
# Validate password by trying to list databases
if ! docker exec -i firefly_iii_db mariadb -u firefly -p"${DB_PASSWORD}" -e "SHOW DATABASES;" > /dev/null 2>&1; then
echo "Error: Invalid database password"
exit 1
fi
BACKUP_FILE=$1
RESTORE_DIR="firefly_restore_temp"
# Extract the backup
echo "Extracting backup..."
mkdir -p $RESTORE_DIR
tar -xzf $BACKUP_FILE -C $RESTORE_DIR
# Find the extracted directory (it should be named with a date)
EXTRACTED_DIR=$(ls $RESTORE_DIR/firefly_backups)
FULL_RESTORE_PATH="$RESTORE_DIR/firefly_backups/$EXTRACTED_DIR"
# Verify backup contents
if [ ! -f "$FULL_RESTORE_PATH/database.sql" ]; then
echo "ERROR: Database backup file not found!"
rm -rf $RESTORE_DIR
exit 1
fi
if [ ! -d "$FULL_RESTORE_PATH/uploads" ]; then
echo "ERROR: Uploads directory not found!"
rm -rf $RESTORE_DIR
exit 1
fi
# Restore database
echo "Restoring database..."
cat "$FULL_RESTORE_PATH/database.sql" | docker exec -i firefly_iii_db mariadb -u firefly -p"${DB_PASSWORD}" firefly
if [ $? -ne 0 ]; then
echo "ERROR: Database restore failed!"
rm -rf $RESTORE_DIR
exit 1
fi
# Restore uploads
echo "Restoring uploads..."
docker cp "$FULL_RESTORE_PATH/uploads/." firefly_iii_core:/var/www/html/storage/upload/
if [ $? -ne 0 ]; then
echo "ERROR: Uploads restore failed!"
rm -rf $RESTORE_DIR
exit 1
fi
# Cleanup
echo "Cleaning up temporary files..."
rm -rf $RESTORE_DIR
echo "Restore completed successfully!"
echo "Restarting Firefly III container..."
docker restart firefly_iii_core
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment