How to automatically back up the mysql database regularly

How to automatically back up the mysql database regularly

We all know that data is priceless. If we don’t back up the data, it is equivalent to leaving the data naked. Once the server has problems, we can only cry. The following will introduce the automatic backup of the MySQL database. You can choose where to store the backup data according to the situation.

The first one is that the data runs on the server. If you only want to regularly backup the database on the server (the server system here is centos7), you can set it as follows:

First, write the sh file: /usr/local/backup/bkDatabase.sh:

#!/bin/bash

mysqldump -uusername -ppassword DatabaseName | gzip > /home/backup/DatabaseName_$(date +%Y%m%d_%H%M%S).sql.gz

Add permissions: chmod u+x bkDatabase.sh

Test whether the execution is ./bkDatabase.sh

Add scheduled tasks to the system

Execute the command crontab -e

Edit the content and add the example of executing the script file regularly at 23:00 every day for backup:

00 23 * * * /usr/local/backup/bkDatabaseName.sh

Check whether the scheduled task is effective: crontab -l

Restart the crontab service: service crond restart

The second method is to automatically and regularly back up the MySQL database of the remote server and store it locally (the local operating system is Windows 7). This method is safer and is therefore recommended.

First, write the backup.bat file.

@echo off
set "Ymd=%date:~,4%%date:~5,2%%date:~8,2%"
md D:\mysite-backup\%Ymd%\
C:\"Program Files"\"mysql-server 5.7.14"\bin\mysqldump --opt -hx.xxx database > D:\database-backup\%Ymd%\database_%Ymd%.sql
echo "Database backup completed"

Then modify the my.ini file to add the following content and restart the mysql service.

[mysqldump]
user=root
password=xyq

Click backup.bat to test whether the backup file is generated successfully.

Create a scheduled task locally to back up the remote database

1. Open Control Panel > Administrative Tools > Task Scheduler and create a task:

Write the picture description here

2. Fill in the trigger and set the execution time:

Write the picture description here

3. Add execution script file:

Write the picture description here

Now you are done. The system will regularly back up the MySQL database on the remote server at the specified time.

This is the end of this article about the method and steps of MySQL scheduled automatic database backup. For more relevant MySQL scheduled automatic backup content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • SQL server automatic database backup method
  • The best way to automatically backup the mysql database (windows server)
  • Complete steps to set up an automatic backup strategy for SQL Server database
  • How to implement scheduled backup of MySQL database
  • Detailed explanation of several methods of MySQL automatic backup and recovery (graphic tutorial)
  • Manual and scheduled backup steps for MySQL database

<<:  Use href in html to pop up a file download dialog box when clicking a link

>>:  Implementation of building custom images with Dockerfile

Recommend

Sample code for installing Jenkins using Docker

Two problems that are easy to encounter when inst...

How to show or hide common icons on the desktop in Windows Server 2012

Windows Server 2012 and Windows Server 2008 diffe...

Usage of the target attribute of the html tag a

1: If you use the tag <a> to link to a page,...

Detailed explanation of CSS style sheets and format layout

Style Sheets CSS (Cascading Style Sheets) is used...

3 codes for automatic refresh of web pages

In fact, it is very simple to achieve this effect,...

MySQL 5.7.17 installation graphic tutorial (windows)

I recently started learning database, and I feel ...

The difference between html, xhtml and xml

Development Trends: html (Hypertext Markup Languag...

In-depth understanding of the matching logic of Server and Location in Nginx

Server matching logic When Nginx decides which se...

Raspberry Pi msmtp and mutt installation and configuration tutorial

1. Install mutt sudo apt-get install mutt 2. Inst...

Implementation of code optimization for Vue2.x project performance optimization

Table of contents 1 Use of v-if and v-show 2. Dif...

How to use the Clipboard API in JS

Table of contents 1. Document.execCommand() metho...

About Vue's 4 auxiliary functions of Vuex

Table of contents 1. Auxiliary functions 2. Examp...

Install ethereum/Ethereum from scratch under CentOS7

Table of contents Preface Add sudo write permissi...

Record of the actual process of packaging and deployment of Vue project

Table of contents Preface 1. Preparation - Server...