|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# This script sets up a weekly VACUUM FULL job to clean up database bloat |
| 4 | +# VACUUM FULL reclaims more space than regular AUTOVACUUM by rewriting the entire table |
| 5 | + |
| 6 | +# Create scripts directory if it doesn't exist |
| 7 | +mkdir -p /var/lib/postgresql/scripts |
| 8 | + |
| 9 | +# Create the vacuum script that will run VACUUM FULL on all tables in the database |
| 10 | +cat > /var/lib/postgresql/scripts/vacuum_full.sh << 'EOF' |
| 11 | +#!/bin/sh |
| 12 | +
|
| 13 | +# Log file for vacuum operations |
| 14 | +LOG_FILE="/var/lib/postgresql/vacuum_full.log" |
| 15 | +
|
| 16 | +echo "$(date): Starting weekly VACUUM FULL operation" >> "$LOG_FILE" |
| 17 | +
|
| 18 | +# Get list of all tables in the database |
| 19 | +TABLES=$(psql -t -c "SELECT schemaname || '.' || tablename FROM pg_tables WHERE schemaname NOT IN ('pg_catalog', 'information_schema')" "${POSTGRES_DB:-postgres}") |
| 20 | +
|
| 21 | +# Run VACUUM FULL on each table |
| 22 | +for TABLE in $TABLES; do |
| 23 | + echo "$(date): Running VACUUM FULL on $TABLE" >> "$LOG_FILE" |
| 24 | + psql -c "VACUUM FULL $TABLE" "${POSTGRES_DB:-postgres}" |
| 25 | + echo "$(date): Completed VACUUM FULL on $TABLE" >> "$LOG_FILE" |
| 26 | +done |
| 27 | +
|
| 28 | +echo "$(date): Weekly VACUUM FULL operation completed" >> "$LOG_FILE" |
| 29 | +EOF |
| 30 | + |
| 31 | +# Make the script executable |
| 32 | +chmod +x /var/lib/postgresql/scripts/vacuum_full.sh |
| 33 | + |
| 34 | +# Set up cron job to run the script weekly (Sunday at 12:00 AM) |
| 35 | +CRON_ENTRY="0 0 * * 0 /var/lib/postgresql/scripts/vacuum_full.sh" |
| 36 | + |
| 37 | +# Detect which cron system is available |
| 38 | +if command -v crond > /dev/null 2>&1; then |
| 39 | + # Alpine Linux (dcron) |
| 40 | + echo "Setting up cron job using Alpine's crond (dcron)..." |
| 41 | + mkdir -p /etc/crontabs |
| 42 | + echo "$CRON_ENTRY" > /etc/crontabs/postgres |
| 43 | + chmod 600 /etc/crontabs/postgres |
| 44 | + echo "Weekly VACUUM FULL job has been scheduled (Sundays at 12:00 AM) using dcron" |
| 45 | + |
| 46 | + # Create a script to start crond when container starts |
| 47 | + cat > /var/lib/postgresql/scripts/start_cron.sh << 'EOF' |
| 48 | +#!/bin/sh |
| 49 | +if [ -x "$(command -v crond)" ]; then |
| 50 | + echo "Starting crond service for scheduled database maintenance..." |
| 51 | + crond -b -L /var/lib/postgresql/cron.log |
| 52 | +fi |
| 53 | +EOF |
| 54 | + chmod +x /var/lib/postgresql/scripts/start_cron.sh |
| 55 | + |
| 56 | +elif command -v cron > /dev/null 2>&1; then |
| 57 | + # Debian/Ubuntu (standard cron) |
| 58 | + echo "Setting up cron job using standard cron..." |
| 59 | + (crontab -u postgres -l 2>/dev/null || echo "") | echo "$CRON_ENTRY" | crontab -u postgres - |
| 60 | + echo "Weekly VACUUM FULL job has been scheduled (Sundays at 12:00 AM) using standard cron" |
| 61 | +else |
| 62 | + echo "WARNING: No cron system found. VACUUM FULL will not be automatically scheduled." |
| 63 | +fi |
0 commit comments