A simple bash scripting to backup All MySQL Database
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" BACKUP_DIR="/backup/database/$TIMESTAMP"
Provide execution permission
Make your script executable
chmod +x backup_database.sh
Backup Location
The Shell script executes and stores the backup of all the databases in the directory
dir:/backup/databases/the_given_date
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/backup_database.sh
If you want to configure for other time or want to more more about crontab configuration : https://crontab.guru/examples.html
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!
Comments
Leave a comment
You are not LoggedIn but you can comment as an anonymous user which requires manual approval. For better experience please Login.