Claude Code an agentic coding tool that can read modify and execute code in your working directory
root@ra-ai-01:/AI/ClaudeCode# curl -fsSL https://claude.ai/install.sh | bash
root@ra-ai-01:/AI/ClaudeCode# ollama launch claude –model gemma4
╭─── Claude Code v2.1.138 ──────────────────────────────────╮
│ │ Tips for getting started │
│ Welcome back! │ Ask Claude to create a new app or clone a repository │
│ │ ───────────────────────────────────────────────── │
│ ▐▛███▜▌ │ What’s new │
│ ▝▜█████▛▘ │ Internal fixes │
│ ▘▘ ▝▝ │ [VSCode] Fixed extension failing to activate on Windows │
│ │ Added `CLAUDE_CODE_ENABLE_FEEDBACK_SURVEY_FOR_OTEL` to re-enable the session quality survey for … │
│ gemma4 · API Usage Billing │ /release-notes for more │
│ /AI/ClaudeCode │ │
╰─────────────────────────────────────────────────────╯
❯ Create a shell script to perform an online backup of a PostgreSQL database with verification
● I will create a secure, well-commented shell script that uses pg_dump to perform the online backup and includes a verification step by attempting to restore the dump file to a temporary database instance.
Here is the script:
#!/bin/bash
# Script: pg_db_backup.sh
# Description: Performs an online, compressed backup of a PostgreSQL database
# and verifies the integrity by attempting a restore to a temporary database.
#
# Usage: ./pg_db_backup.sh <DB_NAME> <DB_USER> [BACKUP_DIR]
# Example: ./pg_db_backup.sh my_production_db postgres /var/backups/postgres
# — Configuration —
# Ensure these variables are set or passed as arguments
# Source DB credentials/host are typically defined here or through environment variables
DB_HOST=${DB_HOST:-localhost}
DB_PORT=${DB_PORT:-5432}
COMPRESSION_TOOL=“gzip“ # Can be changed to bzip2
# — Helper Functions —
# Function to validate required dependencies
check_dependencies() {
for cmd in pg_dump ${COMPRESSION_TOOL}; do
if ! command -v „$cmd“ &> /dev/null; then
echo „ERROR: Required command ‚$cmd‘ is not installed or not in PATH.“ >&2
exit 1
fi
done
}
# Function to perform the actual backup dump
perform_dump() {
echo „— 1. Starting PostgreSQL Dump —„
echo „Dumping database ‚$1‘ from $2:$3:$4 to $5…“
# Use pg_dump for logical backup format, which is generally best.
# We pipe the output directly to compression to save space.
if pg_dump -h „$DB_HOST“ -p „$DB_PORT“ -U „$DB_USER“ -d „$DB_NAME“ |
$COMPRESSION_TOOL > „$BACKUP_FILE“; then
echo „SUCCESS: Database dump completed and saved to $BACKUP_FILE.“
return 0
else
echo „ERROR: Failed to dump the database. Check credentials, permissions, and if the DB name is correct.“ >&2
return 1
fi
}
# Function to verify the backup file
verify_backup() {
echo -e „\n— 2. Starting Backup Verification (Restore Check) —„
echo „Attempting to restore a minimal schema/data subset to a temporary database to verify integrity.“
TEMP_DB_NAME=“temp_verify_$$“
# 1. Check if we can create a temporary database
if ! psql -h „$DB_HOST“ -p „$DB_PORT“ -U „$DB_USER“ -c „CREATE DATABASE $TEMP_DB_NAME;“ > /dev/null 2>&1; then
echo „WARNING: Could not create temporary database ‚$TEMP_DB_NAME‘. This may indicate permission issues or conflicting
names.“
echo „Verification skipped. The dump file itself might still be corrupt.“
return 1
fi
# 2. Attempt to restore (pg_restore is better for compressed dumps)
# Note: We are restoring only a small subset to minimize risk and time.
# The best practice is to only restore schema/core tables.
if pg_restore -h „$DB_HOST“ -p „$DB_PORT“ -U „$DB_USER“ -d „$TEMP_DB_NAME“ -C –clean –if-exists -s „$BACKUP_FILE“ > /dev/null
2>&1; then
echo „SUCCESS: Verification passed. The dump file appears to be intact and restorable.“
return 0
else
echo „ERROR: Restoration failed. The dump file may be corrupt or the database structure might have changed.“ >&2
return 1
fi
}
# Function to clean up temporary resources
cleanup() {
echo -e „\n— 3. Cleaning Up —„
# Remove the temporary database created for verification
psql -h „$DB_HOST“ -p „$DB_PORT“ -U „$DB_USER“ -c „DROP DATABASE IF EXISTS $TEMP_DB_NAME;“ > /dev/null 2>&1
echo „Cleanup complete.“
}
# — Main Execution —
main() {
# Check arguments: DB Name, User, Output Directory
if [ „$#“ -ne 3 ]; then
echo „Usage: $0 <DB_NAME> <DB_USER> <OUTPUT_DIR>“ >&2
exit 1
fi
DB_NAME=“$1″
DB_USER=“$2″
OUTPUT_DIR=“$3″
# Setup variables
TIMESTAMP=$(date +“%Y%m%d_%H%M%S“)
BACKUP_FILE=“$OUTPUT_DIR/$DB_NAME-$TIMESTAMP.dump.$COMPRESSION_TOOL“
TEMP_DB_NAME=“temp_verify_$$“ # Use PID for uniqueness
# 1. Pre-flight Checks
check_dependencies
# Ensure the output directory exists
mkdir -p „$OUTPUT_DIR“
# 2. Backup and Dump
if perform_dump „$DB_NAME“ „$DB_USER“ „$DB_HOST“ „$BACKUP_FILE“; then
# 3. Verification
verify_backup
else
echo „Backup process aborted due to failure during the dump stage.“
fi
# 4. Cleanup
cleanup
}
# Execute the main function with provided arguments
main „$@“
Instructions for Use:
1. Save the script: Save the content above into a file named pg_db_backup.sh.
2. Make it executable: Run chmod +x pg_db_backup.sh.
3. Run the backup: Execute the script from your terminal, providing the database name, the PostgreSQL user, and the desired output directory.
./pg_db_backup.sh your_database_name your_db_user /path/to/backups
The script handles the following:
– Online Backup: Uses pg_dump, which is designed for live database backups.
– Compression: Compresses the dump file using gzip (or a configurable alternative).
– Error Handling: Includes checks for dependencies and failures during the dump.
– Verification: Attempts to restore the backup to a unique, temporary database (temp_verify_$$) to ensure the file is usable and the core schema is intact.
– Cleanup: Automatically drops the temporary verification database afterward.
This approach ensures not only that the file was created, but also that the data within the file is structurally sound and ready for restoration.
✻ Crunched for 14m 42s
──────────────────────────
❯
──────────────────────────
Press Ctrl-C again to exit