Automatic backup of MySQL database using shell script

Automatic backup of MySQL database using shell script

Automatic backup of MySQL database using shell script

It is a good habit to back up the database frequently. Although the probability of database damage or data loss is very low, once such a thing happens, it is useless to regret. Generally, there is a function button for backing up the database in the background of a website or application, but it needs to be executed manually. We need a secure way to automatically back up every day. The following shell script is how you can set up Crontab to back up the MySQL database every day.

#!/bin/bash
# Database authentication user=""
 password=""
 host=""
 db_name=""
# Other backup_path="/path/to/your/home/_backup/mysql"
 date=$(date +"%d-%b-%Y")
# Set the default permissions for exported files to umask 177
# Dump database to SQL file mysqldump --user=$user --password=$password --host=$host $db_name > $backup_path/$db_name-$date.sql

Through the above script, we can export a SQL backup file every day, and the file name is generated according to the date of the day. Over time, a lot of such files will be generated. It is necessary to delete some old backup files regularly. The following command line is for this task. You can add it after the above script.

# Delete the backup files older than 30 days find $backup_path/* -mtime +30 -exec rm {} \;

I once encountered a problem when using the above script. There was no error when Crontab executed the script export regularly, but the exported SQL file was empty. However, when I logged into the console and executed the script manually, the backup was successful. Later I found that the Crontab execution script lacked system environment information and could not find mysqldump. The correction method was to use the full path of mysqldump. The reason why there is no error message is that mysqldump outputs the error message to stderr. Add "2>&1" to the end of the command so that you can see the error message:

mysqldump -ujoe -ppassword > /tmp/somefile 2>&1

Thank you for reading, I hope it can help you, thank you for your support of this site!

You may also be interested in:
  • HBASE commonly used shell commands, add, delete, modify and query methods
  • Shell script to implement mysql scheduled backup, deletion and recovery functions
  • How to simply process MySQL query results in shell
  • Write a mysql data backup script using shell
  • How to quickly log in to MySQL database without password under Shell
  • Use shell scripts to add, delete, modify, and check mysql and configure my.cnf

<<:  View the port number occupied by the process in Linux

>>:  JavaScript to achieve the effect of clicking on the self-made menu

Recommend

vue.js Router nested routes

Preface: Sometimes in a route, the main part is t...

Some methods to optimize query speed when MySQL processes massive data

In the actual projects I participated in, I found...

Rounding operation of datetime field in MySQL

Table of contents Preface 1. Background 2. Simula...

HTML table tag tutorial (13): internal border style attributes RULES

RULES can be used to control the style of the int...

Understand the implementation of Nginx location matching in one article

Since the team is separating the front-end and ba...

Five solutions to cross-browser problems (summary)

Brief review: Browser compatibility issues are of...

A time-consuming troubleshooting process record of a docker error

Table of contents origin Environmental Informatio...

How to configure path alias for react scaffolding

The react version when writing this article is 16...

VUE Getting Started Learning Event Handling

Table of contents 1. Function Binding 2. With par...

Docker installation and deployment of Net Core implementation process analysis

1. Docker installation and settings #Install Cent...

How to use positioning to center elements (web page layout tips)

How to center an element in the browser window He...

Design and implementation of Vue cascading drop-down box

Table of contents 1. Database design 2. Front-end...

Docker pull image and tag operation pull | tag

I re-read the source code of the Fabric project a...

A brief analysis of controlled and uncontrolled components in React

Table of contents Uncontrolled components Control...

Why node.js is not suitable for large projects

Table of contents Preface 1. Application componen...