


Linux MySQL scheduled backup and upload to git repository
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
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
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
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
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
There are only five parts of the time specified in the Linux crontab
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
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
If the service does not start, then restart the scheduled task
$ systemctl restart crond
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

Ouyi OKX, the world's leading digital asset exchange, has now launched an official installation package to provide a safe and convenient trading experience. The OKX installation package of Ouyi does not need to be accessed through a browser. It can directly install independent applications on the device, creating a stable and efficient trading platform for users. The installation process is simple and easy to understand. Users only need to download the latest version of the installation package and follow the prompts to complete the installation step by step.

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

Ouyi, also known as OKX, is a world-leading cryptocurrency trading platform. The article provides a download portal for Ouyi's official installation package, which facilitates users to install Ouyi client on different devices. This installation package supports Windows, Mac, Android and iOS systems. Users can choose the corresponding version to download according to their device type. After the installation is completed, users can register or log in to the Ouyi account, start trading cryptocurrencies and enjoy other services provided by the platform.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...
