{"id":18288922,"url":"https://github.com/alilotfi23/pgautobackup","last_synced_at":"2026-04-15T18:31:47.791Z","repository":{"id":261132713,"uuid":"883368398","full_name":"alilotfi23/PgAutoBackup","owner":"alilotfi23","description":"shell script code to backup pg database","archived":false,"fork":false,"pushed_at":"2024-11-07T08:57:49.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-09T07:15:46.526Z","etag":null,"topics":["bash-script","cronjob","linux","postgresql","shell-script"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alilotfi23.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-04T20:59:27.000Z","updated_at":"2025-03-17T09:35:06.000Z","dependencies_parsed_at":"2024-12-22T22:41:07.398Z","dependency_job_id":"74bd44da-2f5c-440d-9d55-d59bc963ecec","html_url":"https://github.com/alilotfi23/PgAutoBackup","commit_stats":{"total_commits":5,"total_committers":1,"mean_commits":5.0,"dds":0.0,"last_synced_commit":"8ddbe1809c09ed9bb8e0396c8f271cfd3bdbdcca"},"previous_names":["alilotfi23/pgautobackup"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alilotfi23%2FPgAutoBackup","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alilotfi23%2FPgAutoBackup/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alilotfi23%2FPgAutoBackup/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alilotfi23%2FPgAutoBackup/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alilotfi23","download_url":"https://codeload.github.com/alilotfi23/PgAutoBackup/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247994130,"owners_count":21030049,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash-script","cronjob","linux","postgresql","shell-script"],"created_at":"2024-11-05T14:03:43.810Z","updated_at":"2026-04-15T18:31:47.666Z","avatar_url":"https://github.com/alilotfi23.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PostgreSQL Backup Script\n\nThis script automates daily and weekly backups of a PostgreSQL database, with options for logging, directory management, and error handling. It is intended to be run on a Linux server with PostgreSQL installed and accessible.\n\n## Features\n\n- **Daily Backups**: Stores a database backup every day in a designated \"daily\" directory.\n- **Weekly Backups**: Stores a weekly backup (on Sundays) in a separate \"weekly\" directory.\n- **Automatic Directory Management**: Creates necessary backup directories if they do not exist.\n- **Error Handling**: Utilizes `set -e` to halt execution on any command failure.\n- **Logging**: Logs backup success and failure events for easy tracking.\n\n## Requirements\n\n- **PostgreSQL Client**: Ensure `pg_dump` is installed and available on the system running the script.\n- **Permissions**: The script requires read and write access to the specified backup directory.\n- **Cron Setup (optional)**: To automate daily execution, set up a cron job.\n\n## Configuration\n\nEdit the following variables at the beginning of the script to match your PostgreSQL setup:\n\n- `DB_NAME`: The name of the database to back up.\n- `DB_USER`: The PostgreSQL user with access to the database.\n- `DB_HOST`: The host where the database is running (use `localhost` if it's on the same server).\n- `BACKUP_DIR`: The main directory where backups will be stored.\n\n## Usage\n\n1. **Clone the Repository** or create the script directly on your server.\n\n2. **Edit Configuration**: Modify `DB_NAME`, `DB_USER`, `DB_HOST`, and `BACKUP_DIR` as needed.\n\n3. **Run the Script Manually** (for testing):\n   ```bash\n   ./backup_script.sh\n   ```\n\n4. **Automate with Cron (Optional)**:\n   To run this script daily, set up a cron job:\n   ```bash\n   crontab -e\n   ```\n   Add the following line to run the backup at midnight every day:\n   ```bash\n   0 0 * * * /path/to/backup_script.sh\n   ```\n\n## Example Script\n\n```bash\n#!/bin/bash\nset -e\n\n# Configuration\nDB_NAME=\"postgres\"\nDB_USER=\"postgres\"\nDB_HOST=\"localhost\"\nBACKUP_DIR=\"/home/user/backup\"\nDAILY_BACKUP_DIR=\"$BACKUP_DIR/daily\"\nWEEKLY_BACKUP_DIR=\"$BACKUP_DIR/weekly\"\nDATE=$(date +\"%Y%m%d_%H%M%S\")\nLOG_FILE=\"$BACKUP_DIR/backup_log.txt\"\n\n# Create directories if they don't exist\nmkdir -p \"$DAILY_BACKUP_DIR\"\nmkdir -p \"$WEEKLY_BACKUP_DIR\"\n\n# Function to perform a backup with logging\nperform_backup() {\n    local backup_file=\"$1\"\n    if pg_dump -U \"$DB_USER\" -h \"$DB_HOST\" \"$DB_NAME\" \u003e \"$backup_file\"; then\n        echo \"$DATE: Backup successful: $backup_file\" \u003e\u003e \"$LOG_FILE\"\n    else\n        echo \"$DATE: Backup failed: $backup_file\" \u003e\u003e \"$LOG_FILE\"\n    fi\n}\n\n# Daily backup\nDAILY_BACKUP_FILE=\"$DAILY_BACKUP_DIR/${DB_NAME}_$DATE.sql\"\nperform_backup \"$DAILY_BACKUP_FILE\"\n\n# Weekly backup (only on Sundays)\nif [ \"$(date +%u)\" -eq 7 ]; then\n    WEEKLY_BACKUP_FILE=\"$WEEKLY_BACKUP_DIR/${DB_NAME}_$DATE.sql\"\n    perform_backup \"$WEEKLY_BACKUP_FILE\"\nfi\n\necho \"Backup completed at $DATE\"\n```\n\n## Logging\n\n- The script logs all backup events in `backup_log.txt` in the `BACKUP_DIR`.\n- Each entry includes a timestamp and specifies whether the backup was successful or failed.\n\n## Error Handling\n\n- The `set -e` command stops the script from any error, preventing partial backups.\n- Logging provides detailed error information in case of failure.\n\n## License\n\nThis project is licensed under the MIT License.\n\n---\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falilotfi23%2Fpgautobackup","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falilotfi23%2Fpgautobackup","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falilotfi23%2Fpgautobackup/lists"}