How do I backup and restore my DHIS2 Database instance

Please can anyone point me to a thorough documentation on how I can backup our DHIS2 database?

Which installation type did you use?

  1. Standalone.
  2. dhis2-tools-ng
  3. dhis2-server-tools - Ansible

Nevertheless, you can automate the backup or manually.

  1. Manual backup if you have access to the database server
 $ pg_dump -T analytics_* -T aggregated* -T completeness* -O -x database_name | gzip > name_backup_.sql.gz
  1. Auto backup

To back up your DHIS2 database using a cron job in PostgreSQL, you can follow these steps:

  1. Create a backup script: Start by creating a shell script that will execute the PostgreSQL pg_dump command to create the database backup. Open a text editor and create a new file, for example, backup_dhis2.sh. Enter the following code in the file:
#!/bin/bash

# Set the backup directory and filename
BACKUP_DIR="/path/to/backup/directory"
BACKUP_FILE="dhis2_backup_$(date +'%Y%m%d%H%M%S').sql"

# Run the pg_dump command to create the backup
pg_dump -U your_username -h localhost -d your_database_name > "$BACKUP_DIR/$BACKUP_FILE"

# Optional: Compress the backup file
gzip "$BACKUP_DIR/$BACKUP_FILE"

Make sure to replace your_username with your actual PostgreSQL username and your_database_name with the name of your DHIS2 database. Set the BACKUP_DIR variable to the desired backup directory path. You can also modify the BACKUP_FILE variable to customize the backup file name.

  1. Make the script executable: Set the executable permission on the backup script file by running the following command:
chmod +x backup_dhis2.sh
  1. Test the backup script: Run the script manually to verify that it creates a valid backup file in the specified directory. You can do this by executing the following command:
./backup_dhis2.sh
  1. Schedule the cron job: Open the crontab file using the command:
crontab -e

Add a new line to schedule the backup script execution. For example, to run the backup daily at 2 AM, add the following line:

0 2 * * * /path/to/backup_dhis2.sh

Save the crontab file.

  1. Monitor and validate backups: Regularly check the backup files created by the cron job to ensure they are being generated as expected. You can also periodically restore a backup file to a test environment to verify its integrity and ensure it can be successfully restored if needed.

Note: It’s essential to secure the backup files and store them in a location separate from your database server to avoid data loss due to hardware failures or other issues.

Remember to customize the backup script and cron job settings based on your specific setup. Additionally, refer to the PostgreSQL documentation for more information on the pg_dump command and additional backup options

  1. Automated Backup on Linux - PostgreSQL wiki - The one that I have been using, applies to ansible I should think. example file - dhis2-tools-ng/setup/service/dhis2-backup at master · bobjolliffe/dhis2-tools-ng · GitHub
  2. PostgreSQL: Documentation: 15: pg_dump - Further reading
  3. GitHub - citusdata/pg_cron: Run periodic jobs in PostgreSQL - Further reading

Let us know if you need any further information.
Moses

7 Likes

moses mwale, you are amazing. This is exactly what I was looking for, and in fact your link to the dhis2-server-tools - Ansible install looks to be also what I wanted but didn’t actually find in the official guides way back when I was first digging into the DHIS2 world.

Thank you again for this post. I’ll reply after seeing how all of this went.

5 Likes