Techie May 2022
Introduction
Data backup is the practice of copying data from a primary to a secondary location that you use for recovery in case your original data is lost or corrupted.
This document will describe how to set up an automated mysql database backup system that not only stores the backup file on the local machine, but also sends the zipped file to an email address at a given time on a daily basis.
You will need a gmail account that’ll be used to automatically send backup files to recipients.
This will work on linux distros, both server and desktop. The steps on here were tested on an ubuntu 18.04 server and desktop.
a) Install mailutils
1 . Install mailutils from apt:
sudo apt install mailutils
2 . In the “General type of mail configuration:” prompt, choose “Internet Site” and press enter.
3 . A postfix configuration prompt will open. On the “System mail name:” prompt, enter gmail.com
b) Setup Gmail App Passwords
ssmtp will use your gmail account to send mail, but you can not use your normal gmail password with external applications. Gmail has a nifty feature called App passwords that lets you sign in to your Google Account from apps on devices that don’t support 2-Step Verification. Click here to set up App passwords. On the page, select ‘Mail’ under the ‘select app’ tab, then enter a custom name under the ‘select device’ tab.
Click on the ‘Generate’ button to generate the password. Just like your normal password, this app password grants complete access to your Google Account. Copy 16-character password shown, you will need it for the next step.
c) Install ssmtp
1 . Install ssmtp:
sudo apt-get install ssmtp
2 . Edit ssmtp.conf file:
sudo nano /etc/ssmtp/ssmtp.conf
Replace the placeholders (google_apps_password_here and your_account) with your details. Your file should resemble this (your hostname will be different):
3 . Edit revaliases file:
sudo nano /etc/ssmtp/revaliases
Add this (remember to replace the place holder):
d) Create the bash script responsible for creating mysql dumps
Typically, you’d put it in:
/usr/local/sbin/
1 . cd into /usr/local/sbin/ and create a bash file. Name it backup_mysql.sh
sudo nano backup_mysql.sh
2 . Add this backup script to backup_mysql.sh then save and close it:
e) Create the cron job
1 . Open crontab as root
sudo crontab -e
2 . Add this at the end of the file:
This cron job will run the backup script created in the previous step, everyday at 4:30 PM.
With this, the zipped backup file will be sent to the recipient email you set in the bash file, every day at 4:30 PM. Feel free to change cron job time to your current time to test it.
Thanks for reading, see you in the next one!