Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to back up MySQL data in Linux to git Warehouse regularly

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

How to back up MySQL data in Linux to git warehouse regularly? I believe that many novice rookies have not yet learned this skill. Through the summary of this article, I hope you can learn this skill. The following materials are the steps for implementation.

Brief introduction

When we deploy our small and medium-sized projects, in data storage, we usually choose mysql as our storage tool. So for a large project, the amount of data every day is very large. For the data generated every day, if one day our website or server is attacked, our data loss is very explosive, so naturally it is designed to back up the database. So what kind of backup do we want?

The backup data files may be stored in the server directory, the backup cycle is of course based on the amount of data, here we usually back up in the early morning of every day. The backed-up files are stored in the directory of our server, but if the server crashes one day, then the backed-up files will be gone. so we imagine a good solution is that every backup of the database is automatically submitted to the remote warehouse. Here I will take the code cloud as an example.

Code cloud

First of all, we set up a remote warehouse, where I chose Ma Yun.

Create a new private warehouse. Of course, in order to avoid password submission files each time, ssh key can be generated in the server.

Server New backup

Create a new backup directory on the server to store the backed-up files

$mkdir / bak

After entering this directory, continue to create two new folders mysqlBak and shDir, one is to put the script files, and the other is to put the backup files.

Next, we can create a new script and execute it after entering the shDir directory.

$vim mysqlBak.sh

The specific code is as follows:

#! bin/sh# database configuration information # createAt= `date +% Y-%m-%d-%H:%M:% S`user = rootpasswd=ghc1996dbname=ispacemysql_back_path=/bak/mysqlBak# execution command # # mysqldump-u $user-p$passwd $dbname > $mysql_back_path/$createAt.sqlcd / bak/mysqlBak/usr/local/git/bin/git add. / usr/local/git/bin/git commit-m $createAt/usr/local/git/bin/git push

Here is just a simple script, I want to know linux is easy to understand, the implementation is to back up the database and push to the remote warehouse.

So since it is a script, we need to specify when to execute the script and specify that the script is executed.

$crontab-e

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

$0 0 * / bin/sh / bak/shDir/mysqlbak.sh

The time specified for the crontab of linux has only five parts

Use the command crontab-e and edit the timing script directly. Time + specific name

For example, Liezi is:

00 bin/sh 3 7 9 12 15 18 21 23 * / bin/sh / bak/shell/mysqlBak.sh

In this way, I will execute this script file at 23 o'clock every day, which means that I will execute this script file at 23 o'clock every day, so that I can backup the basic database.

Perform scheduled tasks:

$crontab-l

Restart the scheduled task if the service is not started

$systemctl restart crond

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

Initialize the git directory for the directory that needs to submit files, so that the bureau can constitute the task we need.

Of course, there may be some problems in the process, I have listed the relevant links below.

In this way, we can back up our database every morning and submit it to our remote warehouse, which is what we want.

I also said that the backup cycle depends on the amount of data in our project.

Each framework has its own backup mechanism. What I write here is a general backup mechanism that we have implemented.

The above is the method of regularly backing up the MySQL data in Linux to the git warehouse, and you need to use it yourself to understand the details. If you want to know more about it, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report