This commit is contained in:
dedhersel
2026-02-04 11:53:27 +01:00
commit 562afc3e7b
17 changed files with 1016 additions and 0 deletions

60
pgm_bckp/README.md Normal file
View File

@@ -0,0 +1,60 @@
# 📦 Requirements
```bash
sudo apt update
sudo apt install mariadb-client postgresql-client
```
# ⚠️ Common Issues and Fixes
### ❌ mysqldump: command not found
### Cause: MySQL client is not installed.
Fix:
```bash
sudo apt install mariadb-client
```
### ❌ pg_dump: command not found
### Cause: PostgreSQL client is missing.
Fix:
```bash
sudo apt install postgresql-client
```
### ❌ pg_dump: error: aborting because of server version mismatch
### Cause: The local pg_dump is version 15, while the remote PostgreSQL server is version 17.
Fix: Install pg_dump version 17 from the official PostgreSQL (PGDG) repository:
```bash
# Add the official PostgreSQL repository
echo "deb http://apt.postgresql.org/pub/repos/apt/ bookworm-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/pgdg.gpg
sudo apt update
# Install the PostgreSQL 17 client
sudo apt install postgresql-client-17
```
# ⏰ Automating via Cron
Open your crontab:
```bash
crontab -e
```
Add a line like this to run the script daily at 1 AM:
```bash
0 1 * * * /path/to/backup.sh >> /path/to/logs/backup.log 2>&1
```
# 🔐 Security Tips
Restrict access to the config file containing credentials:
```bash
chmod 600 config.sh
```

48
pgm_bckp/backup.sh Normal file
View File

@@ -0,0 +1,48 @@
#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/config.sh"
# Check if required commands are available
missing=0
command -v mysqldump >/dev/null 2>&1 || { echo "❌ mysqldump not found. Install it with: sudo apt install mariadb-client or sudo apt install mysql-client"; missing=1; }
command -v pg_dump >/dev/null 2>&1 || { echo "❌ pg_dump not found. Install it with: sudo apt install postgresql-client"; missing=1; }
if [ "$missing" -eq 1 ]; then
echo "❌ Aborting: Required tools are missing."
exit 1
fi
mkdir -p "$backup_dir"
for db_id in "${database_ids[@]}"; do
IFS='|' read -r db_type db_name db_host db_port db_user db_pass <<< "${!db_id}"
timestamp=$(date +%Y%m%d%H%M%S)
backup_file="$backup_dir/$db_name-$timestamp.sql"
case "$db_type" in
mysql)
mysqldump --user="$db_user" --password="$db_pass" --host="$db_host" --port="$db_port" --databases "$db_name" > "$backup_file"
;;
postgres)
export PGPASSWORD="$db_pass"
pg_dump --username="$db_user" --host="$db_host" --port="$db_port" --format=plain --file="$backup_file" "$db_name"
;;
*)
echo "❌ Unsupported database type: $db_type for $db_name"
continue
;;
esac
if [ $? -eq 0 ]; then
echo "✅ Backup of $db_type database '$db_name' completed: $backup_file"
else
echo "❌ Error during backup of $db_type database '$db_name'"
rm -f "$backup_file"
fi
# Cleanup
find "$backup_dir" -type f -name "$db_name-*.sql" -mtime +$backup_retention_days -exec rm -f {} \;
done

13
pgm_bckp/config.sh Normal file
View File

@@ -0,0 +1,13 @@
# Backup destination and retention
backup_dir="db_backup"
backup_retention_days=7
# List of database identifiers
database_ids=("db1" "db2" "pg1")
# For each database, define its parameters
# Format: type|name|host|port|user|password
db1="mysql|db1|127.0.0.1|3306|root|rootpass"
db2="mysql|db2|10.0.0.2|3307|backup_user|backup123"
pg1="postgres|my_pgdb|192.168.1.100|5432|pguser|pgpass"