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

A detailed explanation of how React Fiber works

Table of contents What is React Fiber? Why React ...

Docker builds cluster MongoDB implementation steps

Preface Due to the needs of the company's bus...

CSS hacks \9 and \0 may not work for hacking IE11\IE9\IE8

Every time I design a web page or a form, I am tr...

Centos8.3, docker deployment springboot project actual case analysis

introduction Currently, k8s is very popular, and ...

Summary of important components of MySQL InnoDB

Innodb includes the following components 1. innod...

CentOS7 installation zabbix 4.0 tutorial (illustration and text)

Disable SeLinux setenforce 0 Permanently closed: ...

MySQL 8.0.14 installation and configuration method graphic tutorial

This article records the installation and configu...

Install CentOS 7 on VMware14 Graphic Tutorial

Introduction to CentOS CentOS is an enterprise-cl...

Problems encountered by MySQL nested transactions

MySQL supports nested transactions, but not many ...

Detailed explanation of mktemp, a basic Linux command

mktemp Create temporary files or directories in a...

Native js implements custom scroll bar component

This article example shares the specific code of ...

Detailed steps to install MySQL 5.7 via YUM on CentOS7

1. Go to the location where you want to store the...

JS realizes picture digital clock

This article example shares the specific code of ...