Detailed explanation of how to use the mysql backup script mysqldump

Detailed explanation of how to use the mysql backup script mysqldump

This article shares the MySQL backup script for your reference. The specific contents are as follows

#!/bin/bash
#Full backup mode, usually executed on the slave machine, suitable for small and medium-sized MySQL databases #Delete backups older than 15 days #Author: fafu_li
#Date: 2015.08.10

source /etc/profile #Load system environment variablessource ~/.bash_profile #Load user environment variablesset -o nounset #Exit when referencing uninitialized variables#set -o errexit #Exit when an error occurs when executing a shell commanduser="root"
password="123456"
host="localhost"
port="3306"
#Database to be backed up, array db=("test")
#Lock mode during backup,
#MyISAM is locked table --lock-all-tables,
#InnoDB locks rows --single-transaction
lock="--single-transaction"
mysql_path="/usr/local/mysql"
backup_path="${mysql_path}/backup"
date=$(date +%Y-%m-%d_%H-%M-%S)
day=15
backup_log="${mysql_path}/backup.log"

#Create a backup directory if [ ! -e $backup_path ]; then
  mkdir -p $backup_path
fi

#Delete the previous backup find $backup_path -type f -mtime +$day -exec rm -rf {} \; > /dev/null 2>&1

echo "Start backing up database: ${db[*]}"

#Backup and compress backup_sql(){
  dbname=$1
  backup_name="${dbname}_${date}.sql"
  #-R backup stored procedures, functions, triggers mysqldump -h $host -P $port -u $user -p$password $lock --default-character-set=utf8 --flush-logs -R $dbname > $backup_path/$backup_name  
  if [[ $? == 0 ]];then
    cd $backup_path
    tar zcpvf $backup_name.tar.gz $backup_name
    size=$(du $backup_name.tar.gz -sh | awk '{print $1}')
    rm -rf $backup_name
    echo "$date backup $dbname($size) successful"
  else
    cd $backup_path
    rm -rf $backup_name
    echo "$date backup $dbname failed"
  fi
}

#Loop backup length=${#db[@]}
for (( i = 0; i < $length; i++ )); do
    backup_sql ${db[$i]} >> $backup_log 2>&1
done

echo "Backup completed, see the result in $backup_log"
du $backup_path/*$date* -sh | awk '{print "file:" $2 ", size:" $1}'

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Implementing batch processing of MySQL automatic backup under Windows (copying directory or mysqldump backup)
  • MySQL data migration using MySQLdump command
  • Detailed explanation of Linux mysqldump exporting database, data, and table structure
  • Detailed discussion on the issue of mysqldump data export
  • A brief discussion on how to use mysqldump (MySQL database backup and recovery)
  • 8 ways to manually and automatically backup your MySQL database
  • Linux implements automatic and scheduled backup of MySQL database every day
  • Detailed explanation of several methods of MySQL automatic backup and recovery (graphic tutorial)
  • How to set up automatic daily backup of mysql in CentOS system
  • Writing daily automatic backup of MySQL database using mysqldump in Centos7

<<:  vue-router hook function implements routing guard

>>:  How to use docker+devpi to build local pypi source

Recommend

Solve the problem of resetting the Mysql root user account password

Problem description: The following error message ...

MySQL installation tutorial under Windows with pictures and text

MySQL installation instructions MySQL is a relati...

A simple example of mysql searching for data within N kilometers

According to the coefficient of pi and the radius...

How to forget the password of Jenkins in Linux

1.Jenkins installation steps: https://www.jb51.ne...

Docker Machine in-depth explanation

Differences between Docker and Docker Machine Doc...

Tips for implementing multiple borders in CSS

1. Multiple borders[1] Background: box-shadow, ou...

Linux disk space release problem summary

The /partition utilization of a server in IDC is ...

Detailed explanation of the difference between CSS link and @import

How to add css in html? There are three ways to s...

Tutorial on installing and configuring MySql5.7 in Alibaba Cloud ECS centos6.8

The default MySQL version under the Alibaba Cloud...

How to use Lottie animation in React Native project

Lottie is an open source animation library for iO...

Summary of HTML formatting standards for web-based email content

1. Page requirements 1) Use standard headers and ...

Use Javascript to develop sliding-nav navigation plug-in with sliding bar effect

Table of contents 1. Introduction 2. Usage 3. Dev...

Vue3 encapsulates its own paging component

This article example shares the specific code of ...