Auto-commit from giteapush.sh at 2025-05-01 05:32:48

This commit is contained in:
DocTator 2025-05-01 05:32:48 -04:00
parent 9f98e5c56b
commit f090861aaf
5 changed files with 237 additions and 66 deletions

View File

@ -1,66 +0,0 @@
#!/bin/bash
# === CONFIG ===
REPLICA_SERVER="" # <-- fill this in
SSH_USER="" # <-- fill this in
PG_USER="postgres" # usually postgres
# === Connect and check SSH first ===
echo "[*] Connecting to replica server $REPLICA_SERVER..."
ssh -o BatchMode=yes -o ConnectTimeout=5 "$SSH_USER@$REPLICA_SERVER" "echo 2>&1" && SSH_OK=true || SSH_OK=false
if [[ "$SSH_OK" == false ]]; then
echo "❌ SSH connection failed! Could not reach $REPLICA_SERVER."
echo "👉 Suggested Action: Check server DNS, IP, SSH service, firewall."
exit 2
fi
# === Run the Replication Check ===
OUTPUT=$(ssh "doc@38.102.127.174" "sudo -u $PG_USER psql -Atc \"SELECT application_name, state, sync_state, write_lag, flush_lag, replay_lag FROM pg_stat_replication;\"")
if [[ -z "$OUTPUT" ]]; then
echo "❌ No replication data found!"
echo "👉 Suggested Action: Check PostgreSQL service on both primary and replica. Verify replication setup."
exit 1
fi
# === Parse and Advise ===
echo ""
echo "=== Replication Status ==="
echo "$OUTPUT"
echo "==========================="
while IFS='|' read -r application_name state sync_state write_lag flush_lag replay_lag; do
echo ""
echo "🛠 Replica: $application_name"
echo "🔹 Connection State: $state"
echo "🔹 Sync State: $sync_state"
echo "🔹 Write Lag: $write_lag"
echo "🔹 Flush Lag: $flush_lag"
echo "🔹 Replay Lag: $replay_lag"
if [[ "$state" == "streaming" && "$sync_state" == "sync" ]]; then
echo "✅ Status: Healthy synchronous replication. No action needed."
elif [[ "$state" == "streaming" && "$sync_state" == "async" ]]; then
echo "⚠️ Warning: Replica is asynchronous. Check if this is expected."
elif [[ "$state" == "catchup" ]]; then
echo "⚠️ Warning: Replica is catching up. Monitor closely."
else
echo "❌ Error: Unknown or broken state. Immediate action required."
fi
if [[ "$write_lag" != "" && "$write_lag" != "0" ]]; then
echo "⚠️ Write Lag Detected: $write_lag"
fi
if [[ "$flush_lag" != "" && "$flush_lag" != "0" ]]; then
echo "⚠️ Flush Lag Detected: $flush_lag"
fi
if [[ "$replay_lag" != "" && "$replay_lag" != "0" ]]; then
echo "⚠️ Replay Lag Detected: $replay_lag"
fi
done <<< "$OUTPUT"
echo ""
echo "[✓] Replica check complete."

View File

@ -0,0 +1,64 @@
#!/bin/bash
# === Prompt for Target ===
read -p "Enter SSH username: " USERNAME
read -p "Enter server hostname (e.g. krang.internal): " HOSTNAME
REMOTE="$USERNAME@$HOSTNAME"
# === Alert Config (local alerts) ===
TELEGRAM_BOT_TOKEN="8178867489:AAH0VjN7VnZSCIWasSz_y97iBLLjPJA751k"
TELEGRAM_CHAT_ID="1559582356"
MASTODON_INSTANCE="https://chatwithus.live"
MASTODON_TOKEN="rimxBLi-eaJAcwagkmoj6UoW7Lc473tQY0cOM041Euw"
TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
# === Remote Disk Check + Cleanup Script ===
REMOTE_SCRIPT=$(cat << 'EOF'
#!/bin/bash
THRESHOLD_PERCENT=15
HOST=$(hostname)
ALERTED=false
df -h --output=target,pcent | tail -n +2 | while read -r mount usage; do
percent=$(echo "$usage" | tr -d '%')
if [ "$percent" -ge $((100 - THRESHOLD_PERCENT)) ]; then
echo "[!] $HOST: Low space on $mount ($usage used). Running cleanup..."
apt-get clean -y > /dev/null 2>&1
journalctl --vacuum-time=3d > /dev/null 2>&1
docker system prune -af --volumes > /dev/null 2>&1
rm -rf /tmp/* /var/tmp/*
echo "[✓] $HOST: Cleanup complete for $mount"
else
echo "[OK] $HOST: $mount has enough space ($usage used)"
fi
done
EOF
)
# === Run Remote Script via SSH ===
echo "[*] Connecting to $REMOTE..."
OUTPUT=$(ssh "$REMOTE" "$REMOTE_SCRIPT")
# === Log and Notify ===
echo "[$TIMESTAMP] === Remote Disk Check on $HOSTNAME ===" >> /var/log/disk_mitigator.log
echo "$OUTPUT" >> /var/log/disk_mitigator.log
# Alert if low space was found
if echo "$OUTPUT" | grep -q "\[!\]"; then
MSG="⚠️ Disk cleanup triggered on $HOSTNAME via Krang.\n\n$OUTPUT"
# Send alerts
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-d chat_id="$TELEGRAM_CHAT_ID" \
-d text="$MSG" > /dev/null
curl -s -X POST "$MASTODON_INSTANCE/api/v1/statuses" \
-H "Authorization: Bearer $MASTODON_TOKEN" \
-d "status=$MSG" \
-d "visibility=unlisted" > /dev/null
fi
echo "[✓] Done. Output logged and alerts (if any) sent."

View File

@ -0,0 +1,169 @@
# 🐍 Python Virtual Environment Backup Script
This Bash script automates the process of backing up Python virtual environments. It performs the following steps:
1. Locates virtual environments in a specified root directory
2. Archives them into a compressed `.tar.gz` file
3. Transfers the backup to a remote server using `rsync`
4. Deletes local backups older than a specified retention period
---
## 📦 Features
- Detects virtual environments by checking for `bin/activate`
- Compresses selected environments into a tarball
- Transfers the backup to a remote server via SSH
- Deletes backups older than a retention threshold
- Uses strict error handling to avoid silent failures
---
## ⚙️ Configuration
| Variable | Description |
|------------------|-------------------------------------------------------------------------|
| `VENV_ROOT` | Directory where virtual environments are stored |
| `BACKUP_DIR` | Local directory where the backup tarballs are saved |
| `RETENTION_DAYS` | Number of days to retain local backups |
| `REMOTE_USER` | SSH user for the remote backup server |
| `REMOTE_HOST` | Hostname or IP address of the remote server |
| `REMOTE_PATH` | Remote directory where backups will be copied via `rsync` |
---
## 📝 Script Walkthrough
### 1. Setup
```bash
#!/usr/bin/env bash
set -euo pipefail
```
- Enables strict error handling to prevent unnoticed failures.
---
### 2. Define Variables
```bash
VENV_ROOT="/home/doc"
BACKUP_DIR="$VENV_ROOT/backups"
RETENTION_DAYS=7
REMOTE_USER="root"
REMOTE_HOST="thevault.bounceme.net"
REMOTE_PATH="/mnt/backup3/pythonvenvs"
```
---
### 3. Generate Timestamped Backup Filename
```bash
DATE=$(date +'%F_%H-%M-%S')
BACKUP_FILE="$BACKUP_DIR/venvs_backup_$DATE.tar.gz"
```
---
### 4. Ensure Backup Directory Exists
```bash
mkdir -p "$BACKUP_DIR"
```
---
### 5. Locate Virtual Environments
```bash
mapfile -t VENV_DIRS < <(
find "$VENV_ROOT" -maxdepth 1 -type d \
-exec test -f "{}/bin/activate" \; -print
)
```
If no environments are found, the script exits:
```bash
if [ ${#VENV_DIRS[@]} -eq 0 ]; then
echo "❌ No virtual environments found under $VENV_ROOT"
exit 1
fi
```
---
### 6. Extract Environment Names and Archive
```bash
VENV_NAMES=()
for path in "${VENV_DIRS[@]}"; do
VENV_NAMES+=( "$(basename "$path")" )
done
echo "🔄 Backing up virtual environments: ${VENV_NAMES[*]}"
tar czf "$BACKUP_FILE" -C "$VENV_ROOT" "${VENV_NAMES[@]}"
echo "✅ Local backup saved to $BACKUP_FILE"
```
---
### 7. Sync Backup to Remote Server
```bash
echo "📡 Sending backup to ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}/"
rsync -az --progress "$BACKUP_FILE" \
"${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}/"
```
Check if sync was successful:
```bash
if [ $? -ne 0 ]; then
echo "❌ Remote sync failed!"
exit 1
else
echo "✅ Remote sync succeeded."
fi
```
---
### 8. Cleanup Old Backups
```bash
echo "🗑️ Removing local backups older than $RETENTION_DAYS days..."
find "$BACKUP_DIR" -type f -name "venvs_backup_*.tar.gz" \
-mtime +$RETENTION_DAYS -delete
echo "🎉 Backup and cleanup complete."
```
---
## 🧪 Example Output
```text
🔄 Backing up virtual environments: venv1 venv2
✅ Local backup saved to /home/doc/backups/venvs_backup_2025-05-01_02-00-00.tar.gz
📡 Sending backup to root@thevault.bounceme.net:/mnt/backup3/pythonvenvs/
✅ Remote sync succeeded.
🗑️ Removing local backups older than 7 days...
🎉 Backup and cleanup complete.
```
---
## 🛡️ Notes
- Ensure SSH access is set up for passwordless login to the remote host.
- Confirm that `rsync` is installed on both the local and remote systems.
- Consider running this script via `cron` or `systemd` for automation.
---
## 🖋️ Author
Maintained by **Doc**. Custom backup tooling for Genesis Hosting infrastructure.

View File

@ -1109,3 +1109,7 @@ paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
Failed to send Mastodon DM (attempt 1): {"error":"The access token is invalid"} Failed to send Mastodon DM (attempt 1): {"error":"The access token is invalid"}
Failed to send Mastodon DM (attempt 2): {"error":"The access token is invalid"} Failed to send Mastodon DM (attempt 2): {"error":"The access token is invalid"}
Failed to send Mastodon DM (attempt 3): {"error":"The access token is invalid"} Failed to send Mastodon DM (attempt 3): {"error":"The access token is invalid"}
✅ Genesis Radio Healthcheck 2025-05-01 05:30:17: All systems normal.
Failed to send Mastodon DM (attempt 1): {"error":"The access token is invalid"}
Failed to send Mastodon DM (attempt 2): {"error":"The access token is invalid"}
Failed to send Mastodon DM (attempt 3): {"error":"The access token is invalid"}