Home  >  Article  >  Database  >  How to implement scheduled backup of mysql database in Linux (code)

How to implement scheduled backup of mysql database in Linux (code)

不言
不言Original
2018-09-11 14:26:571322browse

The content of this article is about how to implement scheduled backup of mysql database (code) in Linux. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Check the disk space:

[root@localhost backup]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/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 file

2. Create the backup directory:

cd /home
mkdir backup
cd backup

3. Create a backup Shell script:

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

#!/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:

Replace bkDatabaseName.sh with an interesting name

You can choose one of sql backup and gz backup, or full backup

The username and password need to be replaced

4. Add executable permissions:

chmod u+x bkDatabaseName.sh

Test whether the file can be executed normally (./bkDatabaseName.sh)

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

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

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

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

5. Add scheduled tasks

Confirm whether crontab is installed:

Execute If the crontab command reports command not found, it means that it is not installed.

Execute the command:

crontab -e

Enter the following content and save:

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

/* * 1 * * / Several * Represents the minutes, hours, dates, months, and days of the week to perform backup operations.

For example: perform backups every minute/1 * * * * / (tested)

Perform backups at 3 a.m. every day/00 3 * * * / (Not tested)

6. Stop the backup operation

When regular backup is not required, perform this operation. The normal process will be completed in step five~

crontab -r

Note: Clean up the long-expired sql backup in time to prevent the disk from filling up

Related recommendations:

Linux scheduled backup mysql database_MySQL

How to implement automatic daily backup of mysql database under linux_MySQL

The above is the detailed content of How to implement scheduled backup of mysql database in Linux (code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn