Simple script that makes a dump of a given mariadb database and cleans up old backups, intended to be used with cron jobs.
- Run
sudo crontab -e
to start editing your cron jobs - Add a new cron job for your desired database
- Native
- Template:
0 23 * * * bash <full path to mariadb-easy-dump.sh> <database name> <backups to keep> <backup directory>
- Example:
0 23 * * * bash /etc/mariadb-easy-dump/mariadb-easy-dump.sh application-a 5 /mnt/backup_drive/application_a
- Template:
- Docker
- Template:
0 23 * * * bash <full path to mariadb-easy-dump-docker.sh> <mariadb container name> <database name> <credentials file path> <backups to keep> <backup directory>
- Example:
0 23 * * * bash /etc/mariadb-easy-dump/mariadb-easy-dump-docker.sh application-a-mariadb application-a /mnt/backup_drive/credentials/application-a.cnf 5 /mnt/backup_drive/application_a
- Template:
- For more information about the parameters, see section "Script parameters".
- Native
- Save and exit
If you need to authenticate using a specific user, you can update your /etc/.my.cnf
or ~/.my.cnf
to configure those credentials.
[mysqldump]
user="mariadb_user"
password="mariadb_password"
Warning
Make sure to also secure the .my.cnf
file with appropriate permissions to prevent unauthorized users from reading this file.
chown YOUR_CRONJOB_USER .my.cnf; chmod 600 .my.cnf
Tip
For Docker you can store the credential files inside the credentials
directory of this repository, this directory is gitignored and won't conflict with future updates.
bash mariadb-easy-dump.sh <database name> <backups to keep> <backup directory> [failure hook script] [success hook script]
Parameter | Required | Type | Description |
---|---|---|---|
database name |
Yes | string |
Name of the database that we should make a backup of. |
backups to keep |
Yes | integer |
Amount of older backups you want to keep, deleted in order of oldest first. |
backup directory |
Yes | path (string) |
Path where the backup dumps should be stored at. |
failure hook script |
No | path (string) |
The path to the hook script that should be executed when the backup fails. |
success hook script |
No | path (string) |
The path to the hook script that should be executed when the backup succeeds. |
bash mariadb-easy-dump-docker.sh <mariadb container name> <database name> <credentials file path> <backups to keep> <backup directory> [failure hook script] [success hook script]
Parameter | Required | Type | Description |
---|---|---|---|
mariadb container name |
Yes | string |
Name of the MariaDB Docker container that we should make a backup of. |
database name |
Yes | string |
Name of the database that we should make a backup of. |
credentials file path |
Yes | path (string) |
The path to where the credentials file is stored. |
backups to keep |
Yes | integer |
Amount of older backups you want to keep, deleted in order of oldest first. |
backup directory |
Yes | path (string) |
Path where the backup dumps should be stored at. |
failure hook script |
No | path (string) |
The path to the hook script that should be executed when the backup fails. |
success hook script |
No | path (string) |
The path to the hook script that should be executed when the backup succeeds. |
If you want to try to make the backup immediately or it doesn't seem to work properly, you can always just run the script manually.
Just make sure to use the same user as your cron job, which will be root
in case you followed the setup instructions directly.
It could also be another user in case you ran the crontab
command with a specific user, for example crontab -e -u tom
.
This script can call another bash script in case the backup succeeds or fails, which can be used for monitoring for example.
You can configure these using the optional failure hook script
and success hook script
script parameters.
Warning
The hook script will be called with the same permissions as the mariadb-easy-dump.sh
script.
Warning
When you are using the Docker variant of the script, the script is executed on the host machine, not the container.
Tip
The database name is passed to the script, and should be accessable via $1
.
Tip
You can store hooks inside the hooks
directory of this repository, this directory is gitignored and won't conflict with future updates.