### Docker Compose Installation for PG Back Web Source: https://github.com/eduardolat/pgbackweb/blob/main/README.md Example Docker Compose configuration for running PG Back Web with PostgreSQL. It specifies image, ports, volumes, environment variables, and service dependencies. Ensure to replace sensitive information like encryption keys and connection strings with your own. ```yaml services: pgbackweb: image: eduardolat/pgbackweb:latest ports: - "8085:8085" # Access the web interface at http://localhost:8085 volumes: - ./backups:/backups # If you only use S3 destinations, you don't need this volume environment: # Optional environment variables are ignored, see the configuration section below for more details PBW_ENCRYPTION_KEY: "my_secret_key" # Change this to a strong key PBW_POSTGRES_CONN_STRING: "postgresql://postgres:password@postgres:5432/pgbackweb?sslmode=disable" depends_on: postgres: condition: service_healthy postgres: image: postgres:18 environment: POSTGRES_USER: postgres POSTGRES_DB: pgbackweb POSTGRES_PASSWORD: password ports: - "5432:5432" volumes: - ./data:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 5s timeout: 5s retries: 5 ``` -------------------------------- ### Build Go Binary and Frontend Assets Source: https://github.com/eduardolat/pgbackweb/blob/main/AGENTS.md Builds the Go binary for the application and compiles frontend assets. This is a core command for creating a deployable version of the application. ```bash task build ``` -------------------------------- ### Load Application Configuration in Go Source: https://context7.com/eduardolat/pgbackweb/llms.txt Loads environment variables for application configuration using the 'config' package. It validates required variables like encryption key and database connection string, and prints server listening details. Dependencies include the 'fmt' and 'github.com/eduardolat/pgbackweb/internal/config' packages. ```go package main import ( "fmt" "github.com/eduardolat/pgbackweb/internal/config" ) func loadConfiguration() error { env, err := config.GetEnv() if err != nil { return fmt.Errorf("configuration error: %w", err) } fmt.Printf("Server will listen on %s:%s\n", env.PBW_LISTEN_HOST, env.PBW_LISTEN_PORT) fmt.Printf("Database: %s\n", env.PBW_POSTGRES_CONN_STRING) // Required environment variables: // - PBW_ENCRYPTION_KEY: Encryption key for sensitive data // - PBW_POSTGRES_CONN_STRING: PostgreSQL connection string // Optional environment variables: // - PBW_LISTEN_HOST: Listen host (default: 0.0.0.0) // - PBW_LISTEN_PORT: Listen port (default: 8085) // - PBW_PATH_PREFIX: URL path prefix (default: empty) // - TZ: Timezone (default: UTC) return nil } ``` -------------------------------- ### Webhook Service Configuration and Event Triggering in Go Source: https://context7.com/eduardolat/pgbackweb/llms.txt This Go code snippet demonstrates how to set up and manage webhooks for various backup lifecycle events using the pgbackweb webhook service. It shows how to create a new webhook with specific event types and target details, and how to manually trigger an 'execution_success' event. The service supports multiple event types including backup status, database health, and destination health. ```go package main import ( "context" "fmt" "github.com/eduardolat/pgbackweb/internal/database/dbgen" "github.com/eduardolat/pgbackweb/internal/service/webhooks" "github.com/google/uuid" ) func setupWebhooks(service *webhooks.Service) error { ctx := context.Background() backupID := uuid.MustParse("770e8400-e29b-41d4-a716-446655440000") // Create webhook for backup success webhook, err := service.CreateWebhook(ctx, dbgen.WebhooksServiceCreateWebhookParams{ Name: "slack-notification", URL: "https://hooks.slack.com/services/T00/B00/XXXX", EventType: "execution_success", IsActive: true, TargetType: "backup", TargetID: backupID, }) if err != nil { return fmt.Errorf("failed to create webhook: %w", err) } fmt.Printf("Webhook created: %s\n", webhook.ID) // Trigger webhook manually (usually triggered by system events) service.RunExecutionSuccess(backupID) // Available event types: // - execution_success: Backup completed successfully // - execution_failed: Backup failed // - database_healthy: Database health check passed // - database_unhealthy: Database health check failed // - destination_healthy: Storage destination healthy // - destination_unhealthy: Storage destination unhealthy return nil } func listEventTypes() { // Available webhook event types events := webhooks.FullEventTypes for key, name := range events { fmt.Printf("%s: %s\n", key, name) } } ``` -------------------------------- ### Execute Backup Immediately - Go Source: https://context7.com/eduardolat/pgbackweb/llms.txt Initiates an immediate backup execution for a specified backup ID. It then retrieves and prints the status and size of the latest execution. Requires the 'executions.Service' and 'uuid' packages. ```go package main import ( "context" "fmt" "github.com/eduardolat/pgbackweb/internal/service/executions" "github.com/google/uuid" ) func executeBackup(service *executions.Service) error { ctx := context.Background() backupID := uuid.MustParse("770e8400-e29b-41d4-a716-446655440000") err := service.RunExecution(ctx, backupID) if err != nil { return fmt.Errorf("backup execution failed: %w", err) } // Check execution status executions, err := service.ListBackupExecutions(ctx, backupID) if err != nil { return fmt.Errorf("failed to list executions: %w", err) } if len(executions) > 0 { latest := executions[0] fmt.Printf("Execution %s completed with status: %s\n", latest.ID, latest.Status) if latest.FileSize.Valid { fmt.Printf("Backup size: %d bytes\n", latest.FileSize.Int64) } } return nil } ``` -------------------------------- ### Local Filesystem Operations with Storage Client in Go Source: https://context7.com/eduardolat/pgbackweb/llms.txt This Go code snippet illustrates how to manage backup files on the local filesystem using the storage client. It supports uploading files with automatic directory creation, retrieving the full path of a stored file, and deleting files. The function takes a relative path and a reader for the backup data as input. ```go package main import ( "bytes" "fmt" "github.com/eduardolat/pgbackweb/internal/integration/storage" ) func manageLocalBackups() error { client := storage.New() // Upload to local storage backupData := bytes.NewReader([]byte("backup content")) relativePath := "production/2024-12-30/dump-20241230-143022.zip" fileSize, err := client.LocalUpload(relativePath, backupData) if err != nil { return fmt.Errorf("local upload failed: %w", err) } fmt.Printf("Saved %d bytes locally\n", fileSize) // Get full filesystem path fullPath := client.LocalGetFullPath(relativePath) fmt.Printf("Full path: %s\n", fullPath) // /backups/production/2024-12-30/dump-20241230-143022.zip // Delete backup if err := client.LocalDelete(relativePath); err != nil { return fmt.Errorf("deletion failed: %w", err) } return nil } ``` -------------------------------- ### Run All Project Tests Source: https://github.com/eduardolat/pgbackweb/blob/main/AGENTS.md Executes all unit and integration tests defined within the project. This command is crucial for verifying the correctness of code changes. ```bash task test ``` -------------------------------- ### Production Deployment with Docker Compose Source: https://context7.com/eduardolat/pgbackweb/llms.txt Provides a complete Docker Compose configuration for deploying PG Back Web in a production environment. It sets up the pgbackweb service and a PostgreSQL database, defining volumes, environment variables, and network ports. The deployment requires a 'latest' pgbackweb image and a PostgreSQL image, with the pgbackweb service depending on a healthy postgres service. ```yaml services: pgbackweb: image: eduardolat/pgbackweb:latest ports: - "8085:8085" volumes: - ./backups:/backups environment: PBW_ENCRYPTION_KEY: "strong-random-encryption-key-here" PBW_POSTGRES_CONN_STRING: "postgresql://postgres:password@postgres:5432/pgbackweb?sslmode=disable" PBW_LISTEN_HOST: "0.0.0.0" PBW_LISTEN_PORT: "8085" TZ: "America/New_York" depends_on: postgres: condition: service_healthy restart: unless-stopped postgres: image: postgres:18 environment: POSTGRES_USER: postgres POSTGRES_DB: pgbackweb POSTGRES_PASSWORD: password ports: - "5432:5432" volumes: - ./data:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 5s timeout: 5s retries: 5 restart: unless-stopped ``` -------------------------------- ### Create PostgreSQL Database Connection - Go Source: https://context7.com/eduardolat/pgbackweb/llms.txt Registers a new PostgreSQL database connection for backup management. This function tests the connection before storing the database credentials within the system. It requires a `databases.Service` instance and `dbgen.DatabasesServiceCreateDatabaseParams` containing database details. ```go package main import ( "context" "fmt" "github.com/eduardolat/pgbackweb/internal/database/dbgen" "github.com/eduardolat/pgbackweb/internal/service/databases" ) func createDatabase(service *databases.Service) error { ctx := context.Background() db, err := service.CreateDatabase(ctx, dbgen.DatabasesServiceCreateDatabaseParams{ Name: "production-db", PgVersion: "16", ConnectionString: "postgresql://user:pass@localhost:5432/mydb?sslmode=require", }) if err != nil { return fmt.Errorf("failed to create database: %w", err) } fmt.Printf("Database created with ID: %s\n", db.ID) return nil } ``` -------------------------------- ### Restore Database from Backup - Go Source: https://context7.com/eduardolat/pgbackweb/llms.txt Restores a database from a specified backup execution. Supports restoring to a pre-registered database connection or to an ad-hoc connection string. Requires the 'restorations.Service' and 'uuid' packages. ```go package main import ( "context" "database/sql" "fmt" "github.com/eduardolat/pgbackweb/internal/service/restorations" "github.com/google/uuid" ) func restoreBackup(service *restorations.Service) error { ctx := context.Background() executionID := uuid.MustParse("880e8400-e29b-41d4-a716-446655440000") // Option 1: Restore to registered database databaseID := uuid.NullUUID{ UUID: uuid.MustParse("550e8400-e29b-41d4-a716-446655440000"), Valid: true, } err := service.RunRestoration(ctx, executionID, databaseID, "") if err != nil { return fmt.Errorf("restoration failed: %w", err) } // Option 2: Restore to ad-hoc connection string customConnString := "postgresql://user:pass@restore-host:5432/restored_db" err = service.RunRestoration(ctx, executionID, uuid.NullUUID{}, customConnString) if err != nil { return fmt.Errorf("restoration to custom db failed: %w", err) } fmt.Println("Database restored successfully") return nil } ``` -------------------------------- ### S3 Operations with Storage Client in Go Source: https://context7.com/eduardolat/pgbackweb/llms.txt This Go code snippet demonstrates how to interact with S3-compatible storage for uploading, downloading, and deleting backup files. It requires S3 credentials, region, endpoint, and bucket name as input. The function tests the connection, uploads a file, generates a presigned download link with a specified expiry, and then deletes the file. ```go package main import ( "bytes" "fmt" "time" "github.com/eduardolat/pgbackweb/internal/integration/storage" ) func manageS3Backups() error { client := storage.New() // S3 credentials accessKey := "AKIAIOSFODNN7EXAMPLE" secretKey := "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" region := "us-east-1" endpoint := "https://s3.amazonaws.com" bucket := "my-backups" // Test connection if err := client.S3Test(accessKey, secretKey, region, endpoint, bucket); err != nil { return fmt.Errorf("S3 connection failed: %w", err) } // Upload backup backupData := bytes.NewReader([]byte("backup content")) key := "backups/2024-12-30/dump-20241230-143022.zip" fileSize, err := client.S3Upload(accessKey, secretKey, region, endpoint, bucket, key, backupData) if err != nil { return fmt.Errorf("upload failed: %w", err) } fmt.Printf("Uploaded %d bytes to %s\n", fileSize, key) // Generate download link (valid for 1 hour) downloadURL, err := client.S3GetDownloadLink( accessKey, secretKey, region, endpoint, bucket, key, 1*time.Hour, ) if err != nil { return fmt.Errorf("failed to generate download link: %w", err) } fmt.Printf("Download URL: %s\n", downloadURL) // Delete backup if err := client.S3Delete(accessKey, secretKey, region, endpoint, bucket, key); err != nil { return fmt.Errorf("deletion failed: %w", err) } return nil } ``` -------------------------------- ### Database Service - Create Database Connection Source: https://context7.com/eduardolat/pgbackweb/llms.txt Register a new PostgreSQL database for backup management. This API tests the database connection before storing the credentials. ```APIDOC ## POST /api/databases ### Description Registers a new PostgreSQL database connection for backup management. The service will test the connection before storing the credentials. ### Method POST ### Endpoint /api/databases ### Parameters #### Request Body - **name** (string) - Required - The name of the database connection. - **pg_version** (string) - Required - The PostgreSQL version (e.g., "16"). - **connection_string** (string) - Required - The PostgreSQL connection string (e.g., "postgresql://user:pass@host:port/dbname?sslmode=require"). ### Request Example ```json { "name": "production-db", "pg_version": "16", "connection_string": "postgresql://user:pass@localhost:5432/mydb?sslmode=require" } ``` ### Response #### Success Response (200) - **id** (string) - The unique identifier for the created database connection. - **name** (string) - The name of the database. - **pg_version** (string) - The PostgreSQL version. - **created_at** (string) - The timestamp when the database connection was created. #### Response Example ```json { "id": "a1b2c3d4-e5f6-7890-1234-567890abcdef", "name": "production-db", "pg_version": "16", "created_at": "2023-10-27T10:00:00Z" } ``` ``` -------------------------------- ### Run Database Migrations with Goose Source: https://github.com/eduardolat/pgbackweb/blob/main/AGENTS.md Applies database schema migrations using the Goose migration tool. This is essential for setting up or updating the database schema. ```bash task goose -- up ``` -------------------------------- ### List Available Taskfile Commands Source: https://github.com/eduardolat/pgbackweb/blob/main/CONTRIBUTING.md Lists all available commands defined in the Taskfile for project management. This command is essential for understanding and executing various development tasks within the project. ```bash task --list ``` -------------------------------- ### PostgreSQL Database Dump Operations - Go Source: https://context7.com/eduardolat/pgbackweb/llms.txt Performs low-level PostgreSQL database dump operations using version-specific binaries. Includes parsing versions, testing connections, and creating compressed dumps with custom parameters. Requires the 'postgres' integration package. ```go package main import ( "fmt" "io" "os" "github.com/eduardolat/pgbackweb/internal/integration/postgres" ) func dumpDatabase() error { client := postgres.New() // Parse PostgreSQL version version, err := client.ParseVersion("16") if err != nil { return fmt.Errorf("invalid pg version: %w", err) } connString := "postgresql://user:pass@localhost:5432/mydb" // Test connection if err := client.Test(version, connString); err != nil { return fmt.Errorf("connection test failed: %w", err) } // Create backup with custom options dumpReader := client.DumpZip(version, connString, postgres.DumpParams{ DataOnly: false, SchemaOnly: false, Clean: true, IfExists: true, Create: false, NoComments: true, }) // Save to file outFile, err := os.Create("/backups/dump.zip") if err != nil { return fmt.Errorf("failed to create file: %w", err) } defer outFile.Close() written, err := io.Copy(outFile, dumpReader) if err != nil { return fmt.Errorf("failed to write dump: %w", err) } fmt.Printf("Backup completed: %d bytes written\n", written) return nil } ``` -------------------------------- ### Create S3 Storage Destination - Go Source: https://context7.com/eduardolat/pgbackweb/llms.txt Configures an S3-compatible storage destination for storing backup files. The function validates access to the specified S3 bucket before saving the destination configuration. It requires a `destinations.Service` instance and `dbgen.DestinationsServiceCreateDestinationParams` with S3 credentials and bucket information. ```go package main import ( "context" "fmt" "github.com/eduardolat/pgbackweb/internal/database/dbgen" "github.com/eduardolat/pgbackweb/internal/service/destinations" ) func createS3Destination(service *destinations.Service) error { ctx := context.Background() dest, err := service.CreateDestination(ctx, dbgen.DestinationsServiceCreateDestinationParams{ Name: "aws-backup-bucket", AccessKey: "AKIAIOSFODNN7EXAMPLE", SecretKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", Region: "us-east-1", Endpoint: "https://s3.amazonaws.com", BucketName: "my-postgres-backups", }) if err != nil { return fmt.Errorf("failed to create destination: %w", err) } fmt.Printf("Destination created with ID: %s\n", dest.ID) return nil } ``` -------------------------------- ### Run Project Linters Source: https://github.com/eduardolat/pgbackweb/blob/main/AGENTS.md Runs linters against the project's codebase to check for code style violations and potential errors. This helps maintain code quality and consistency. ```bash task lint ``` -------------------------------- ### Regenerate SQLC Database Code Source: https://github.com/eduardolat/pgbackweb/blob/main/AGENTS.md Regenerates Go code from SQL queries using SQLC. This command should be run after any changes are made to the SQL files to ensure the Go code is up-to-date. ```bash task gen:db ``` -------------------------------- ### Reset PG Back Web Password via Docker Exec Source: https://context7.com/eduardolat/pgbackweb/llms.txt Demonstrates how to reset the application's password using the `docker exec` command. This utility allows for interactive password changes by executing the `change-password` command within the running pgbackweb container. The process involves following on-screen prompts to enter and confirm the new password. ```bash # Reset password using Docker exec docker exec -it pgbackweb sh -c change-password # Follow the interactive prompts: # Enter new password: ******** # Confirm password: ******** # Password updated successfully ``` -------------------------------- ### Backup Service - Create Scheduled Backup Job Source: https://context7.com/eduardolat/pgbackweb/llms.txt Configure a scheduled backup job, specifying the database, destination, cron schedule, and pg_dump options. The system automatically schedules the backup execution. ```APIDOC ## POST /api/backups/schedule ### Description Configures a scheduled backup job. This endpoint allows specifying the source database, destination storage, cron schedule for execution, timezone, retention policy, and various pg_dump options. ### Method POST ### Endpoint /api/backups/schedule ### Parameters #### Request Body - **name** (string) - Required - The name of the backup job. - **database_id** (string) - Required - The ID of the database to back up. - **destination_id** (string) - Required - The ID of the storage destination. - **is_active** (boolean) - Required - Whether the backup job is active. - **cron_expression** (string) - Required - The cron schedule for the backup (e.g., "0 2 * * *"). - **time_zone** (string) - Required - The timezone for the schedule (e.g., "America/New_York"). - **retention** (integer) - Required - The number of days to retain backups. - **opt_data_only** (boolean) - Optional - If true, only backup data is included. - **opt_schema_only** (boolean) - Optional - If true, only the schema is backed up. - **opt_clean** (boolean) - Optional - If true, drops database objects before recreating them. - **opt_if_exists** (boolean) - Optional - If true, and `opt_clean` is also true, drops existing database objects. - **opt_create** (boolean) - Optional - If true, creates the database before connecting. - **opt_no_comments** (boolean) - Optional - If true, does not include comments in the backup. ### Request Example ```json { "name": "nightly-production-backup", "database_id": "550e8400-e29b-41d4-a716-446655440000", "destination_id": "660e8400-e29b-41d4-a716-446655440000", "is_active": true, "cron_expression": "0 2 * * *", "time_zone": "America/New_York", "retention": 30, "opt_clean": true, "opt_if_exists": true, "opt_no_comments": true } ``` ### Response #### Success Response (200) - **id** (string) - The unique identifier for the scheduled backup job. - **name** (string) - The name of the backup job. - **database_id** (string) - The ID of the database being backed up. - **destination_id** (string) - The ID of the storage destination. - **is_active** (boolean) - Indicates if the job is active. - **cron_expression** (string) - The cron schedule. - **next_execution** (string) - The timestamp of the next scheduled execution. #### Response Example ```json { "id": "c3d4e5f6-a7b8-9012-3456-7890abcdef12", "name": "nightly-production-backup", "database_id": "550e8400-e29b-41d4-a716-446655440000", "destination_id": "660e8400-e29b-41d4-a716-446655440000", "is_active": true, "cron_expression": "0 2 * * *", "next_execution": "2023-10-28T02:00:00Z" } ``` ``` -------------------------------- ### Destination Service - Create S3 Storage Destination Source: https://context7.com/eduardolat/pgbackweb/llms.txt Configure an S3-compatible storage destination for backup files. The service validates bucket access before saving the destination configuration. ```APIDOC ## POST /api/destinations ### Description Configures an S3-compatible storage destination for backup files. The service validates access to the specified bucket before saving the destination configuration. ### Method POST ### Endpoint /api/destinations ### Parameters #### Request Body - **name** (string) - Required - The name of the storage destination. - **access_key** (string) - Required - The access key for S3 authentication. - **secret_key** (string) - Required - The secret key for S3 authentication. - **region** (string) - Required - The AWS region (e.g., "us-east-1"). - **endpoint** (string) - Optional - The S3 endpoint URL (e.g., "https://s3.amazonaws.com"). Defaults to AWS S3 if not provided. - **bucket_name** (string) - Required - The name of the S3 bucket. ### Request Example ```json { "name": "aws-backup-bucket", "access_key": "AKIAIOSFODNN7EXAMPLE", "secret_key": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", "region": "us-east-1", "endpoint": "https://s3.amazonaws.com", "bucket_name": "my-postgres-backups" } ``` ### Response #### Success Response (200) - **id** (string) - The unique identifier for the created storage destination. - **name** (string) - The name of the destination. - **bucket_name** (string) - The name of the S3 bucket. - **created_at** (string) - The timestamp when the destination was created. #### Response Example ```json { "id": "b2c3d4e5-f6a7-8901-2345-67890abcdef1", "name": "aws-backup-bucket", "bucket_name": "my-postgres-backups", "created_at": "2023-10-27T10:05:00Z" } ``` ``` -------------------------------- ### Schedule Backup Job - Go Source: https://context7.com/eduardolat/pgbackweb/llms.txt Configures and schedules a new backup job. This function allows setting a cron expression for the backup frequency, specifying pg_dump options, and defining retention policies. It automatically schedules the next execution based on the provided cron expression and timezone. Requires a `backups.Service` instance and `dbgen.BackupsServiceCreateBackupParams`. ```go package main import ( "context" "fmt" "github.com/eduardolat/pgbackweb/internal/database/dbgen" "github.com/eduardolat/pgbackweb/internal/service/backups" "github.com/google/uuid" ) func scheduleBackup(service *backups.Service) error { ctx := context.Background() databaseID := uuid.MustParse("550e8400-e29b-41d4-a716-446655440000") destinationID := uuid.NullUUID{ UUID: uuid.MustParse("660e8400-e29b-41d4-a716-446655440000"), Valid: true, } backup, err := service.CreateBackup(ctx, dbgen.BackupsServiceCreateBackupParams{ Name: "nightly-production-backup", DatabaseID: databaseID, DestinationID: destinationID, IsActive: true, CronExpression: "0 2 * * *", // 2 AM daily TimeZone: "America/New_York", Retention: 30, // Keep 30 days OptDataOnly: false, OptSchemaOnly: false, OptClean: true, OptIfExists: true, OptCreate: false, OptNoComments: true, }) if err != nil { return fmt.Errorf("failed to create backup: %w", err) } fmt.Printf("Backup scheduled with ID: %s, next run: %v\n", backup.ID, backup.NextExecution) return nil } ``` -------------------------------- ### Define Pagination Function Signature Source: https://github.com/eduardolat/pgbackweb/blob/main/internal/util/paginateutil/README.md Defines the expected signature for a pagination wrapper function, including request parameters, context, and the return types for pagination response and data. It assumes the use of custom request parameter structs like PaginateXYZParams. ```go type PaginateXYZParams struct { Page int Limit int ABCFilter sql.NullString } func PaginateXYZ( ctx context.Context, params PaginateXYZParams, ) ( paginateutil.PaginateResponse, []XYZ, error ) ``` -------------------------------- ### Reset PG Back Web Password via Docker Source: https://github.com/eduardolat/pgbackweb/blob/main/README.md Command to reset the PG Back Web user password. This command is executed within the running PG Back Web container. You need to replace `` with the actual name or ID of your PG Back Web container. ```bash docker exec -it sh -c change-password ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.