Category Archives: bash

Tips and tricks for the *nix shell bash

MySQL automated database backup

This article describes a way to setup a regular backup for your MySQL databases. This method uses the crontab to schedule backup jobs (you could also use the logrotate method).

The database backups are stored in separate files. Once database backup is completed the file is zipped and password encrypted with openssl.

First create a .my.cnf file in your home directory with the following contents:

Make sure the file is only readable by your self:

Next create a SQL user as shown below:

Create a folder in for example /var/backups/ and name it mysql

Create a script called at a suitable location, for example your $HOME/cron/scripts folder (make it NOT world readable / writeable):

Now it is time to add a cron.d file; go to /etc/cron.d and add a crontab file, e.g. backup_mysql, with the following contents:

Replace the M and H with minute and hour you want the backup to be performed.

That’s it; your mysql databases are backedup at the sepcified interval.



Generate password with bash

You can use the following shell function to generate random password.

Edit ~/.bashrc file, enter:

Save and close the file. Source ~/.bashrc again, enter:

To generate random password, enter:

To generate 8 character long random password, enter: