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

Use tomcat to deploy SpringBoot war package in centos environment

Prepare war package 1. Prepare the existing Sprin...

How to configure environment variables in Linux environment

JDK download address: http://www.oracle.com/techn...

Example of how to adapt the Vue project to the large screen

A brief analysis of rem First of all, rem is a CS...

RHEL7.5 mysql 8.0.11 installation tutorial

This article records the installation tutorial of...

Vue Beginner's Guide: Environment Building and Getting Started

Table of contents Initial Vue Building a Vue deve...

The role of nextTick in Vue and several simple usage scenarios

Purpose Understand the role of nextTick and sever...

How to build php-nginx-alpine image from scratch in Docker

Although I have run some projects in Docker envir...

vitrualBox+ubuntu16.04 install python3.6 latest tutorial and detailed steps

Because I need to use Ubuntu+Python 3.6 version t...

Detailed explanation of styles in uni-app

Table of contents Styles in uni-app Summarize Sty...

Docker deployment RabbitMQ container implementation process analysis

1. Pull the image First, execute the following co...

H tags should be used reasonably in web page production

HTML tags have special tags to handle the title of...

Summary of 10 amazing tricks of Element-UI

Table of contents el-scrollbar scroll bar el-uplo...

Bootstrap 3.0 learning notes button style

This article mainly explains the style of buttons...

Nginx reverse proxy forwards port 80 requests to 8080

Let's first understand a wave of concepts, wh...