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

Essential knowledge for web development interviews and written tests (must read)

The difference between inline elements and block-...

Example of using MySQL to count the number of different values ​​in a column

Preface The requirement implemented in this artic...

In-depth analysis of the diff algorithm in React

Understanding of diff algorithm in React diff alg...

Several methods to clear floating (recommended)

1. Add an empty element of the same type, and the...

How to set up Referer in Nginx to prevent image theft

If the server's images are hotlinked by other...

MySQL transaction concepts and usage in-depth explanation

Table of contents The concept of affairs The stat...

Briefly describe the difference between Redis and MySQL

We know that MySQL is a persistent storage, store...

js precise calculation

var numA = 0.1; var numB = 0.2; alert( numA + num...

MySQL 5.5.27 winx64 installation and configuration method graphic tutorial

1. Installation Package MYSQL service download ad...

Example of how to create a local user in mysql and grant database permissions

Preface When you install MySQL, you usually creat...

MySQL uses aggregate functions to query a single table

Aggregate functions Acts on a set of data and ret...

CSS screen size adaptive implementation example

To achieve CSS screen size adaptation, we must fi...

How to inherit CSS line-height

How is Line-height inherited?Write a specific val...