Manual database backup sometimes creates more difficulty and also take lot time, So I've created a simple bash script to take backup for all of the databases in your machine

Modify Credentials

Before running your script. please modify your credentials and directory before executing the script. An example could be:

MYSQL_USER ="root"  
MYSQL_PASSWORD= "myrootpassword"

Provide execution permission

Make your script executable

chmod +x

Backup Location

The Shell script executes and stores the backup of all the databases in the directory


Take database backup automatically

If you want to backup your database in a certain interval, we can use crontab to achieve this.

To edit crontab, run:

crontab -e

Here is an example of taking the backup everyday at midnight.

00 00 * * * /root/

If you want to configure for other time or want to more more about crontab configuration :

Cleanup old files

If you want to remove old files add the below code in the end of the script. Here is an example of removing old files older than 5 days.

find "$BACKUP_DIR" -mtime +5 -type d -exec rm -rf {} \;

Feel free to download this script and enjoy!