Implementation script for scheduled database backup in Linux

Implementation script for scheduled database backup in Linux

Scenario: The server database needs to be backed up regularly every day

1. First determine the location of the backup script

I put it in /usr/local/backup and named it bkDatabase.sh

2. Write a shell script

# A few points to note# 1. -password If the password contains brackets or underscores, please quote the password in double quotes# 2. This script backs up the database course1 and compresses it# 3. The name of the backup file is course and is timestampedmysqldump -uroot -ppassword course1 | gzip > /usr/local/backup/course_$(date +%Y%m%d_%H%M%S).sql.gz

3. Add permissions to bash

chmod u+x bkDatabase.sh

4. Test to see if the script is executable correctly

./bkDatabase.sh

5. Open the scheduled task

# The first time you set up a scheduled task, you may be asked to enter vim to edit the scheduled task. Select basic and you can use crontab -e

6. Editorial Content

I have three scheduled tasks here as shown below:

The second one

# Indicates execution once every minute. The script to be executed is /usr/local/backup/bkDatabase.sh
*/1 * * * * /usr/local/backup/bkDatabase.sh

7. View scheduled tasks crontab -l

You can see whether the addition is successful (as shown in the figure)

Check whether the backup is successful in the backup directory

8. Step into the pit

Some scheduled tasks are closed. Use the command service crond status to check whether crond is normal.

If it is normal, just ignore it. If it does not start, restart it once.

For different Linux versions, the distribution has this service

Restart service command: [root@centos6 /]# service crond restart
Start service command: [root@centos6 /]# service crond start
Stop service command: [root@centos6 /]# service crond stop

The distribution does not have this service

Stop service: [root@centos6 /]# /etc/init.d/cron stop
Start the service: [root@centos6 /]# /etc/init.d/cron start

Server data restoration

There are too many pitfalls here, pay attention to the way of decompressing gz files! ! !

1. First, decompress the data backed up by the scheduled task

Note that the course_20190511_214326.sql.gz generated above is decompressed

# Unzip the gz file to generate the course_20190511_214326.sql file gunzip course_20190511_214326.sql.gz

2. Then import the data into the database

If the target server does not have the database you want, you need to create database target database;

# Send the backup file sql to the course table through the < symbol mysql -u root -p indicates the use of mysql database mysql -u root -p course< course_20190511_214326.sql

Then you will be prompted to Enter Password, enter the mysql password to import

The above is the details of how to implement scheduled database backup in Linux. For more information about scheduled database backup in Linux, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • How to set up automatic daily database backup in Linux
  • Linux implements automatic and scheduled backup of MySQL database every day
  • How to automatically back up the MySQL database in Linux every day
  • A simple method to implement scheduled backup of MySQL database in Linux
  • Linux regularly backs up the MySQL database and deletes previous backup files (recommended)

<<:  20 Signposts on the Road to Becoming an Excellent UI (User Interface) Designer

>>:  How to use CSS to fill the parent container div with img images and adjust the container size

Recommend

Zabbix uses PSK shared key to encrypt communication between Server and Agent

Since Zabbix version 3.0, it has supported encryp...

Tomcat Server Getting Started Super Detailed Tutorial

Table of contents 1. Some concepts of Tomcat –1, ...

Solution for using Baidu share on Https page

Since enabling https access for the entire site, ...

Implementation of css transform page turning animation record

Page turning problem scenario B and C are on the ...

Vue implements setting multiple countdowns at the same time

This article example shares the specific code of ...

How to write DROP TABLE in different databases

How to write DROP TABLE in different databases 1....

Detailed explanation of MySQL's MERGE storage engine

The MERGE storage engine treats a group of MyISAM...

JS function call, apply and bind super detailed method

Table of contents JS function call, apply and bin...

How to view the IP address of the Docker container

I always thought that Docker had no IP address. I...

A brief discussion on when MySQL uses internal temporary tables

union execution For ease of analysis, use the fol...

How to run Hadoop and create images in Docker

Reinventing the wheel, here we use repackaging to...

How to reset the root password in Linux mysql-5.6

1. Check whether the MySQL service is started. If...