Add group annotations for container, database, and volume management operations

This commit is contained in:
sha
2025-09-27 00:44:58 +03:00
parent 4817ca3d35
commit 80cefd02dd
7 changed files with 258 additions and 66 deletions
+87 -1
View File
@@ -1,6 +1,7 @@
# Universal PostgreSQL database operations
# Execute PostgreSQL SQL query
[group: 'database']
postgres-sql query service="postgres" compose-file="":
#!/usr/bin/env bash
set -euo pipefail
@@ -31,6 +32,7 @@ postgres-sql query service="postgres" compose-file="":
fi
# Check PostgreSQL connection and status
[group: 'database']
postgres-check service="postgres" compose-file="":
#!/usr/bin/env bash
set -euo pipefail
@@ -55,6 +57,7 @@ postgres-check service="postgres" compose-file="":
fi
# List PostgreSQL databases
[group: 'database']
postgres-list-databases service="postgres" compose-file="":
#!/usr/bin/env bash
set -euo pipefail
@@ -183,4 +186,87 @@ postgres-shell service="postgres" compose-file="":
just exec "$service" "psql -U postgres" "$file_arg"
else
just exec "$service" "psql -U postgres"
fi
fi
# Restore PostgreSQL database from backup file
[group: 'database']
postgres-restore backup_file service="postgres" compose-file="" backup_path="./backups":
#!/usr/bin/env bash
set -euo pipefail
backup_file="{{backup_file}}"
service="{{service}}"
compose_file="{{compose-file}}"
backup_path="{{backup_path}}"
if [ -z "$backup_file" ]; then
echo "Error: Backup file name is required" >&2
echo "Usage: just postgres-restore backup_file.sql" >&2
exit 1
fi
# Build compose file argument
file_arg=""
if [ -n "$compose_file" ]; then
file_arg="$compose_file"
fi
# Check if backup file exists
if [ ! -f "$backup_path/$backup_file" ]; then
echo -e "{{RED}}Error: Backup file '$backup_path/$backup_file' not found{{NC}}" >&2
echo -e "{{YELLOW}}Available backups:{{NC}}"
ls -la "$backup_path/" 2>/dev/null || echo "No backups directory found at $backup_path"
exit 1
fi
# Display backup file info
echo -e "{{BLUE}}Backup file information:{{NC}}"
echo -e "{{YELLOW}}File:{{NC}} $backup_path/$backup_file"
echo -e "{{YELLOW}}Size:{{NC}} $(du -h "$backup_path/$backup_file" | cut -f1)"
echo -e "{{YELLOW}}Modified:{{NC}} $(stat -c %y "$backup_path/$backup_file" 2>/dev/null || stat -f %Sm "$backup_path/$backup_file")"
echo ""
# Detect backup type
if [[ "$backup_file" == *.gz ]]; then
backup_type="gzipped SQL"
echo -e "{{BLUE}}Backup type:{{NC}} $backup_type"
else
backup_type="plain SQL"
echo -e "{{BLUE}}Backup type:{{NC}} $backup_type"
fi
echo -e "{{BLUE}}Target service:{{NC}} $service"
echo ""
echo -e "{{RED}}⚠️ WARNING: This will OVERWRITE the current database!{{NC}}"
echo -e "{{YELLOW}}This action will replace all data in the target database{{NC}}"
echo -e "{{YELLOW}}Make sure you have a backup of current data if needed{{NC}}"
echo ""
read -p "Are you sure you want to restore from '$backup_file'? Type 'yes' to continue: " confirm
if [ "$confirm" != "yes" ]; then
echo "Restore cancelled"
exit 1
fi
echo -e "{{BLUE}}Restoring database from $backup_file...{{NC}}"
# Copy backup file to container and restore
if [[ "$backup_file" == *.gz ]]; then
# For gzipped files
if [ -n "$file_arg" ]; then
just exec "$service" "gunzip -c /backups/$backup_file | psql -U postgres" "$file_arg"
else
just exec "$service" "gunzip -c /backups/$backup_file | psql -U postgres"
fi
else
# For plain SQL files
if [ -n "$file_arg" ]; then
just exec "$service" "psql -U postgres < /backups/$backup_file" "$file_arg"
else
just exec "$service" "psql -U postgres < /backups/$backup_file"
fi
fi
echo -e "{{GREEN}}✓ Database restored successfully from $backup_file{{NC}}"