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

VUE render function usage and detailed explanation

Table of contents Preface The role of render Rend...

React implements dynamic pop-up window component

When we write some UI components, if we don't...

How to enhance Linux and Unix server security

Network security is a very important topic, and t...

Example of how to configure nginx to implement SSL

Environmental Description Server system: Ubuntu 1...

Two methods of restoring MySQL data

1. Introduction Some time ago, there were a serie...

Collection of 25 fonts used in famous website logos

This article collects the fonts used in the logos...

Install Zookeeper under Docker (standalone and cluster)

After starting Docker, let's take a look at t...

Summary of how to use the MySQL authorization command grant

How to use the MySQL authorization command grant:...

Example of troubleshooting method to solve Nginx port conflict

Problem Description A Spring + Angular project wi...

How to use Vue cache function

Table of contents Cache function in vue2 Transfor...

WiFi Development | Introduction to WiFi Wireless Technology

Table of contents Introduction to WiFi Wireless T...

Vue+Vant implements the top search bar

This article example shares the specific code of ...

Vue realizes the whole process of slider drag verification function

Rendering Define the skeleton, write HTML and CSS...

Summary of methods to check whether the port is open in Linux

Method 1: Use lsof command We can use the lsof co...