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 DB Backup Script #692

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions backup-coyote-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

# Set the current date
current_date=$(date +%m_%d_%Y)

# Set the file name pattern
file_name="coyote_db_${current_date}.sql"
Copy link
Contributor

Choose a reason for hiding this comment

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

I thin coyote_prod would be more helpful here, in case we do end up backing up other environments. Even if we don't, it's more explicit, and unambiguous is what you need while restoring a broken system.


# Docker container name
container_name="coyote_db_1"
Copy link
Contributor

Choose a reason for hiding this comment

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

Given that this is orchestrated via Docker Compose, can we execute commands via compose too, and reference service names instead of container names? I don't want to rely on container names when service names can be considered more stable.


# Directory on the host where backups will be stored
backup_dir=/root/db_backups
Copy link
Contributor

Choose a reason for hiding this comment

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

I think making this ~/db_backups would be better; as to not unnecessarily leak details about our setup, and make it over-reliant on details that can change. In the future, maybe we could set the location as an env var.


# 1. Create Postgres Dump inside the Docker Container
docker exec -it $container_name bash -c "pg_dump -U postgres -d coyote > /$file_name"

# 2. Copy the Dump from the Docker Container to the Host
# Make sure the backup directory exists
mkdir -p $backup_dir

# Copy the file from the container to the host
docker cp $container_name:/$file_name $backup_dir

# 3. Compress the dump file using `gzip`
xz $backup_dir/$file_name

# 4. Remove the dump file from the container
docker exec -it $container_name bash -c "rm /$file_name"

# 5. Remove backup files older than 30 days
find $backup_dir -type f -name "coyote_db_*.sql" -mtime +30 -exec rm {} \;