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
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
80641a3
Add Backup script
perpetrator1 Oct 1, 2024
9003985
Add cron Job doc and fixed env names in backup script
perpetrator1 Oct 9, 2024
d1c48ec
Merge branch 'ohcnetwork:develop' into docker_backup
perpetrator1 Oct 9, 2024
f5a9a72
Fixed docs, env variables
perpetrator1 Oct 10, 2024
ab5bea8
Improve backup script
perpetrator1 Oct 11, 2024
852e415
Fix typos
perpetrator1 Oct 11, 2024
f498e30
Fix backupfile path
perpetrator1 Oct 13, 2024
ba6623f
Add bind in db volume
perpetrator1 Oct 13, 2024
f0788e8
Fix readme, Add backup doc
perpetrator1 Oct 13, 2024
76b0a0f
Update README.md
perpetrator1 Oct 13, 2024
7b7a7ee
Update README.md
perpetrator1 Oct 13, 2024
c583b5d
Update README.md
perpetrator1 Oct 13, 2024
20cf6cc
Update Backup.md
perpetrator1 Oct 13, 2024
60b8bf7
Update Backup.md
perpetrator1 Oct 13, 2024
f3c5f55
Update Backup.md
perpetrator1 Oct 13, 2024
a1d1793
Update README.md
perpetrator1 Oct 13, 2024
7249d2c
Update Backup.md
perpetrator1 Oct 13, 2024
ec39c91
Update Backup.md
perpetrator1 Oct 13, 2024
bae6d22
Update Backup.md
perpetrator1 Oct 13, 2024
4697dbe
Update README.md
perpetrator1 Oct 13, 2024
f39a8ff
Update Backup.md
perpetrator1 Oct 13, 2024
a9e871a
Merge branch 'ohcnetwork:develop' into docker_backup
perpetrator1 Oct 14, 2024
970923c
Move backup.md from root dir to docs dir
perpetrator1 Oct 14, 2024
dfa9adc
convert backup doc from markdown to rst
perpetrator1 Oct 14, 2024
e12dda3
Update backup.rst
perpetrator1 Oct 14, 2024
a57c9c2
Delete docs/databases/backup.md
perpetrator1 Oct 14, 2024
2a0aab7
Update README.md
perpetrator1 Oct 14, 2024
6010600
Update backup.rst
perpetrator1 Oct 14, 2024
a725aa1
Update backup.rst
perpetrator1 Oct 14, 2024
6e6b06f
Update docker-compose.yaml
perpetrator1 Oct 14, 2024
5fefe19
Add target dir to .env
perpetrator1 Oct 14, 2024
6baf830
Merge branch 'ohcnetwork:develop' into docker_backup
perpetrator1 Oct 15, 2024
3bebdfa
- Add compression to backup files
perpetrator1 Oct 15, 2024
a79b07d
Add file path in .env
perpetrator1 Oct 15, 2024
3da5e47
Change bind directory
perpetrator1 Oct 15, 2024
763fbbb
Update backup.rst
perpetrator1 Oct 15, 2024
89dd3ec
Fix typo
perpetrator1 Oct 15, 2024
7dddb89
Merge branch 'develop' into docker_backup
perpetrator1 Oct 15, 2024
50379d9
Merge branch 'develop' into docker_backup
perpetrator1 Oct 16, 2024
700fcd8
Update backup.rst
perpetrator1 Oct 17, 2024
9f2f363
Fix path name and env var name
perpetrator1 Oct 26, 2024
5cf8505
Edit file paths, docs
perpetrator1 Oct 26, 2024
e8fe8d7
Fix path, var name
perpetrator1 Oct 26, 2024
a99360d
Merge branch 'ohcnetwork:develop' into docker_backup
perpetrator1 Oct 26, 2024
d699368
add fallback to BACKUP_DIR in docker compose
sainak Nov 13, 2024
c472374
Merge branch 'ohcnetwork:develop' into docker_backup
perpetrator1 Nov 13, 2024
072e814
Update backup.rst
perpetrator1 Nov 14, 2024
e8c7d43
Add error handling
Nov 14, 2024
5eb2349
Update backup.rst
perpetrator1 Nov 14, 2024
31129a9
Update backup.sh
perpetrator1 Nov 14, 2024
cbcbc57
Update backup script
Nov 15, 2024
5d43dfd
Fix finding container name
Nov 15, 2024
43adec2
Fix finding containers
Nov 15, 2024
c610fa0
Update backup.rst
perpetrator1 Nov 15, 2024
5301fc8
Merge branch 'develop' into docker_backup
perpetrator1 Nov 15, 2024
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
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ POSTGRES_HOST=db
POSTGRES_DB=care
POSTGRES_PORT=5432
DATABASE_URL=postgres://postgres:postgres@localhost:5433/care
BACKUP_DIR="./care-backups"
DB_BACKUP_RETENTION_PERIOD=7
REDIS_URL=redis://localhost:6380
CELERY_BROKER_URL=redis://localhost:6380/0

Expand Down
6 changes: 4 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Any support is welcome. You could help by writing documentation, pull-requests,

### Getting Started

