Home > Operation and Maintenance > Linux Operation and Maintenance > Linux MySQL scheduled backup and upload to git repository

Linux MySQL scheduled backup and upload to git repository

藏色散人
Release: 2019-12-24 14:29:29
forward
2457 people have browsed it

Introduction

When we deploy our small and medium-sized projects, we usually choose mysql as our storage tool for data storage. So for a large project, every day The amount of data is very large. For the data generated every day, if our website or server is attacked one day, our data loss will be an explosion, so it is natural to design a backup of the database. So what kind of backup is What do we want?

We may store the backed up data files in the server directory. The backup cycle is of course based on the amount of data. Here we usually back up once every morning. After backup The files are stored in the directory of our server, but if the server crashes one day, the backed up files will be gone. So we imagine a good solution is to back up the database every day and automatically submit each backup to the remote warehouse. Here I take Code Cloud as an example.

Recommended study: "linux tutorial"

Code Cloud

First establish a remote warehouse. Here I chose Code Cloud

to create a new private warehouse. Of course, in order to submit files without a password every time, the ssh key can be generated in the server

Create a new backup on the server

In order to store the backed up files on the server, create a new backup directory

$ mkdir /bak
Copy after login

After entering the directory, continue to create two folders, mysqlBak and shDir, one for the script file, One is to put the specific backup files.

Now we can create a new script, enter the shDir directory and execute

$ vim mysqlBak.sh
Copy after login

The specific code is as follows:

#!bin/sh
################### 数据库配置信息 #######################
createAt=`date +%Y-%m-%d-%H:%M:%S`
user=root
passwd=ghc1996
dbname=ispace
mysql_back_path=/bak/mysqlBak
################### 执行命令 #######################
mysqldump -u $user -p$passwd $dbname > $mysql_back_path/$createAt.sql
cd /bak/mysqlBak
/usr/local/git/bin/git add .
/usr/local/git/bin/git commit -m $createAt
/usr/local/git/bin/git push
Copy after login

This is just a It is a simple script, I think it is easy to understand for those who know Linux. What it does is back up the database and push it to the remote warehouse.

So since it is a script, we need to indicate when to execute this script and specify the execution of the script.

$ crontab -e
Copy after login

We hope to perform a backup in the early morning of every day and add it to the remote warehouse, then add

$ 0 0 * * * /bin/sh /bak/shDir/mysqlbak.sh
Copy after login

There are only five parts of the time specified in the Linux crontab

Linux MySQL scheduled backup and upload to git repository

Use the command crontab -e and edit the timing script directly. The specific name of the time

For example:

0 0,3,7,9,12,15,18,21,23 * * * /bin/sh /bak/shell/mysqlBak.sh
Copy after login

In this case, I will be at 0,,3,7,9,12,15,18,21,23 o'clock every day Execute this script file, then this will realize the basic database backup

Execute the scheduled task:

$ crontab -l
Copy after login

If the service does not start, then restart the scheduled task

$ systemctl restart crond
Copy after login

Then Now that the scheduled task has been started, the prerequisite for submitting the remote warehouse is to generate the ssh key on the server and add it to the code cloud, which is also mentioned above.

For the directory where files need to be submitted, initialize the git directory. Okay, this round can constitute the tasks we need.

Of course you may encounter some problems during the process, I have listed them in the relevant links below.

This way As a result, we can back up our database in the early morning of every day and submit it to the remote warehouse of our code cloud at the same time. This is also the effect we want.

I also said that the backup cycle depends on us It depends on the size of the data volume of the project.

Each framework has its own backup mechanism. What I write here is a general backup mechanism implemented by ourselves

The above is the detailed content of Linux MySQL scheduled backup and upload to git repository. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:ruoxiaozh.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template