How to backup MySQL regularly and upload it to Qiniu

How to backup MySQL regularly and upload it to Qiniu

In most application scenarios, we need to back up important data and place it in a safe place in case of emergency.

Common MySQL data backup methods include directly packaging and copying the corresponding database or table files (physical backup), mysqldump full logical backup, xtrabackup incremental logical backup, etc.

Common data storage methods include local storage, FTP upload to a remote server, cloud storage (such as Alibaba Cloud OSS, Qiniu Cloud Storage, etc.), and even local storage.

We may not want to back up manually every time, nor do we want to spend so much time downloading every time, nor do we want to lose it on the server, because we need off-site backup. Then we can try to write a script to regularly back up the database and then automatically upload it to a designated server or cloud storage.

Here, we will talk about how to back up MySQL on a Linux server and upload it to Qiniu Cloud Storage.

Preparation

• Linux

•crontab service

You need to ensure that the crond service is in the started & auto-started state.

•gzip command

The system must be able to execute the gzip command normally to compress files.

•mysqldump command

The system must be able to execute the mysqldump command normally for logical data backup. The data backed up by mysqldump consists of executable SQL and there is no version incompatibility issue.

•qshell tool

qshell is a command line tool that Qiniu Cloud officially implements using the public API in Qiniu documents to facilitate developers to test and use Qiniu API services.

Specific documents and download address: https://developer.qiniu.com/kodo/tools/1302/qshell

• Qiniu Cloud Account

The prerequisite for storing data is of course to have a Qiniu account. Qiniu provides individuals with 10G of free storage space for personal use. Registered Address:
https://portal.qiniu.com/signup?code=3looatwobaxci

• Qiniu storage space

After you have a Qiniu Cloud account, you need to manually create a space (bucket) in the console to store data.

qshell configuration

The qshell we downloaded from the official address is a compressed package that supports multiple system platforms. Select the binary file of our corresponding system and grant it executable permissions. It can also be placed in a directory such as /usr/local/bin/ to facilitate direct calling of the qshell command.

Configure Qiniu account, ak and sk are in Qiniu Cloud Console > Personal Center > Key Management.
qshell account ak sk

This command will write the ak/sk account to ~/.qshell/account.json, and no further configuration is required.

The qshell command we use here is rput, which is to upload a file in multi-part upload mode. Use the document:

https://github.com/qiniu/qshell/blob/master/docs/rput.md

qshell rput <Bucket> <Key> <LocalFile> true

For other detailed functions of qshell, please refer to its documentation.

Script content

#!/bin/sh
#mysql data backup script
#
# use mysqldump --help,get more detail.
dbname=your_dbname
user=your_db_username
password=your_db_password
bakDir=/opt/backup/sql
logFile=/opt/backup/mysqlbak.log
datetime=`date +%Y%m%d%H%M%S`
keepDay=7
echo "-------------------------------------------" >> $logFile
echo $(date +"%y-%m-%d %H:%M:%S") >> $logFile
echo "--------------------------" >> $logFile
cd $bakDir
bakFile=$dbname.$datetime.sql.gz
mysqldump -u $user -p $password $dbname | gzip > $bakFile
echo "Database [$dbname] backup completed" >> $logFile
echo "$bakDir/$bakFile" >> $logFile
echo "Start uploading backup files to Qiniu Cloud Storage" >> $logFile
/usr/local/bin/qshell rput <Bucket> database/$bakFile $bakFile true | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" >> $logFile 2>&1
echo "Delete the backup file ${keepDay} days ago" >> $logFile
find $bakDir -ctime +$keepDay >> $logFile
find $bakDir -ctime +$keepDay -exec rm -rf {} \;
echo " " >> $logFile
echo " " >> $logFile

The database configuration, log files, storage path, <Bucket>, etc. in the script need to be modified by yourself and exist. database/$bakFile represents <Key>, which is the path and file name in Qiniu storage and can be customized.

The script file needs to have executable permissions, and then the script can be executed for testing.

Scheduled tasks

# Execute the backup script at 2 a.m. every day * 2 * * * /opt/backup/baksql.sh

If the scheduled task is not executed, you can check the log /var/log/cron to troubleshoot the problem, or check whether crond is running.

Summarize

The above is the method I introduced to you to regularly back up Mysql and upload it to Qiniu. I hope it will be helpful to you. If you have any questions, please leave me a message and I 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:
  • Shell script to implement mysql scheduled backup, deletion and recovery functions
  • Sharing of mysql scheduled backup Shell script under CentOS
  • MySQL scheduled backup solution (using Linux crontab)
  • Brief analysis of mysql scheduled backup tasks
  • MySQL scheduled database backup operation example
  • How to implement scheduled backup of MySQL database
  • A simple method to implement scheduled backup of MySQL database in Linux
  • Linux implements automatic and scheduled backup of MySQL database every day
  • Mysql database scheduled backup script sharing
  • Implementation of MySQL scheduled backup script under Windows
  • The best way to automatically backup the mysql database (windows server)
  • Using MySQL in Windows: Implementing Automatic Scheduled Backups

<<:  The complete version of the common Linux tool vi/vim

>>:  Detailed explanation of Angular routing basics

Recommend

Solution to forgetting the root password of MySQL 5.7 and 8.0 database

Note: To crack the root password in MySQL5.7, you...

Detailed explanation of the use of Teleport in Vue3

Table of contents Purpose of Teleport How Telepor...

MySQL 5.6.36 Windows x64 version installation tutorial detailed

1. Target environment Windows 7 64-bit 2. Materia...

What does the n after int(n) in MySQL mean?

You may already know that the length 1 of int(1) ...

MySQL table return causes index invalidation case explanation

Introduction When the MySQL InnoDB engine queries...

Summary of the characteristics of SQL mode in MySQL

Preface The SQL mode affects the SQL syntax that ...

Detailed example of inserting custom HTML records in Quill editor

It is already 2020. Hungry humans are no longer s...

How to quickly install Nginx in Linux

Table of contents What is nginx 1. Download the r...

Use pure CSS to disable the a tag in HTML without JavaScript

In fact, this problem has already popped up when I...

Vue implements an example of pulling down and scrolling to load data

Table of contents Step 1: Installation Step 2: Ci...

Vue implements three-dimensional column chart based on echarts

The three-dimensional column chart consists of th...

Vue implements countdown function

This article example shares the specific code of ...