Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scripts and docs to configure docker backups #2534

Merged
merged 55 commits into from
Nov 25, 2024

Conversation

perpetrator1
Copy link
Contributor

@perpetrator1 perpetrator1 commented Oct 13, 2024

Proposed Changes

  • Added a script for backing up the PSQL database in the docker container
  • Added docs to set up the cronjob in README.md
  • Edited the compose file to mount a local directory to new directory inside the container
  • Added docs to restore the container's database
  • Fixed some typos

Associated Issue

  • Create db backup scripts for instances running on pure docker #2301
  • The backup script works perfectly
  • Mounted a local directory to a new directory(backups) inside the container, so that the user can move the backed up sql file inside this local directory and restore the container's database
  • The backup.md doc will help the user to restore the container's database

Architecture changes

  • Added a new doc
  • Added a new script in /care/scripts
    Only PR's with test cases included and passing lint and test pipelines will be reviewed

@ohcnetwork/care-backend-maintainers @ohcnetwork/care-backend-admins

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced automated backup and restore procedures for PostgreSQL databases.
    • Added environment variables for backup directory and retention period.
    • New script for managing database backups.
  • Documentation

    • Updated README.md to include backup and restore instructions.
    • Added a comprehensive guide for database backups in docs/databases/backup.rst.
    • Improved clarity and corrected typographical errors in CONTRIBUTING.md.
  • Chores

    • Enhanced docker-compose.yaml with new volume mapping for backups.

@perpetrator1 perpetrator1 requested a review from a team as a code owner October 13, 2024 17:52
Backup.md Outdated Show resolved Hide resolved
Backup.md Outdated Show resolved Hide resolved
Backup.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
docker-compose.yaml Outdated Show resolved Hide resolved
scripts/backup.sh Outdated Show resolved Hide resolved
scripts/backup.sh Outdated Show resolved Hide resolved
scripts/backup.sh Outdated Show resolved Hide resolved
@sainak sainak changed the title Docker backup Add scripts and docs to configure docker backups Oct 14, 2024
@perpetrator1
Copy link
Contributor Author

I've changed the code and the docs as requested, are there more changes?

docker-compose.yaml Outdated Show resolved Hide resolved
Copy link

coderabbitai bot commented Nov 13, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes in this pull request introduce new environment variables for backup configuration in the .env.example file, enhance the CONTRIBUTING.md for clarity, and add a section in the README.md for backup and restore procedures. A new volume mapping for backups is included in docker-compose.yaml, while a new documentation file backup.rst details the automated backup process for a PostgreSQL database. Lastly, a script backup.sh is created to facilitate this backup process, ensuring old backups are managed according to the specified retention period.

Changes

File Change Summary
.env.example Added BACKUP_DIR="./care-backups" and DB_BACKUP_RETENTION_PERIOD=7.
CONTRIBUTING.md Corrected typographical errors and improved clarity; no functional changes.
README.md Added a line in the "Docker" subsection directing users to backup and restore documentation.
docker-compose.yaml Added volume mapping - ${BACKUP_DIR:-./care-backups}:/backups for the db service.
docs/databases/backup.rst New documentation detailing PostgreSQL backup automation, including script usage and cron job setup.
scripts/backup.sh New script created to automate PostgreSQL database backups, managing old backups according to retention.

Poem

In the land of code where backups reign,
New scripts and docs, oh what a gain!
With variables set, and clarity bright,
Our data's safe, tucked in tight.
So here's to the changes, both big and small,
For keeping our databases standing tall! 🎉

Warning

Rate limit exceeded

@dumbstertruck3 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 13 minutes and 13 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 5d43dfd and 5301fc8.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5d43dfd and 5301fc8.

