How to implement scheduled backup of MySQL database

How to implement scheduled backup of MySQL database

1. Create a shell script

 vim backupdb.sh
 Create the script as follows:
 #!/bin/sh
 db_user="root"
 db_passwd="123456"
 db_name="userdb"
 name="$(date +"%Y%m%d%H%M%S")" 
 /usr/bin/mysqldump -u$db_user -p$db_passwd $db_name >>/home/backup/$name.sql
 illustrate:
 /usr/bin/mysqldump: the path of the mysqldump backup tool in the mysql database installation directory dbname: the name of the database to be backed up /home/backup/$name.sql: the backup file output location, which can be set according to the situation

2. Add execution permissions to the shell script

chmod +x backupdb.sh

3. Add a scheduled task to the script

crontab -e
Enter the name of the previous line to edit the scheduled task, and finally add the following content 00 01 * * * /bin/sh /usr/local/mysql/backupdb.sh
The above scheduled task means that the automatic backup script will be executed at 1:00 a.m. every day to perform a scheduled backup of the MySQL database.

Description of crontab file:

In the crontab file created by the user, each line represents a scheduled task, and each field in each line represents a setting. Its format is divided into six fields per line. The first five fields are time setting fields, and the sixth field is the command field to be executed.

The format is as follows: minute hour day month week command

Parameter Description:

minute: represents the minute, which can be any integer from 0 to 59.
hour: represents the hour, which can be any integer from 0 to 23.
day: represents the date, which can be any integer from 1 to 31.
month: represents the month, which can be any integer from 1 to 12.
week: represents the day of the week, which can be any integer from 0 to 7, where 0 or 7 represents Sunday.
command: The command to be executed, which can be a Linux system command or a script file written by yourself.

Summarize

The above is the scheduled backup of MySQL database introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • How to automatically back up the mysql database regularly
  • SQL server automatic database backup method
  • The best way to automatically backup the mysql database (windows server)
  • Complete steps to set up an automatic backup strategy for SQL Server database
  • Detailed explanation of several methods of MySQL automatic backup and recovery (graphic tutorial)
  • Manual and scheduled backup steps for MySQL database

<<:  How to implement remote access control in Centos 7.4

>>:  Three solutions for sub-functions accessing external variables in JavaScript

Recommend

How to hide a certain text in HTML?

Text hiding code, hide a certain text in HTML Copy...

The concept and characteristics of MySQL custom variables

A MySQL custom value is a temporary container for...

WeChat applet custom tabBar step record

Table of contents 1. Introduction 2. Customize ta...

How to optimize MySQL query speed

In the previous chapters, we introduced how to ch...

Example code for Html layered box-shadow effect

First, let’s take a look at the picture: Today we...

CSS menu button animation

To write a drop-down menu, click the button. The ...

A brief discussion on the magical slash in nginx reverse proxy

When configuring nginx reverse proxy, the slashes...

Use overflow: hidden to disable page scrollbars

Copy code The code is as follows: html { overflow...

MySQL index usage monitoring skills (worth collecting!)

Overview In a relational database, an index is a ...

How to change the character set encoding to UTF8 in MySQL 5.5/5.6 under Linux

1. Log in to MySQL and use SHOW VARIABLES LIKE &#...

MySQL index leftmost principle example code

Preface I was recently reading about MySQL indexe...

Vue two-choice tab bar switching new approach

Problem Description When we are working on a proj...

How to match the size of text in web design: small text, big experience

With the rise of mobile terminals such as iPad, p...