1. Check the disk space:

To avoid backup failures and data loss caused by insufficient disk space, you need to select sufficient disk space for scheduled backup.

Save to current disk this is easiest, but least recommended; The server has multiple hard drives, so it is best to store backups on another hard drive. Choose a better and safer storage medium when conditions permit;

# df -hFilesystem
Copy the code

Create a backup directory.

/home = /home; /home = /home;

cd /home
mkdir backupcd backup
Copy the code

Create backup Shell script

Notice that the DatabaseName in the following command is changed to the actual DatabaseName; Of course, you can also use the actual naming rules!

vi bkDatabaseName.sh
Copy the code

Type/paste the following:

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

Compress the backup:

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

Note: replace username with the actual username; Replace password with the actual password; Replace DatabaseName with the actual DatabaseName;

4, add executable permission:

chmod u+x bkDatabaseName.sh
Copy the code

Add executable permission after the first execution, see if the script has errors, can be used normally;

./bkDatabaseName.sh
Copy the code

Adding a Scheduled Task

Detect or install crontab

Check whether the crontab is installed: Run the crontab command. If command not found is displayed, the crontab is not installed

If crontab is not installed, install it first. For details, see: Using yum in CentOS to Install the crontab program run the RPM command to install the crontab program from the CentOS system disk

Adding a Scheduled Task

Execute command:

crontab -e
Copy the code

Just as with the VI editor, you can edit the scheduled tasks. Enter the following and save:

*/1 * * * * /home/backup/bkDatabaseName.sh
Copy the code

What exactly does that mean? Mean every minute to perform a shell script “/ home/backup/bkDatabaseName. Sh”.

6. Test whether the task is executed

Simply execute the “ls” command a few times and see if the file has been created after a minute!

If the task fails to be executed, run the following command to view the task log:

# tail -f /var/log/cron
Copy the code

The output looks like this:

Sep 30 14:01:01 bogon run-parts(/etc/cron.hourly)[2503]: starting 0anacronSep 30 14:01:01 bogon run-parts(/etc/cron.hourly)[2512]: finished 0anacronSep 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 0anacronSep 30 15:01:02 bogon run-parts(/etc/cron.hourly)[3101]: finished 0anacronSep 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 0anacronSep 30 16:01:01 bogon run-parts(/etc/cron.hourly)[3714]: finished 0anacronSep 30 16:15:29 bogon crontab[3598]: (root) END EDIT (root)
Copy the code