MySQL Sub-Library backup

Source: Internet
Author: User

1. Requirements overview
    • Back up MySQL database data 00:00 every day;
    • Each library generates a file, using gzip compression, file name: Backup_ Library Name _yyyymmdd. sql.gz, note that YYYYMMDD need to be the day before;
    • Save the backup file under/backup/mysql/, create a new daily backup data directory YYYYMMDD, note the day before
    • The log is saved under/backup/mysql/and is named:yyyymmdd. Log

2. Environment Preparation

Operating system: CentOS release 6.5
MySQL data: MySQL 5.1.73

3. Script description
    • Determine if the backup directory exists and create it if it does not exist;
    • Use the MySQL command to get a list of all databases;
    • Traverse all data lists and use the mysqldump command to back up

4. Script writing
1#!/bin/Bash2 3#log file name4log=/Backup/mysql/$ (Date+%y%m%d-d -1day).Log5 6# CreateBackupDirectory7backup_dir=/Backup/mysql/$ (Date+%y%m%d-d -1day)8 9 if[!-D "${backup_dir}"]; ThenTen         Echo"Create backup Directory ${backup_dir}" >> $LOG One         mkdir${Backup_dir} A fi -  -  the#BackupDB data -  forI in ' mysql-uroot-p123456-e ' show databases;` -  Do -  +         if["$i"! = "Database"]; Then -                 Echo"Start Backup $i" >> $LOG +Mysqldump-uroot-p123456-b "$i"--flush-privileges--single-transaction--master-data=2--flush-logs--triggers-- Routines--events--hex-blob | gzip > ${backup_dir}/backup_ "$i" _$ (Date+%y%m%d-d -1day). sql.GZ A                 Echo"End Backup $i" >> $LOG at fi -  -  Done -  - Echo"End Backup" >> $LOG

5. Scheduled Tasks
1 [[email protected] mysql]# crontab-e200 * * */bin/sh /root/ Scripts/db_backup. sh >/dev/null2>&1

The output needs to be redirected to an empty

6. Key Commands

The day before YYYYMMDD

Date +%y%m%d-d -1day

Gzip

Cat Install gzip install. log.gz

MySQL command queries all libraries

" show databases; "

mysqldump command to back up the database

Mysqldump-uroot-p123456-b test--flush-privileges--single-transaction--master-data=2gzip >/backup/mysql/20180515/backup_test_20180515.sql.gz

MySQL Sub-Library backup

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.