How to use crontab to backup MySQL database regularly in Linux system

How to use crontab to backup MySQL database regularly in Linux system

Use the system crontab to execute backup files regularly and save the backup results by date to achieve the purpose of backup.

1. Create a path to save the backup file /mysqldata

#mkdir /mysqldata

2. Create /usr/sbin/bakmysql file

#vi /usr/sbin/bakmysql

enter

rq= date +%Y%m%d
tar zcvf /mysqldata/mysql$rq.tar.gz /var/lib/mysql

Or written as

rq= date +%Y%m%d
mysqldump –all-databases -u root -p password > /mysqldata/mysql$rq.sql

/var/lib/mysql is the directory where your database files are located. Some users may have /usr/local/mysql/data. This may be different for each user.

/mysqldata/ indicates the directory where backup files are saved. Everyone can do this according to their own requirements.

3. Modify the file attributes to make it executable

# chmod +x /usr/sbin/bakmysql

4. Modify /etc/crontab

#vi /etc/crontab

Add below

01 3 * * * root /usr/sbin/bakmysql

Indicates that the backup is performed at 3 o'clock every day

5. Restart crond

# /etc/rc.d/init.d/crond restart

Finish.

So every day you can see such files in /mysqldata

mysql20040619.tar.gz

You can just download it.

Well, the above is how to quickly use crontab to schedule backup of MySQL in Linux system. For more information about the use of crontab, please see the following related articles

You may also be interested in:
  • Detailed explanation of the idea of ​​using mysqldump+expect+crontab to implement mysql periodic cold backup in linux
  • MySQL scheduled backup solution (using Linux crontab)
  • Analysis of the reasons why the mysql-libs* crontab command that comes with Linux 6.7 cannot be used after uninstallation
  • MySQL scheduled backup using crontab scheduled backup example under Linux

<<:  js uses FileReader to read local files or blobs

>>:  Detailed process of building mongodb and mysql with docker-compose

Recommend

Solve the problem that IN subquery in MySQL will cause the index to be unusable

Today I saw a case study on MySQL IN subquery opt...

Detailed explanation of Shell script control docker container startup order

1. Problems encountered In the process of distrib...

Mysql solution to improve the efficiency of copying large data tables

Preface This article mainly introduces the releva...

How to insert pictures into HTML pages and add map index examples

1. Image formats supported on the WEB: GIF: can s...

JavaScript implements checkbox selection function

This article example shares the specific code of ...

In-depth analysis of MySQL data type DECIMAL

Preface: When we need to store decimals and have ...

Detailed tutorial on installing mysql 8.0.20 on CentOS7.8

1. Install MySQL software Download and install My...

Analysis of the Linux input subsystem framework principle

Input subsystem framework The linux input subsyst...

How to install multiple mysql5.7.19 (tar.gz) files under Linux

For the beginner's first installation of MySQ...

Cross-browser local storage Ⅰ

Original text: http://www.planabc.net/2008/08/05/...

W3C Tutorial (15): W3C SMIL Activities

SMIL adds support for timing and media synchroniz...

VMware installation of Centos8 system tutorial diagram (Chinese graphical mode)

Table of contents 1. Software and system image 2....

A detailed introduction to deploying RabbitMQ environment with docker

Prerequisites: Docker is already installed 1. Fin...