Seup the task on coolify to dump and then add crontab
Last active
January 31, 2025 04:59
-
-
Save nathanialhenniges/35e460f9a753af57e5d89ef346ea9a8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # Configuration | |
| BACKUP_DIR="/home/postgres/backup/data" # Path to existing backup directory | |
| BUCKET_NAME="your-bucket-name" | |
| RCLONE_REMOTE="cf-r2" # Must match the remote in rclone config | |
| TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S") | |
| DISCORD_TIMESTAMP=$(date -u +%s) | |
| WEBHOOK_URL="" | |
| # Ensure the backup directory exists | |
| if [ ! -d "$BACKUP_DIR" ]; then | |
| echo "❌ Backup directory does not exist: $BACKUP_DIR" | |
| STATUS="❌ Backup directory not found" | |
| COLOR=16711680 # Red | |
| else | |
| # Sync to Cloudflare R2 | |
| rclone sync "$BACKUP_DIR" "$RCLONE_REMOTE:$BUCKET_NAME" | |
| if [ $? -eq 0 ]; then | |
| STATUS="✅ Backup sync successful" | |
| COLOR=65280 # Green | |
| else | |
| STATUS="❌ Backup sync failed" | |
| COLOR=16711680 # Red | |
| fi | |
| fi | |
| # Properly formatted JSON payload | |
| JSON_PAYLOAD=$( | |
| cat <<EOF | |
| { | |
| "embeds": [ | |
| { | |
| "title": "$STATUS", | |
| "description": "Backup sync to R2 was $(if [ $? -eq 0 ]; then echo 'successful'; else echo 'unsuccessful'; fi).", | |
| "color": $COLOR, | |
| "fields": [ | |
| { | |
| "name": "Frequency", | |
| "value": "Weekly on $(date -u +"%A")", | |
| "inline": true | |
| }, | |
| { | |
| "name": "Time Executed", | |
| "value": "<t:$DISCORD_TIMESTAMP:R>", | |
| "inline": true | |
| } | |
| ] | |
| } | |
| ] | |
| } | |
| EOF | |
| ) | |
| # Send webhook to Discord | |
| curl -H "Content-Type: application/json" -X POST -d "$JSON_PAYLOAD" "$WEBHOOK_URL" | |
| echo "Backup sync process completed." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # Configuration | |
| BACKUP_DIR="/home/postgres/backup/data" | |
| DB_NAME="postgres" | |
| DB_USER="postgres" | |
| DB_PASSWORD="twenty" | |
| TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S") | |
| DISCORD_TIMESTAMP=$(date -u +%s) | |
| BACKUP_FILE="${BACKUP_DIR}/postgres_backup_${TIMESTAMP}.sql" | |
| WEBHOOK_URL="" | |
| # Export the password for pg_dump | |
| export PGPASSWORD="$DB_PASSWORD" | |
| # Ensure the backup directory exists | |
| mkdir -p "$BACKUP_DIR" | |
| # Check if PostgreSQL is ready | |
| pg_isready -U "$DB_USER" -h localhost -d "$DB_NAME" | |
| if [ $? -ne 0 ]; then | |
| STATUS="❌ Database dump unsuccessful" | |
| COLOR=16711680 # Red | |
| SUCCESS="unsuccessful" | |
| else | |
| # Backup the database | |
| pg_dump -U "$DB_USER" -h localhost -d "$DB_NAME" -F c -f "$BACKUP_FILE" | |
| if [ $? -eq 0 ]; then | |
| STATUS="✅ Database dump successful" | |
| COLOR=65280 # Green | |
| SUCCESS="successful" | |
| else | |
| STATUS="❌ Database dump unsuccessful" | |
| COLOR=16711680 # Red | |
| SUCCESS="unsuccessful" | |
| fi | |
| fi | |
| # Properly formatted JSON payload | |
| JSON_PAYLOAD=$( | |
| cat <<EOF | |
| { | |
| "embeds": [ | |
| { | |
| "title": "$STATUS", | |
| "description": "Database dump for CRM was $SUCCESS.", | |
| "color": $COLOR, | |
| "fields": [ | |
| { | |
| "name": "Frequency", | |
| "value": "Weekly on $(date -u +"%A")", | |
| "inline": true | |
| }, | |
| { | |
| "name": "Time Executed", | |
| "value": "<t:$DISCORD_TIMESTAMP:R>", | |
| "inline": true | |
| } | |
| ] | |
| } | |
| ] | |
| } | |
| EOF | |
| ) | |
| # Send webhook to Discord | |
| curl -H "Content-Type: application/json" -X POST -d "$JSON_PAYLOAD" "$WEBHOOK_URL" | |
| echo "Backup completed: $BACKUP_FILE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment