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

Problems and solutions encountered when installing mininet on Ubuntu 16.04.4LTS

Mininet Mininet is a lightweight software defined...

CSS shadow animation optimization tips

This technique comes from this article - How to a...

MySQL can actually implement distributed locks

Preface In the previous article, I shared with yo...

Detailed explanation of Mysql function call optimization

Table of contents Function call optimization Func...

mysql5.7.14 decompressed version installation graphic tutorial

MySQL is divided into Community Edition (Communit...

Detailed steps and problem solving methods for installing MySQL 8.0.19 on Linux

I recently bought a Tencent Cloud server and buil...

Example verification MySQL | update field with the same value will record binlog

1. Introduction A few days ago, a development col...

How to simply encapsulate axios in vue

Inject axios into Vue import axios from 'axio...

Docker sets up port mapping, but cannot access the solution

#docker ps check, all ports are mapped CONTAINER ...

The use of vue directive v-bind and points to note

Table of contents 1. v-bind: can bind some data t...

HTML table tag tutorial (17): table title vertical alignment attribute VALIGN

The table caption can be placed above or below th...

Implementation code of Nginx anti-hotlink and optimization in Linux

Hide version number The version number is not hid...

This article will show you how to use Vue 3.0 responsive

Table of contents Use Cases Reactive API related ...

Mariadb remote login configuration and problem solving

Preface: The installation process will not be des...