postgres service for external use
This commit is contained in:
60
scripts/backup.sh
Normal file
60
scripts/backup.sh
Normal file
@@ -0,0 +1,60 @@
|
||||
#!/bin/bash
|
||||
# PostgreSQL Backup Script for Proxmox LXC Container
|
||||
|
||||
# Load environment variables
|
||||
if [ -f ../.env ]; then
|
||||
source ../.env
|
||||
else
|
||||
echo "Error: .env file not found!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Set default values if not provided in .env
|
||||
BACKUP_DIR=${BACKUP_DIR:-"../backups"}
|
||||
POSTGRES_USER=${POSTGRES_USER:-"postgres"}
|
||||
POSTGRES_DB=${POSTGRES_DB:-"postgres"}
|
||||
BACKUP_RETENTION_DAYS=${BACKUP_RETENTION_DAYS:-7}
|
||||
|
||||
# Create backup directory if it doesn't exist
|
||||
mkdir -p ${BACKUP_DIR}
|
||||
|
||||
# Generate timestamp for backup file
|
||||
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
|
||||
BACKUP_FILE="${BACKUP_DIR}/postgres_backup_${TIMESTAMP}.sql.gz"
|
||||
|
||||
echo "Starting PostgreSQL backup..."
|
||||
echo "Database: ${POSTGRES_DB}"
|
||||
echo "Backup file: ${BACKUP_FILE}"
|
||||
|
||||
# Perform the backup
|
||||
docker exec -t postgres pg_dump -U ${POSTGRES_USER} -d ${POSTGRES_DB} | gzip > ${BACKUP_FILE}
|
||||
|
||||
# Check if backup was successful
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "Backup completed successfully: ${BACKUP_FILE}"
|
||||
|
||||
# Remove backups older than retention period
|
||||
find ${BACKUP_DIR} -name "postgres_backup_*.sql.gz" -type f -mtime +${BACKUP_RETENTION_DAYS} -delete
|
||||
echo "Cleaned up old backups (older than ${BACKUP_RETENTION_DAYS} days)"
|
||||
else
|
||||
echo "Error: Backup failed!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Print backup information
|
||||
echo "Backup size: $(du -h ${BACKUP_FILE} | cut -f1)"
|
||||
echo "Available backups:"
|
||||
ls -lh ${BACKUP_DIR}
|
||||
|
||||
# Create a full volume backup as well (for disaster recovery)
|
||||
echo "Creating volume backup..."
|
||||
VOLUME_BACKUP_FILE="${BACKUP_DIR}/postgres_volume_backup_${TIMESTAMP}.tar.gz"
|
||||
docker run --rm -v postgres_data:/data -v $(pwd)/${BACKUP_DIR}:/backup alpine tar -czf /backup/$(basename ${VOLUME_BACKUP_FILE}) /data
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "Volume backup completed successfully: ${VOLUME_BACKUP_FILE}"
|
||||
else
|
||||
echo "Warning: Volume backup failed!"
|
||||
fi
|
||||
|
||||
exit 0
|
||||
68
scripts/monitor.sh
Normal file
68
scripts/monitor.sh
Normal file
@@ -0,0 +1,68 @@
|
||||
#!/bin/bash
|
||||
# PostgreSQL Monitoring Script for Proxmox LXC Container
|
||||
|
||||
# Load environment variables
|
||||
if [ -f ../.env ]; then
|
||||
source ../.env
|
||||
else
|
||||
echo "Error: .env file not found!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Set default values if not provided in .env
|
||||
POSTGRES_USER=${POSTGRES_USER:-"postgres"}
|
||||
POSTGRES_DB=${POSTGRES_DB:-"postgres"}
|
||||
|
||||
echo "PostgreSQL Service Monitoring for Proxmox LXC"
|
||||
echo "=========================================="
|
||||
echo
|
||||
|
||||
# Check container status
|
||||
echo "Container Status:"
|
||||
docker ps --filter "name=postgres" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
|
||||
echo
|
||||
|
||||
# Check PostgreSQL service health
|
||||
echo "PostgreSQL Health Check:"
|
||||
docker exec postgres pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "PostgreSQL is accepting connections"
|
||||
else
|
||||
echo "PostgreSQL is not accepting connections"
|
||||
fi
|
||||
echo
|
||||
|
||||
# Database size information
|
||||
echo "Database Size Information:"
|
||||
docker exec postgres psql -U ${POSTGRES_USER} -d ${POSTGRES_DB} -c "SELECT datname, pg_size_pretty(pg_database_size(datname)) as size FROM pg_database ORDER BY pg_database_size(datname) DESC;"
|
||||
echo
|
||||
|
||||
# Connection information
|
||||
echo "Current Connections:"
|
||||
docker exec postgres psql -U ${POSTGRES_USER} -d ${POSTGRES_DB} -c "SELECT datname, count(*) as connections FROM pg_stat_activity GROUP BY datname;"
|
||||
echo
|
||||
|
||||
# Active queries
|
||||
echo "Active Queries (running for more than 5 seconds):"
|
||||
docker exec postgres psql -U ${POSTGRES_USER} -d ${POSTGRES_DB} -c "SELECT pid, datname, usename, application_name, client_addr, state, now() - query_start as duration, query FROM pg_stat_activity WHERE state = 'active' AND now() - query_start > interval '5 seconds';"
|
||||
echo
|
||||
|
||||
# Resource usage
|
||||
echo "Container Resource Usage:"
|
||||
docker stats postgres --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}"
|
||||
echo
|
||||
|
||||
# Disk usage
|
||||
echo "Disk Usage:"
|
||||
df -h $(docker volume inspect -f '{{ .Mountpoint }}' postgres_data)
|
||||
echo
|
||||
|
||||
# System resource usage
|
||||
echo "System Resource Usage:"
|
||||
echo "CPU:"
|
||||
top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}'
|
||||
echo "Memory:"
|
||||
free -m | awk 'NR==2{printf "Used: %s MB (%.2f%%)", $3, $3*100/$2 }'
|
||||
echo
|
||||
|
||||
exit 0
|
||||
94
scripts/restore.sh
Normal file
94
scripts/restore.sh
Normal file
@@ -0,0 +1,94 @@
|
||||
#!/bin/bash
|
||||
# PostgreSQL Restore Script for Proxmox LXC Container
|
||||
|
||||
# Check if backup file is provided
|
||||
if [ $# -ne 1 ]; then
|
||||
echo "Usage: $0 <backup-file>"
|
||||
echo "Example: $0 ../backups/postgres_backup_20250419_120000.sql.gz"
|
||||
echo " $0 ../backups/postgres_volume_backup_20250419_120000.tar.gz"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
BACKUP_FILE=$1
|
||||
|
||||
# Check if backup file exists
|
||||
if [ ! -f "${BACKUP_FILE}" ]; then
|
||||
echo "Error: Backup file '${BACKUP_FILE}' not found!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Load environment variables
|
||||
if [ -f ../.env ]; then
|
||||
source ../.env
|
||||
else
|
||||
echo "Error: .env file not found!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Set default values if not provided in .env
|
||||
POSTGRES_USER=${POSTGRES_USER:-"postgres"}
|
||||
POSTGRES_DB=${POSTGRES_DB:-"postgres"}
|
||||
|
||||
echo "Starting PostgreSQL restore..."
|
||||
echo "Database: ${POSTGRES_DB}"
|
||||
echo "Backup file: ${BACKUP_FILE}"
|
||||
|
||||
# Determine backup type (SQL dump or volume backup)
|
||||
if [[ "${BACKUP_FILE}" == *"postgres_backup_"* ]]; then
|
||||
# SQL dump restore
|
||||
# Confirm restore operation
|
||||
read -p "Warning: This will overwrite the existing database. Continue? (y/n): " -n 1 -r
|
||||
echo
|
||||
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
||||
echo "Restore operation cancelled."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Perform the restore
|
||||
echo "Restoring database from SQL dump..."
|
||||
gunzip -c ${BACKUP_FILE} | docker exec -i postgres psql -U ${POSTGRES_USER} -d ${POSTGRES_DB}
|
||||
|
||||
# Check if restore was successful
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "Restore completed successfully from: ${BACKUP_FILE}"
|
||||
else
|
||||
echo "Error: Restore failed!"
|
||||
exit 1
|
||||
fi
|
||||
elif [[ "${BACKUP_FILE}" == *"postgres_volume_backup_"* ]]; then
|
||||
# Volume backup restore
|
||||
# Confirm restore operation
|
||||
read -p "WARNING: This will completely replace the PostgreSQL data volume. All current data will be lost. Continue? (y/n): " -n 1 -r
|
||||
echo
|
||||
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
||||
echo "Restore operation cancelled."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "Stopping PostgreSQL container..."
|
||||
docker-compose down
|
||||
|
||||
echo "Restoring volume data from backup..."
|
||||
# Create a temporary container to restore the volume
|
||||
docker run --rm -v postgres_data:/data -v $(pwd)/$(dirname ${BACKUP_FILE}):/backup alpine sh -c "rm -rf /data/* && tar -xzf /backup/$(basename ${BACKUP_FILE}) -C / --strip-components=1"
|
||||
|
||||
# Check if restore was successful
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "Volume restore completed successfully from: ${BACKUP_FILE}"
|
||||
echo "Starting PostgreSQL container..."
|
||||
docker-compose up -d
|
||||
echo "Waiting for PostgreSQL to start..."
|
||||
sleep 10
|
||||
echo "PostgreSQL service restored and running."
|
||||
else
|
||||
echo "Error: Volume restore failed!"
|
||||
echo "Starting PostgreSQL container..."
|
||||
docker-compose up -d
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
echo "Error: Unknown backup file format. Expected postgres_backup_*.sql.gz or postgres_volume_backup_*.tar.gz"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exit 0
|
||||
21
scripts/setup.sh
Normal file
21
scripts/setup.sh
Normal file
@@ -0,0 +1,21 @@
|
||||
#!/bin/bash
|
||||
# Setup script for PostgreSQL service
|
||||
|
||||
# Make scripts executable
|
||||
chmod +x ./backup.sh
|
||||
chmod +x ./restore.sh
|
||||
chmod +x ./monitor.sh
|
||||
|
||||
# Create necessary directories
|
||||
mkdir -p ../backups
|
||||
mkdir -p ../logs
|
||||
|
||||
# Copy environment file if it doesn't exist
|
||||
if [ ! -f ../.env ]; then
|
||||
cp ../.env.example ../.env
|
||||
echo "Created .env file from .env.example"
|
||||
echo "Please update the .env file with your configuration"
|
||||
fi
|
||||
|
||||
echo "Setup completed successfully!"
|
||||
echo "You can now start the PostgreSQL service with: docker-compose up -d"
|
||||
Reference in New Issue
Block a user