An issue wih the [good first](https://github.com/ohcnetwork/care/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3A%22good+first+issue%22) or [help wanted](https://github.com/ohcnetwork/care/issues?q=is%3Aissue+sort%3Aupdated-desc+label%3A%22help+wanted%22+is%3Aopen) label might be a good place to start with.
An issue with the [good first](https://github.com/ohcnetwork/care/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3A%22good+first+issue%22) or [help wanted](https://github.com/ohcnetwork/care/issues?q=is%3Aissue+sort%3Aupdated-desc+label%3A%22help+wanted%22+is%3Aopen) label might be a good place to start with.

### Setting up the development environment

Expand Down Expand Up @@ -34,11 +34,13 @@ Make sure you have docker and docker-compose installed. Then run:
make build
```



#### Using Virtualenv

Make sure you have Postgres and Redis installed on your system.

##### Setting up postgtres for the first time
##### Setting up postgres for the first time

```bash
sudo -u postgres psql
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ make load-dummy-data
Prebuilt docker images for server deployments are available
on [ghcr](https://github.com/ohcnetwork/care/pkgs/container/care)

For backup and restore use [this](/docs/databases/backup.rst) documentation.
## Contributing

We welcome contributions from everyone. Please read our [contributing guidelines](./CONTRIBUTING.md) to get started.
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
- ./docker/.prebuilt.env
volumes:
- postgres-data:/var/lib/postgresql/data
- ${BACKUP_DIR:-./care-backups}:/backups
ports:
- "5433:5432"

Expand Down
118 changes: 118 additions & 0 deletions docs/databases/backup.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
Docker database backup
======================

This page explains how to automate the backup process of a Docker database on a daily basis and restore the backup snapshot created by the `backup script <../../scripts/backup.sh>`_.

Note: This documentation assumes that you are using a Linux-based system.
-------------------------------------------------------------------------------

Here's how the script works
---------------------------

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``.

Set up a cronjob
----------------

Backup your database running on docker automatically everyday by initiating a cronjob.

Install the package
~~~~~~~~~~~~~~~~~~~

For a fedora based system:

.. code:: bash

sudo dnf install crond

For a debian based system:

.. code:: bash

sudo apt install cron

Automate the cronjob
~~~~~~~~~~~~~~~~~~~~

Open up a crontab:

.. code:: bash

crontab -e

Add the cronjob:

.. code:: bash

0 0 * * * /home/care/scripts/backup.sh
perpetrator1 marked this conversation as resolved.
Show resolved Hide resolved

List the cron jobs
~~~~~~~~~~~~~~~~~~

.. code:: bash

crontab -l

Check the status of cron
~~~~~~~~~~~~~~~~~~~~~~~~

For a fedora based os:

.. code:: bash

sudo systemctl status crond

For a debian based os:

.. code:: bash

sudo systemctl status cron

perpetrator1 marked this conversation as resolved.
Show resolved Hide resolved
Verify the cron job
~~~~~~~~~~~~~~~~~
To verify the cron job is working:

1. Check the system logs for cron activity:

.. code:: bash

sudo grep CRON /var/log/syslog
2. Monitor the backup directory for new files after the scheduled time

Restoration of the Database
===========================

We are basically deleting the container's existing database and creating a new database with the same name. Then we will use ``pg_restore`` to restore the database. Run the following commands in your terminal.

sainak marked this conversation as resolved.
Show resolved Hide resolved
Make sure you have stopped all the containers except the db before proceeding.
------------------------------------------------------------------------------

Delete the existing database:

.. code:: bash

docker exec -it $(docker ps --format '{{.Names}}' --filter name='^care-db$' --filter status=running | head -n1) psql -U postgres -c "DROP DATABASE IF EXISTS care;"

Create the new database:

.. code:: bash

docker exec -it $(docker ps --format '{{.Names}}' --filter name='^care-db$' --filter status=running | head -n1) psql -U postgres -c "CREATE DATABASE care;"

Execute and copy the name of the file you want to restore the database with:

.. code:: bash

sudo ls "${BACKUP_DIR}"

Restore the database:

Replace <file name> with your file name which looks like this ``care_backup_%Y%m%d%H%M%S.sql``

.. code:: bash

docker exec -it $(docker ps --format '{{.Names}}' --filter name='^care-db$' --filter status=running | head -n1) pg_restore -U postgres -d care /backups/<file name>.dump

------------------------------------------------------------------------------------------------------------------

There are way easier ways to do this. If anyone has any particular idea, feel free to make a PR :)
40 changes: 40 additions & 0 deletions scripts/backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash
set -ueo pipefail
# 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
fi
source "${ENV_FILE}"

# Get exact container name and verify it's running
container_name="$(docker ps --format '{{.Names}}' | grep '^care-db$')"
if [[ -z "${container_name}" ]]; then
echo "Error: PostgreSQL container 'care-db' is not running" >&2
exit 1
elif [[ $(echo "${container_name}" | wc -l) -gt 1 ]]; then
echo "Error: Multiple containers matched 'care-db'" >&2
exit 1
fi
perpetrator1 marked this conversation as resolved.
Show resolved Hide resolved

date=$(date +%Y%m%d%H%M%S)

# Validate database user
if [[ -z "${POSTGRES_USER}" ]]; then
echo "Error: POSTGRES_USER environment variable is not set" >&2
exit 1
fi

perpetrator1 marked this conversation as resolved.
Show resolved Hide resolved
# Set backup file name
backup_file="${POSTGRES_DB}_backup_${date}.dump"

perpetrator1 marked this conversation as resolved.
Show resolved Hide resolved
# 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 {} \;
perpetrator1 marked this conversation as resolved.
Show resolved Hide resolved

#backup the database
if ! docker exec -t ${container_name} pg_dump -U ${POSTGRES_USER} -Fc -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}"
perpetrator1 marked this conversation as resolved.
Show resolved Hide resolved