Automatic MariaDB Backups with Unraid
Below is a simple shell
script that I created to run daily backups of all my MariaDB databases running on my Unraid server.
#!/bin/sh
backupdb() {
echo "============================================="
echo "Backing up :: $1"
echo "============================================="
backupTargetRoot="/mnt/user/DB-Backups/nas/$1/$(date '+%Y-%m/')"
backupZipFile="/mnt/user/DB-Backups/nas/$1/$(date '+%Y-%m/%F')-$1.zip"
mysqlDumpFile="/mnt/user/DB-Backups/nas/$1/$(date '+%Y-%m/%F')-$1.sql"
echo " > creating backup root dir: ${backupTargetRoot}"
mkdir -p $backupTargetRoot
rm -f $backupZipFile
echo " > dumping DB to SQL file..."
docker exec mariadb sh -c "exec mariadb-dump $1 -u<user> -p\"<pass>\"" > $mysqlDumpFile
echo " > compressing backup file..."
zip $backupZipFile $mysqlDumpFile
echo " > cleanup and permissions..."
rm $mysqlDumpFile
chmod -R 0777 $backupTargetRoot
echo " > done"
echo ""
}
backupdb "RnGo"
This script performs the following actions:
- Dumps the contents of the DB to disk using the mariadb-dump command (via docker)
- Compresses the dumped file using
zip
- Places the file in the specified output directory
- Adjusts the files permissions (in my case to
0777
, you may want to change that) - Preforms some basic cleanup
This will place the backup files in the <output-dir>/<db-name>/yyyy-mm/
directory and name the zip file using yyyy-mm-dd-<db-name>.zip
format.
To use this script, you will need to replace the <user>
and <pass>
placeholders.
Just append additional calls to the backupdb
function to the end of the script to add additional databases.
Note: the selected DB user needs to have access to each DB you wish to back up.
Just add the script to your Unraid server using the User Scripts plugin, set a sensible schedule, and enjoy the automated backups.
