This script generates backups of all databases in a PostgreSQL Docker container and saves them in a folder in the host machine.
- A script should be created to delete certain old backups from time to time so that the available space does not fill up
- Could work with more database systems than just PostgreSQL
- Clone this repository
- Copy the file
database_backup_generator.shto your host machine - Give execution permissions to the file
chmod +x database_backup_generator.sh- Run the script
./database_backup_generator.sh- You can run the script with the following parameters:
-t: Run interactive mode--container-name <container_name>: The name of the container with the PostgreSQL image--username <username>: The username of the PostgreSQL user--backup-directory <path>: The path of the folder where the backups will be saved--default-dbs: If this flag is set, the script will generate backups of the default databases (postgres, template0, template1)
Example:
./database_backup_generator.sh --container-name my_postgres_container --username my_user --backup-directory /tmp/backups --default-dbs- You can add the script to your crontab file to run it automatically.
crontab -e- For example, if you want to run the script every Sunday at 00:00, you can add the following line to your crontab file (replace the paths with your own):
0 0 * * 0 /database_backup_generator.sh >> /tmp/backups/database_backup_generator.log 2>&1(The script will generate a log file in the path /tmp/backups/database_backup_generator.log)