📒 Files selected for processing (1)
  • docs/databases/backup.rst (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • docs/databases/backup.rst

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 8

🧹 Outside diff range and nitpick comments (7)
scripts/backup.sh (2)

7-8: That comment on line 7 seems a bit... redundant, doesn't it?

Also, we should validate the database name:

 date=$(date +%Y%m%d%H%M%S)
-#name the file
+# Validate database name
+if [[ -z "${POSTGRES_DB}" ]]; then
+    echo "Error: POSTGRES_DB environment variable is not set" >&2
+    exit 1
+fi
+
 backup_file="${POSTGRES_DB}_backup_${date}.dump"

10-11: The cleanup could be a bit more informative.

Let's add validation and logging:

+# Validate retention period
+if [[ -z "${DB_BACKUP_RETENTION_PERIOD}" ]] || ! [[ "${DB_BACKUP_RETENTION_PERIOD}" =~ ^[0-9]+$ ]]; then
+    echo "Error: DB_BACKUP_RETENTION_PERIOD must be a positive integer" >&2
+    exit 1
+fi
+
 # Remove old backup/backups
-docker exec -t ${container_name} find "/backups" -name "${POSTGRES_DB}_backup_*.dump" -type f -mtime +${DB_BACKUP_RETENTION_PERIOD} -exec rm {} \;
+docker exec -t ${container_name} find "/backups" -name "${POSTGRES_DB}_backup_*.dump" -type f -mtime +${DB_BACKUP_RETENTION_PERIOD} -print -exec rm {} \; | \
+    sed 's/^/Removing old backup: /'
docker-compose.yaml (1)

14-14: Consider documenting backup directory permissions.

While the volume mount is fine, it might be nice if we documented the recommended permissions for ${BACKUP_DIR:-./care-backups} to prevent unauthorized access to potentially sensitive backup data.

Consider adding a note in the documentation about setting appropriate permissions:

+ # Ensure backup directory has restricted permissions
+ mkdir -p ./care-backups
+ chmod 700 ./care-backups
README.md (1)

75-75: A comma would make this sentence flow better, wouldn't it?

The sentence would be more polished with a comma after "restore":

-For backup and restore use [this](/docs/databases/backup.rst) documentation.
+For backup and restore, use [this](/docs/databases/backup.rst) documentation.
🧰 Tools
🪛 LanguageTool

[uncategorized] ~75-~75: Possible missing comma found.
Context: ...re/pkgs/container/care) For backup and restore use this ...

(AI_HYDRA_LEO_MISSING_COMMA)

docs/databases/backup.rst (3)

4-4: Consider using a more robust path reference

The relative path ../../scripts/backup.sh might break if the documentation structure changes. Perhaps we could use a more... resilient approach?

Consider using a repository-relative path or documenting the expected directory structure.


12-12: The explanation paragraph could use some structure

This rather lengthy paragraph could benefit from some formatting improvements. I mean, if you want users to actually read it...

Consider breaking it into bullet points:

-The script automates the process of creating PostgreSQL database backups from a Docker container. It generates a backup file(``.dump``) using the pg_dump utility in PostgreSQL and stores these files in the path configured in  ``$BACKUP_DIR`` environment variable which is binded to ``/backups`` in the docker container. Backup files older than ``$DB_BACKUP_RETENTION_PERIOD`` days are deleted when the script is executed by default it is set to 7 days. The backup file is saved with the name ``care_backup_%Y%m%d%H%M%S.sql``.
+The script automates PostgreSQL database backups in Docker:
+
+* Generates backup files using ``pg_dump`` utility
+* Stores files in ``$BACKUP_DIR`` (mounted as ``/backups`` in container)
+* Automatically removes backups older than ``$DB_BACKUP_RETENTION_PERIOD`` days (default: 7)
+* Names backups using format: ``care_backup_%Y%m%d%H%M%S.sql``

107-107: Consider a more professional closing note

While the friendly tone is appreciated, perhaps we could phrase this more professionally?

-  There are way easier ways to do this. If anyone has any particular idea, feel free to make a PR :)
+  Note: Contributions to improve this process are welcome. Please submit a pull request with any enhancements.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 69c2b83 and d699368.

📒 Files selected for processing (6)
  • .env.example (1 hunks)
  • CONTRIBUTING.md (2 hunks)
  • README.md (1 hunks)
  • docker-compose.yaml (1 hunks)
  • docs/databases/backup.rst (1 hunks)
  • scripts/backup.sh (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • .env.example
  • CONTRIBUTING.md
🧰 Additional context used
🪛 LanguageTool
README.md

[uncategorized] ~75-~75: Possible missing comma found.
Context: ...re/pkgs/container/care) For backup and restore use this ...

(AI_HYDRA_LEO_MISSING_COMMA)

🔇 Additional comments (3)
scripts/backup.sh (1)

