How to realize automatic backup and regular backup of mysql database under linux

Keywords: crontab Database shell mysqldump

1. View disk space:

Since it is a regular backup, we should choose a disk space with sufficient space to avoid the consequences of backup failure and data loss caused by insufficient space! __________
Storing on the current disk is the simplest, but it is the least recommended; servers have multiple hard disks, it is best to store backups on another hard disk; if conditions permit, choose better and safer storage media;

# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   50G   46G  1.6G  97% /
tmpfs                         1.9G   92K  1.9G   1% /dev/shm
/dev/sda1                     485M   39M  421M   9% /boot
/dev/mapper/VolGroup-lv_home  534G  3.6G  503G   1% /home

2. Create a backup directory:

Above, we use the command to see that there is plenty of space under / home, so we can consider saving backup files in / home.

cd /home
mkdir backup
cd backup

3. Create backup Shell scripts:

Note that the database Name in the following command is replaced by the actual database name;
Of course, you can also use the actual naming rules!

vi bkDatabaseName.sh

Enter/paste the following:

#!/bin/bash
mysqldump -uusername -ppassword DatabaseName > /home/backup/DatabaseName_$(date +%Y%m%d_%H%M%S).sql

Compress backups:

#!/bin/bash
mysqldump -uusername -ppassword DatabaseName | gzip > /home/backup/DatabaseName_$(date +%Y%m%d_%H%M%S).sql.gz

Be careful:
Replace username with the actual username.
Replace the password with the actual password.
Replace the database Name with the actual database name.

4. Add executable permissions:

chmod u+x bkDatabaseName.sh

After adding executable privileges, execute the script first to see if there are any errors in the script and whether it can be used properly.

./bkDatabaseName.sh

5. Adding Planned Tasks

Detection or installation of crontab

Confirm whether crontab is installed:
Executing the crontab command indicates that there is no installation if the command is not found

# crontab
-bash: crontab: command not found

If crontab is not installed at that time, it needs to be installed first. For specific steps, please refer to:
Installation of the Scheduler crontab under CentOS using the yum command
Install the scheduled task program crontab from the CentOS system disk using the rpm command

Adding Scheduled Tasks

Execute orders:

crontab -e

At this point, as with vi editor, you can edit the scheduled tasks.
Enter the following and save:

10 0 * * * /home/backup/bkDatabaseName.sh

What exactly does that mean?
It means that the shell script "/ home/backup/bkDatabaseName.sh" is executed once a day at 10:00.

6. Test whether the task is executed

Simply, let's execute the "ls" command several times to see if the file is created in a minute.

If task execution fails, the task log can be viewed by following commands:

tail -f /var/log/cron

The output is similar to the following:

Sep 30 14:01:01 bogon run-parts(/etc/cron.hourly)[2503]: starting 0anacron
Sep 30 14:01:01 bogon run-parts(/etc/cron.hourly)[2512]: finished 0anacron
Sep 30 15:01:01 bogon CROND[3092]: (root) CMD (run-parts /etc/cron.hourly)
Sep 30 15:01:01 bogon run-parts(/etc/cron.hourly)[3092]: starting 0anacron
Sep 30 15:01:02 bogon run-parts(/etc/cron.hourly)[3101]: finished 0anacron
Sep 30 15:50:44 bogon crontab[3598]: (root) BEGIN EDIT (root)
Sep 30 16:01:01 bogon CROND[3705]: (root) CMD (run-parts /etc/cron.hourly)
Sep 30 16:01:01 bogon run-parts(/etc/cron.hourly)[3705]: starting 0anacron
Sep 30 16:01:01 bogon run-parts(/etc/cron.hourly)[3714]: finished 0anacron
Sep 30 16:15:29 bogon crontab[3598]: (root) END EDIT (root)

Posted by misteraven on Thu, 18 Apr 2019 12:39:32 -0700