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

A brief discussion on MySQL large table optimization solution

background The amount of new data in the business...

JS, CSS and HTML to implement the registration page

A registration page template implemented with HTM...

Solution to mysql failure to start due to insufficient disk space in ubuntu

Preface Recently, I added two fields to a table i...

Why is the disk space still occupied after deleting table data in MySQL?

Table of contents 1. Mysql data structure 2. The ...

jQuery plugin to implement search history

A jQuery plugin every day - to make search histor...

Vue uses three methods to refresh the page

When we are writing projects, we often encounter ...

The difference between MySQL database host 127.0.0.1 and localhost

Many of my friends may encounter a problem and do...

JavaScript implements simple date effects

The specific code of JavaScript date effects is f...

MYSQL slow query and log example explanation

1. Introduction By enabling the slow query log, M...

Implementation of dynamic rem for mobile layout

Dynamic rem 1. First, let’s introduce the current...

MySQL 5.7.18 zip version installation tutorial

The mysql 5.7.18 zip version of MySQL is not like...

JavaScript canvas to achieve code rain effect

This article shares the specific code for canvas ...

Detailed deployment of Alibaba Cloud Server (graphic tutorial)

I have recently learned web development front-end...