What is MySQL
MySQL is an open-source RDBMS (Relational Database Management System). As the name implied it uses SQL (Structured Query Language) to access and manipulate data. MySQL has been widely used to store and manage data ranging from a simple web application to an enterprise class application.
The importance of data in every application require us to regularly back up the data to prevent data loss, for example caused by hardware crashes. In this post I will show you how to back up the database manually and using a script combined with a cron job to run the process automatically.
To create a database backup in MySQL we can use the
mysqldump command. The example syntax of using this command is:
mysqldump -u username -p database_to_backup > backup_file_name.sql
If you need to restore the database you can use the following command:
mysql -u username -p database_to_restore < backup_file_name.sql
Before you can execute the command you might need to create the database if you don’t already have it.
saturn@ubuntu:~$ mysql -u root -p
CREATE DATABASE database_to_restore;
Creating Backup Script
To start let’s create MySQL user account that we are going to use to do the backup process. Login to MySQL using
mysql -u root -p command. Type and execute the following command to create
grant lock tables, select, show view on kodejava.* to 'backupuser'@'localhost' identified by 'backuppasswd'; flush privileges;
Exit from the MySQL using the
exit command and create the following backup script called
backup.sh using your favorite editor. For example, you can use
vim to create the file.
#!/bin/sh BACKUP_HOME="/home/saturn/backup" cd $BACKUP_HOME directory="$(date +%Y%m%d)" if [ ! -d "$directory" ]; then mkdir $directory fi backupdir="$BACKUP_HOME/$directory" backup="kodejava-$(date +%Y%m%d%H%M%S)" mysqldump -ubackupuser -pbackuppasswd --opt kodejava > $backupdir/$backup.sql cd $directory tar -czf $backup.tar.gz $backup.sql rm $backup.sql
To make the
backup.sh file executable you need to run the
chmod +x backup.sh command.
Creating Scheduler Using Crontab
The crontab command is used to schedule commands to be executed periodically at a predetermined time. It will run as a background process without needing user intervention. These kinds of jobs are generally referred to as cron jobs and the jobs will run as the user who creates the cron jobs.
In the example below we register a cron job to execute the script at 12:00AM every day. To edit the cron jobs type
crontab -e, this will open the crontab file.
saturn@ubuntu:~$ crontab -e no crontab for saturn - using an empty one Select an editor. To change later, run 'select-editor'. 1. /bin/ed 2. /bin/nano <---- easiest 3. /usr/bin/vim.basic 4. /usr/bin/vim.tiny Choose 1-4 :
Select an editor to edit the crontab, choose by entering the number of the editor. The easiest one is
nano but you can also use
vim if you comfortable with it.
And you will see an empty crontab file will the following commented messages:
# Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any').# # Notice that tasks will be started based on the cron's system # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command
Go to the end of the file and write the following entry to register a cron job. In the example below we register a cron job to execute the
backup.sh script at 12:00M every day.
# m h dom mon dow command 0 0 * * * /home/saturn/backup.sh
After you save the file you can use the
crontab -l command to list the registered cron job. If you want to know more about crontab you can visit crontab guru website.