Introduction
Backing up your data regularly is essential, especially for critical applications like Home Assistant running MariaDB in a Docker container. This article will guide you through creating scripts that automate the backup and restoration processes, designed to be flexible by accepting arguments for the password and paths.
Prerequisites
- Docker installed and running.
- MariaDB container running with your data.
Backup Script with Arguments
To make our backup process versatile and reusable, we’ll create a shell script that accepts two arguments:
- MariaDB password
- Backup folder path
Script Creation
- Create and open a new script:
nano /path_to_scripts_folder/backup_mariadb.sh
- Copy and paste the following content:
#!/bin/bash
# Check if the right number of arguments are provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <password> <backup_folder_path>"
exit 1
fi
PASSWORD=$1
BACKUP_PATH=$2
docker exec mariadb /usr/bin/mysqldump -u homeassistant --password=$PASSWORD --all-databases | gzip > "$BACKUP_PATH/database_backup_$(date +\%F).sql.gz"
Save and exit the editor.
Make the script executable:
chmod +x /path_to_scripts_folder/backup_mariadb.sh
Now, run the script, passing the password and backup folder path as arguments:
/path_to_scripts_folder/backup_mariadb.sh your_password /path_to_backup_folder/
Scheduling Backups with Crontab
To automate the backup process daily:
- Open the crontab:
crontab -e
- Add the following line to run the script every day at 3 am:
0 3 * * * /path_to_scripts_folder/backup_mariadb.sh your_password /path_to_backup_folder/
Restoring the Database from Backup
Restoration Script with Arguments
To simplify the restoration process, we’ll create a separate shell script that accepts two arguments:
- MariaDB password
- Path to the backup file you want to restore
Script Creation
- Create and open a new script:
nano /path_to_scripts_folder/restore_mariadb.sh
- Copy and paste the following content:
#!/bin/bash
# Check if the right number of arguments are provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <password> <backup_file_path>"
exit 1
fi
PASSWORD=$1
BACKUP_FILE=$2
gunzip < "$BACKUP_FILE" | docker exec -i mariadb /usr/bin/mysql -u homeassistant --password=$PASSWORD
Save and exit the editor.
Make the script executable:
chmod +x /path_to_scripts_folder/restore_mariadb.sh
Now, run the script, passing the password and the path to the backup file as arguments:
/path_to_scripts_folder/restore_mariadb.sh your_password /path_to_backup_folder/database_backup_YOUR_DATE.sql.gz
Notes on Restoration
- Backup before restore: Always take a fresh backup before starting the restoration process. This ensures you have a fallback if the restore doesn’t go as planned.
- Check Compatibility: Ensure the MariaDB version you are restoring to is compatible with the version from which you took the backup.
- Downtime Considerations: Depending on the size of your database and the restoration environment’s performance, the restoration process might take some time. Plan accordingly.