misc
Backing up from a MariaDB database that is in a Docker container
Backing up MariaDB database
For this blog I am using a docker instance that is hosing a MariaDB database. I needed a way to automatically backup the database and upload it to another server.
```bash #!/bin/bash sudo docker exec [CONTAINER] /usr/bin/mysqldump -u [USERNAME] --password=[PASSWORD] [DATABASE] > blog-backup-$(date '+%Y-%m-%d').sql /usr/local/bin/gdrive upload blog-backup-$(data '+%Y-%m-%d').sql -p [FOLDERID] -c /home/pi/.gdrive ```
Do note that I would recommend using a full path for the file name. IE: /home/pi/backups/blog-backup-$(date '+%Y-%m-%d).sql. We will need to assign executable permissions on the script that we just created. This can be achieved by the following command.
chmod +x ./backupScript.sh
Next step would be adding it to Crontab to run at a schedule time. The following command will launch the crontab file.
crontab -e
Append the following line to the crontab to run the script at midnight everyday.
0 0 * * * [FULL PATH TO SCRIPT] >> /home/pi/cronlog.log 2>&1
I like to redirect the output of a script into a file and perform confirmations on that file. The >> /home/pi/cronlog.log 2>&1 will redirect the output to the file cronlog.log. If everything is okay, your database will be uploaded to Google Drive daily at midnight. Note that if your database has confidential data, I would highly recommend against using this method.