1-15: One tiny detail we might want to check...

We should verify the backup directory exists in the container before proceeding:

docker-compose.yaml (1)

14-14: Verify backup script compatibility with the mount point.

The volume mount looks mostly correct, but let's make absolutely sure the backup script is looking for /backups and not /backup.

✅ Verification successful

The mount point is perfectly aligned with the backup script. How... convenient.

The backup script at scripts/backup.sh explicitly uses /backups (with an 's') for all its operations:

  • Finding and removing old backups: /backups
  • Writing new backup files: /backups/${backup_file}

So the volume mount path in docker-compose.yaml is exactly right. Not that we had any doubts.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if backup script uses the correct mount point

# Search for backup path references
rg -i '/backup' --type sh

Length of output: 361

README.md (1)

75-75: Verify the backup documentation link

Let's ensure the documentation link is valid and accessible.

✅ Verification successful

The documentation link appears to be perfectly valid

The backup documentation not only exists but also contains relevant content about database backup and restore procedures. I suppose we can all appreciate when documentation links actually work as intended.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if the backup documentation file exists
if [ -f "docs/databases/backup.rst" ]; then
    echo "✓ Documentation file exists"
    # Check if the file has content
    if [ -s "docs/databases/backup.rst" ]; then
        echo "✓ Documentation file has content"
    else
        echo "✗ Documentation file is empty"
    fi
else
    echo "✗ Documentation file not found at expected path"
fi

Length of output: 217


Script:

#!/bin/bash
# Let's check the content of the backup documentation to ensure it's actually useful
cat docs/databases/backup.rst | head -n 5

Length of output: 274

🧰 Tools
🪛 LanguageTool

[uncategorized] ~75-~75: Possible missing comma found.
Context: ...re/pkgs/container/care) For backup and restore use this ...

(AI_HYDRA_LEO_MISSING_COMMA)

scripts/backup.sh Show resolved Hide resolved
scripts/backup.sh Outdated Show resolved Hide resolved
scripts/backup.sh Outdated Show resolved Hide resolved
docs/databases/backup.rst Outdated Show resolved Hide resolved
docs/databases/backup.rst Show resolved Hide resolved
docs/databases/backup.rst Show resolved Hide resolved
docs/databases/backup.rst Show resolved Hide resolved
docs/databases/backup.rst Outdated Show resolved Hide resolved
Copy link

codecov bot commented Nov 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 69.20%. Comparing base (69c2b83) to head (d699368).
Report is 11 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #2534      +/-   ##
===========================================
- Coverage    70.03%   69.20%   -0.83%     
===========================================
  Files          198      211      +13     
  Lines        11674    11944     +270     
  Branches      1607     1208     -399     
===========================================
+ Hits          8176     8266      +90     
- Misses        3107     3302     +195     
+ Partials       391      376      -15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (2)
scripts/backup.sh (2)

1-9: Perhaps we could validate all required environment variables upfront?

While the script does validate the .env file existence, it might be slightly more elegant to validate all required environment variables at the start:

 source "${ENV_FILE}"
+
+# Validate required environment variables
+required_vars=("POSTGRES_USER" "POSTGRES_DB" "DB_BACKUP_RETENTION_PERIOD")
+for var in "${required_vars[@]}"; do
+    if [[ -z "${!var:-}" ]]; then
+        echo "Error: $var environment variable is not set" >&2
+        exit 1
+    fi
+done
🧰 Tools
🪛 Shellcheck

[warning] 9-9: ShellCheck can't follow non-constant source. Use a directive to specify location.

(SC1090)


11-19: The container validation could use a tiny bit more robustness

While the container name validation is quite good, it might be worth adding timeout handling and docker daemon checks:

+# Check if docker daemon is running
+if ! docker info >/dev/null 2>&1; then
+    echo "Error: Docker daemon is not running" >&2
+    exit 1
+fi
+
 # Get exact container name and verify it's running
-container_name="$(docker ps --format '{{.Names}}' | grep '^care-db$')"
+container_name="$(timeout 10s docker ps --format '{{.Names}}' | grep '^care-db$')"
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between d699368 and e8c7d43.

