Linux shell script--mysql Full Schedule Task

Source: Internet
Author: User
Tags gz file

Linux full-schedule task for MySQL _shell small script for MySQL with smaller data size.


#!/bin/bash


Port= ' 5001 '

User= ' admin '

Passwd= ' admin '

Socked= "/data/socket/mysql${port}.sock"

backup_file= ' /data/mysql/backup ' #注意磁盘空间情况

backup_save_days= ' 2 '#删除N天之前的


Cmd1= ' show databases; '

Time= ' Date +%y%m%d_%h%m%s '

Every_day_backup_dir= "${backup_file}/${time}_backup"

Db_list= "${every_day_backup_dir}/db_list.txt" #数据库名字存放文件


# function:check_db ===> Db_list.txt

check_db ()

{

MKDIR-PV ${every_day_backup_dir} >>/dev/null

Touch ${db_list} && chmod 755 ${db_list} && chown-r mysql.mysql ${db_list}

/usr/local/mysql/bin/mysql-u${user}-p${passwd}-S ${socked}-nbe "${cmd1}" |grep-v ' Information_schema\|performance_ Schema\|mysql ' >${db_list}

}


# function:backup_db ===>xx.sql

backup_db ()

{

/usr/local/mysql/bin/mysqldump-u${user}-p${passwd}-S ${socked} $ >> ${every_day_backup_dir}/$1.sql

}


# Function:tar_file ===>xx.tar.gz

Tar_file ()

{

CD ${backup_file}

TAR-CF ' Date +%y%m%d_%h%m%s '. tar.gz ${time}_backup

RM-FR ${time}_backup

}


# function:delete xx.tar.gz: <before n days>

Delete_file ()

{

CD ${backup_file}

/usr/bin/find-name "*.tar.gz"-mtime +${backup_save_days}-exec rm {} \;

}


# execute_main_process

# 1

check_db


# 2

Cat ${db_list}| While Read line

Do

OLDIFS=${IFS}

backup_db ${line}

IFS=${OLDIFS}

Sleep 20

Done


# 3

Tar_file


# 4

#delete Xx.tar.gz:before N Days


Delete_file ${backup_save_days}


#----End


Attention:

The 1.mysql connection method is to specify the socket file, which can be flexibly changed.

2. Back up each library into a single SQL file, and then compress it all into a x.tar.gz file. Production of one every day.

3. You can change the script appropriately to back up a single library, a single table.


Crontal Scheduled tasks: Daily 1 o'clock in the morning backup

0 1 * * */bin/bash/data/mysql/backup/backup.sh


This article is from the "Breeze" blog, please be sure to keep this source http://11805203.blog.51cto.com/11795203/1828901

Linux shell script--mysql Full Schedule Task

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.