Mysql database scheduled backup script sharing

Mysql database scheduled backup script sharing

BackUpMysql.sh script

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

#Database ip
DBHOST=''
#Database username DBUSER=''
#Database password DBPASSWD=''
#Databases that need to be backed up, multiple databases are separated by spaces DBNAME=''

#Backup time backtime=`date +%Y-%m-%d_%H%M%S`
#Backup path (current directory)
BACKPATH=$(dirname $(readlink -f $0))
echo $BACKPATH
#Log backup path LOGPATH="${BACKPATH}/log"
#Data backup path DBPATH="${BACKPATH}/db"

#Create a backup directory [ ! -d "${LOGPATH}" ] && mkdir -p "${LOGPATH}"
[ ! -d "${DBPATH}" ] && mkdir -p "${DBPATH}"

#Log record header echo "Backup time is ${backtime}, backup database table ${DBNAME} starts" >> ${LOGPATH}/mysqlback.log

#Formal backup database for table in $DBNAME; do
source=`mysqldump -u ${DBUSER} -h${DBHOST} -p${DBPASSWD} ${table}> ${LOGPATH}/${backtime}.sql` 2>> ${LOGPATH}/mysqlback.log;

#Backup succeeds the following operations $? Get the result of the previous command, 0 represents success if [ "$?" == 0 ]; then
cd ${LOGPATH}
#To save hard disk space, compress the database tar -czf ${DBPATH}/${table}${backtime}.tar.gz ./${backtime}.sql > /dev/null
#Delete the original file and keep only the compressed file rm -f ${LOGPATH}/${backtime}.sql
#Delete the backup seven days ago, that is, only save the backup within 7 days find $DBPATH -name "*.tar.gz" -type f -mtime +7 -exec rm -rf {} \; > /dev/null 2>&1
echo "Database table ${DBNAME} is backed up successfully!!" >> ${LOGPATH}/mysqlback.log
else
#If the backup fails, perform the following operations echo "Database table ${DBNAME} backup failed!!" >> ${LOGPATH}/mysqlback.log
fi
done

Use crontab to execute the BackUpMysql.sh script regularly, and configure it to execute at 12:00 every night

Run crontab -e

enter

59 23 * * * /data/mysqlbak/BackUpMysql.sh

Author: Low-key Little Panda Link: http://ilovey.live/archives/Mysqlbackupshell

The above is the detailed content of the Mysql database scheduled backup script. For more information about the Mysql scheduled backup script, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Shell script to backup MySQL database data regularly and retain it for a specified time
  • MySQL scheduled database backup operation example
  • Linux implements scheduled backup of MySQL database and deletes backup files older than 30 days
  • How to implement scheduled backup of MySQL database
  • A simple method to implement scheduled backup of MySQL database in Linux
  • The best way to automatically backup the mysql database (windows server)
  • Several ways to schedule backup of MySQL database (comprehensive)

<<:  Vue implements tree table

>>:  Analysis of two implementation methods for adding static routing in Linux

Recommend

JavaScript to achieve a simple magnifying glass effect

There is a picture in a big box. When you put the...

JavaScript history object explained

Table of contents 1. Route navigation 2. History ...

Detailed explanation of MySQL foreign key constraints

Official documentation: https://dev.mysql.com/doc...

MySQL permission control details analysis

Table of contents 1. Global level 2. Database lev...

JavaScript Basics Operators

Table of contents 1. Operators Summarize 1. Opera...

A super detailed Vue-Router step-by-step tutorial

Table of contents 1. router-view 2. router-link 3...

Implementing CommonJS modularity in browsers without compilation/server

Table of contents introduction 1. What is one-cli...

Introduction to Javascript DOM, nodes and element acquisition

Table of contents DOM node Element node: Text nod...

Example code of javascript select all/unselect all operation in html

Copy code The code is as follows: <html> &l...

Analyzing the MySql CURRENT_TIMESTAMP function by example

When creating a time field DEFAULT CURRENT_TIMEST...

Detailed explanation of NodeJS modularity

Table of contents 1. Introduction 2. Main text 2....

In IIS 7.5, HTML supports the include function like SHTML (add module mapping)

When I first started, I found a lot of errors. In...

Interpretation and usage of various React state managers

First of all, we need to know what a state manage...

How to analyze SQL execution plan in MySQL through EXPLAIN

Preface In MySQL, we can use the EXPLAIN command ...