📒 Files selected for processing (2)
  • docs/databases/backup.rst (1 hunks)
  • scripts/backup.sh (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • docs/databases/backup.rst
🧰 Additional context used
🪛 Shellcheck
scripts/backup.sh

[warning] 9-9: ShellCheck can't follow non-constant source. Use a directive to specify location.

(SC1090)

🔇 Additional comments (1)
scripts/backup.sh (1)

1-1: Verify script permissions

Let's ensure the script has proper execute permissions.

Fix cron job path and add verification
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Outside diff range and nitpick comments (1)
scripts/backup.sh (1)

1-9: I see we're living dangerously without error traps...

While the initialization is decent, we might want to add some safety nets:

 #!/bin/bash
 set -ueo pipefail
+
+# Cleanup function
+cleanup() {
+    # Add cleanup tasks here
+    exit "${1:-0}"
+}
+
+# Error handler
+error() {
+    echo "Error: ${1:-'Unknown error'}" >&2
+    cleanup 1
+}
+
+# Set up trap
+trap 'error "Script interrupted"' INT TERM
+
 # Ensure we can find the .env file
-ENV_FILE="$(dirname "$(readlink -f "$0")")/../.env"
+ENV_FILE="$(cd "$(dirname "$0")/.." && pwd)/.env"
🧰 Tools
🪛 Shellcheck

[warning] 9-9: ShellCheck can't follow non-constant source. Use a directive to specify location.

(SC1090)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5eb2349 and 31129a9.

📒 Files selected for processing (1)
  • scripts/backup.sh (1 hunks)
🧰 Additional context used
🪛 Shellcheck
scripts/backup.sh

[warning] 9-9: ShellCheck can't follow non-constant source. Use a directive to specify location.

(SC1090)

scripts/backup.sh Outdated Show resolved Hide resolved
scripts/backup.sh Outdated Show resolved Hide resolved
scripts/backup.sh Outdated Show resolved Hide resolved
Copy link
Member

@sainak sainak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just fix this issue, ignore the coderabbit's suggestions

scripts/backup.sh Outdated Show resolved Hide resolved
@perpetrator1
Copy link
Contributor Author

Just fix this issue, ignore the coderabbit's suggestions

done

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (2)
scripts/backup.sh (2)

1-9: I see we're trying to be careful with error handling... almost.

While the setup is generally good, we could make it even better with a few tiny improvements:

 #!/bin/bash
 set -ueo pipefail
+
+# Helper function for consistent error messages
+error() {
+    echo "Error: $1" >&2
+    exit 1
+}
+
 # Ensure we can find the .env file
 ENV_FILE="$(dirname "$(readlink -f "$0")")/../.env"
 if [[ ! -f "${ENV_FILE}" ]]; then
-    echo "Error: .env file not found at ${ENV_FILE}" >&2
-    exit 1
+    error ".env file not found at ${ENV_FILE}"
 fi
-source "${ENV_FILE}"
+source "${ENV_FILE}" || error "Failed to source ${ENV_FILE}"
🧰 Tools
🪛 Shellcheck

[warning] 9-9: ShellCheck can't follow non-constant source. Use a directive to specify location.

(SC1090)


1-42: Some architectural thoughts, if you're interested...

A few suggestions to make this script more production-ready:

  1. The container name 'care-db' is hardcoded. Perhaps we could make it configurable?
  2. Consider adding proper logging instead of just echo statements
  3. Implement trap handlers for cleanup on script failure
  4. Add a dry-run mode for testing

Would you like me to provide implementation details for any of these suggestions?

🧰 Tools
🪛 Shellcheck

[warning] 9-9: ShellCheck can't follow non-constant source. Use a directive to specify location.

(SC1090)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 31129a9 and cbcbc57.

📒 Files selected for processing (1)
  • scripts/backup.sh (1 hunks)
🧰 Additional context used
🪛 Shellcheck
scripts/backup.sh

[warning] 9-9: ShellCheck can't follow non-constant source. Use a directive to specify location.

(SC1090)

🔇 Additional comments (2)
scripts/backup.sh (2)

10-17: 🛠️ Refactor suggestion

Oh, validation... how quaint.

While checking for empty variables is a start, perhaps we could be a tad more thorough:

 # Validate required environment variables
 required_vars=("POSTGRES_USER" "POSTGRES_DB" "DB_BACKUP_RETENTION_PERIOD")
 for var in "${required_vars[@]}"; do
     if [[ -z "${!var:-}" ]]; then
-        echo "Error: $var environment variable is not set" >&2
-        exit 1
+        error "$var environment variable is not set"
     fi
 done
+
+# Validate retention period
+if ! [[ "${DB_BACKUP_RETENTION_PERIOD}" =~ ^[0-9]+$ ]]; then
+    error "DB_BACKUP_RETENTION_PERIOD must be a positive integer"
+fi
+
+if [[ "${DB_BACKUP_RETENTION_PERIOD}" -lt 1 ]]; then
+    error "DB_BACKUP_RETENTION_PERIOD must be at least 1"
+fi

Likely invalid or redundant comment.


34-42: ⚠️ Potential issue

A backup without verification is just wishful thinking, isn't it?

Let's make this backup process a bit more... reliable:

 # Remove old backup/backups
-docker exec -t ${container_name} find "/backups" -name "${POSTGRES_DB}_backup_*.dump" -type f -mtime +${DB_BACKUP_RETENTION_PERIOD} -exec rm {} \;
+if ! docker exec -t "${container_name}" find "/backups" -name "${POSTGRES_DB}_backup_*.dump" -type f -mtime +"${DB_BACKUP_RETENTION_PERIOD}" -exec rm {} \;; then
+    error "Failed to clean up old backups"
+fi

 #backup the database
-if ! docker exec -t ${container_name} pg_dump -U ${POSTGRES_USER} -Fc -f /backups/${backup_file} ${POSTGRES_DB}; then
+if ! docker exec -t "${container_name}" pg_dump -U "${POSTGRES_USER}" -Fc -Z 9 -f "/backups/${backup_file}" "${POSTGRES_DB}"; then
     echo "Error: Database backup failed" >&2
     exit 1
 fi
-echo "Backup of database '${POSTGRES_DB}' completed and saved as /backups/${backup_file}"
+
+# Verify backup size
+backup_size=$(docker exec -t "${container_name}" stat -f %z "/backups/${backup_file}")
+if [[ ${backup_size} -lt 1024 ]]; then
+    error "Backup file is suspiciously small (${backup_size} bytes)"
+fi
+
+# Verify backup integrity
+if ! docker exec -t "${container_name}" pg_restore -l "/backups/${backup_file}" >/dev/null 2>&1; then
+    error "Backup file appears to be corrupted"
+fi
+
+echo "Backup of database '${POSTGRES_DB}' completed and verified: /backups/${backup_file} (${backup_size} bytes)"

Likely invalid or redundant comment.

scripts/backup.sh Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (2)
scripts/backup.sh (2)

1-9: Perhaps we could make the .env sourcing a bit more... robust?

While the current implementation is decent, we could silence ShellCheck and add some validation:

 #!/bin/bash
 set -ueo pipefail
+
+# shellcheck source=/dev/null
 source "${ENV_FILE}"
+
+# Validate required environment variables
+required_vars=("POSTGRES_USER" "POSTGRES_DB" "DB_BACKUP_RETENTION_PERIOD")
+for var in "${required_vars[@]}"; do
+    if [[ -z "${!var:-}" ]]; then
+        echo "Error: $var environment variable is not set" >&2
+        exit 1
+    fi
+done
🧰 Tools
🪛 Shellcheck

[warning] 9-9: ShellCheck can't follow non-constant source. Use a directive to specify location.

(SC1090)


21-22: That comment is stating the... obvious.

The comment #name the file doesn't add any value since the code is self-documenting. We could remove it or make it more meaningful.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between cbcbc57 and 5d43dfd.

📒 Files selected for processing (1)
  • scripts/backup.sh (1 hunks)
🧰 Additional context used
🪛 Shellcheck
scripts/backup.sh

[warning] 9-9: ShellCheck can't follow non-constant source. Use a directive to specify location.

(SC1090)

scripts/backup.sh Show resolved Hide resolved
scripts/backup.sh Show resolved Hide resolved
scripts/backup.sh Show resolved Hide resolved
@vigneshhari vigneshhari merged commit f0f0c8b into ohcnetwork:develop Nov 25, 2024
1 check passed
@coderabbitai coderabbitai bot mentioned this pull request Dec 2, 2024
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants