How to implement scheduled backup of MySQL in Linux

How to implement scheduled backup of MySQL in Linux

In actual projects, the database needs to be backed up frequently to prevent emergencies. Some time ago, my database was invaded and all the data was lost. Fortunately, it was just a test database at the time. However, after this crisis, I began to realize this problem.

First write a test database backup command:

#!/bin/sh
DB_USER="root"
DB_PASS="1234"
DB_DATA_NAME="hzl"
BakDir="/data/mysql-data-back"
echo "/usr/bin/mysqldump"
echo "-------------------------------------------" >> $BakDir
echo $(date +"%y-%m-%d %H:%M:%S") >> $BakDir
 
cd $BakDir
/usr/bin/mysqldump --no-defaults -u$DB_USER -p$DB_PASS $DB_DATA_NAME|gzip > $BakDir/db_`date +%F`.gz
 
exit 0

The command is as above, written directly into a shell file. If the file runs successfully, a backup .gz file can be manually generated in the directory.

Please note that the .sh file cannot be run at first.

chmod +x sqlAutoBak.sh

Okay, the above is relatively simple, let’s start the main part below, which is to make the script execute automatically every day.

1. Confirm whether crontab is installed

crontab -l

You can see a list of executed scripts, indicating that the installation was successful. If you don't have one, just find an installation tutorial online.

[root@VM_0_4_centos ~]# crontab -l
*/1 * * * * /usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &

2. Add the backup script to the crontab service

crontab -e

Press the a key to enter edit mode

Enter 0 */1 * * * /home/work/start-service.sh

Press ctrl+c to exit edit mode

Press shift+: and enter wq to exit crontab

Then see the results

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • MySQL multi-instance deployment and installation guide under Linux
  • MySQL 8.0.25 installation and configuration tutorial under Linux
  • How to start multiple MySQL databases on a Linux host
  • Steps and pitfalls of upgrading linux mysql5.5 to mysql5.7
  • Solve the problem of no my.cnf file in /etc when installing mysql on Linux
  • Steps to install MySQL using Docker under Linux
  • Detailed explanation of how to manually deploy a remote MySQL database in Linux
  • Detailed explanation of the idea of ​​using mysqldump+expect+crontab to implement mysql periodic cold backup in linux
  • Aliyun Linux compile and install php7.3 tengine2.3.2 mysql8.0 redis5 process detailed explanation
  • How to reset the root password in Linux mysql-5.6
  • Use MySQL to open/modify port 3306 and open access permissions in Ubuntu/Linux environment
  • MySQL scheduled backup solution (using Linux crontab)
  • Detailed tutorial on installing MySQL database in Linux environment
  • How to automatically backup mysql remotely under Linux
  • Linux MySQL root password forgotten solution
  • Detailed tutorial on installing mysql-8.0.20 under Linux
  • How to use MyCat to implement MySQL master-slave read-write separation in Linux

<<:  The principle and implementation of js drag effect

>>:  Let's talk about Vue's mixin and inheritance in detail

Recommend

MySQL optimization query_cache_limit parameter description

query_cache_limit query_cache_limit specifies the...

Summary of clipboard.js usage

Table of contents (1) Introduction: (2) The ways ...

Pure CSS3 to achieve pet chicken example code

I have read a lot of knowledge and articles about...

MySQL 8.0.13 decompression version installation graphic tutorial under Windows

This article shares with you the MySQL 8.0.13 ins...

HTML discount price calculation implementation principle and script code

Copy code The code is as follows: <!DOCTYPE HT...

Steps to modify the MySQL database data file path under Linux

After installing the MySQL database using the rpm...

Vue Element-ui implements tree control node adding icon detailed explanation

Table of contents 1. Rendering 2. Bind data and a...

WeChat applet to obtain mobile phone number step record

Preface Recently, I encountered such a problem wh...

JavaScript Prototype Details

Table of contents 1. Overview 1.1 What is a proto...

JavaScript realizes magnifying glass special effects

The effect to be achieved: When the mouse is plac...

Example of cross-database query in MySQL

Preface In MySQL, cross-database queries are main...

Use xshell to connect to the Linux server

Benefits of using xshell to connect to Linux We c...

Detailed explanation of the use of React.cloneElement

Table of contents The role of cloneElement Usage ...

web.config (IIS) and .htaccess (Apache) configuration

xml <?xml version="1.0" encoding=&qu...

How to manage multiple projects on CentOS SVN server

One demand Generally speaking, a company has mult...