linux implements a simple method for regularly backing up mysql databases

  • 2020-11-26 19:04:31
  • OfStack

Here are the detailed steps:

1. Check the disk space:


[root@localhost backup]# df -h
 The file system           capacity   Has been used   available   Has been used %  The mount point 
/dev/mapper/centos-root  17G 2.7G  15G  16% /
devtmpfs         476M   0 476M  0% /dev
tmpfs          488M   0 488M  0% /dev/shm
tmpfs          488M 7.7M 480M  2% /run
tmpfs          488M   0 488M  0% /sys/fs/cgroup
/dev/sda1        1014M 130M 885M  13% /boot
tmpfs           98M   0  98M  0% /run/user/0
[root@localhost backup]#

Select the appropriate disk to store the backup files on

2. Create the backup directory:


cd /home
mkdir backup
cd backup

3. Create backup Shell script:

Create the backup script in the directory you created (vi bkDatabaseName.sh)


#!/bin/bash
mysqldump -uroot -proot rtak > /data/backup/rtak_$(date +%Y%m%d_%H%M%S).sql
mysqldump -uroot -proot rtak | gzip > /data/backup/rtak_$(date +%Y%m%d_%H%M%S).sql.gz

Note:

bkDatabaseName.sh replaced with an interesting name

sql backup and gz backup can be chosen 1 or full backup

The username and password need to be replaced

4. Add executable permissions:


chmod u+x bkDatabaseName.sh

Test file execution (./ ES35en.sh)

Note: (1) If the command mysqldump is not found, execute

ln - fs/usr/local mysql/bin/mysqldump/usr/bin (/ usr local/mysql path for mysql installation path)

(2) If there is a warning (Warning: Using a on the command interface can be insecure.) it can be ignored.

(3) Check whether the backup sql file is normal and whether it can be imported into the database normally

5. Add scheduled tasks

Confirm whether crontab is installed:

To execute the crontab command, if command not found, it indicates that it is not installed

Executive command:


crontab -e

Enter the following and save:


*/* * 1 * * /data/backup/bkDatabaseName.sh

/* * 1 * * / several * represent minutes, hours, days, months and days of the week to perform the backup operation

Example: Backup /1 * * * * / (tested) per minute

3 am daily backup /00 3 * * * / (not tested)

6. Stop the backup operation

When there is no need for regular backup, perform this operation, and the normal flow to step 5 is complete


crontab -r

Note: Timely clean sql backups that have long expired to prevent disk occupancy


Related articles: