This information will exhibit robotically again up MySQL databases utilizing Bash script, step-by-step.
Find out how to Robotically Backup MySQL Database Utilizing Bash Script?
Create a Bash script for the backup of the MySQL database. Firstly, open the terminal, create a listing, and navigate to the listing by typing these instructions:
cd mysqlbackup/
The output shows that you’ve efficiently navigated to the listing:
Create a bash script named “backup.sh” utilizing any editor, for this put up nano editor is getting used:
The bash script will create:
Present the MySQL credentials and the title of the database that you just wish to backup:
DB_PASS=”password”
DB_NAME=”database-name”
Set the Backup listing “BACKUP_DIR” by offering the placement the place the backup file ought to save:
BACKUP_DIR=”/path/to/your/backup/listing”
Set the date format for the title of backup file:
DATE=$(date +”%Y-%m-%d_percentH-%M-%S”)
Use this mysqldump command with MySQL database credentials to create the SQL backup file:
mysqldump –user=$DB_USER –password=$DB_PASS $DB_NAME > $BACKUP_DIR/$DB_NAME-$DATE.sql
To compress the SQL backup file with the gzip instrument, use this command:
gzip $BACKUP_DIR/$DB_NAME-$DATE.sql
To save lots of the disk house, take away the previous backup information after a time interval, for this era “7” days previous backup file will probably be eliminated utilizing this command:
discover $BACKUP_DIR -type f -name “*.gz” -mtime +7 -delete
Save the file and exit the nano editor by press “CTRL + X” keys:
Change the permissions of the bash script to executable by working this command:
The error-free output means the execution of the command is profitable:
Run the script utilizing this bash command:
Sort the password of Ubuntu and use the “ls” command to confirm whether or not the backup file is created or not:
The backup file is efficiently created utilizing the bash script. Now to automate the method of backup by utilizing the “cron” job scheduler utility. To submit a brand new cron job use the “-e” possibility with crontab:
Set the time for robotically working the bash script. For this put up, “2 AM” is chosen for working the script robotically:
0 2 * * * /path/to/backup_mysql.sh
Save and exit the file:
Let’s see one other command if you wish to backup your database after each “5 Minutes” sort this and save the file:
*/5 * * * * /path/to/backup_mysql.sh
The crontab will take a while to create the job efficiently:
Examine the listing for backup information which might be robotically created after “2 AM”, utilizing the “ls” command:
The MySQL database backup information are created robotically utilizing the bash script and cron utility.
Conclusion
Create a bash script by offering MySQL credentials and the title of the database that you just wish to again up and the file title format. Use the mysqldump command for creating the backup SQL file, gzip to compress it, and run the script. Use the crontab to make the method of backup automated. This put up mentioned again up a MySQL database utilizing Bash script robotically.