MySQL Database backup script

Source: Internet
Author: User
Tags mkdir mysql backup

Back up the MySQL database script.

Manually creating a directory/var/backups/mysqldata can also be customized, just modify the value of the variable Backup_dir.

#!/bin/bash#mysql Backup Script database=test001mysql_user=rootmysql_password=123.combackup_dir=/var/backups/ Mysqldataname= ' Date ' +%y_%m_%d "' for I in $database do [-D ${backup_dir}/${i}] | | mkdir ${backup_dir}/${i}/mysqldump-u$mysql_user-p$mysql_password--database $i > ${backup_dir}/${i}/${i}_${name} . sql file_n= ' ls ${backup_dir}/${i}/| Wc-l ' rm_file= ' ls-l ${backup_dir}/${i}/| head-2 | Tail-1 | awk ' {print $9} ' if [$file _n-gt 3] then rm-f ${backup_dir}/${i}/$rm _filefi Done


#!/bin/bash

#mysql备份脚本

database=test001 #定义要备份的数据库, separated by spaces

Mysql_user=root #定义备份数据库使用的用户

Mysql_password=123.com #定义用户密码

Backup_dir=/var/backups/mysqldata #定义备份到的目录

Name= ' Date ' +%y_%m_%d "' #定义备份名称后缀, here with the suffix of time


For I in $database

Do

[-D ${backup_dir}/${i}] | | mkdir ${backup_dir}/${i}/

#判断是否有数据库同名目录, if not, create a

Mysqldump-u$mysql_user-p$mysql_password--database $i > ${backup_dir}/${i}/${i}_${name}.sql

#使用mysqldump对数据库进行备份

file_n= ' ls ${backup_dir}/${i}/| Wc-l '

#查看一个备份目录下文件个数

Rm_file= ' Ls-l ${backup_dir}/${i}/| head-2 | Tail-1 | awk ' {print $9} '

#查看备份目录下第一个文件名, I'm looking at the second line here, because with Ls-l, the first line shows the directory details

If [$file _N-GT 30]

#判断如果一个备份目录中的备份文件数是否大于30个

Then

Rm-f ${backup_dir}/${i}/$rm _file

#如果文件数大于30个的话, deleting the first file can be understood as keeping a backup file for up to 30 days

Fi

Done


Write a scheduled task in Crontab and execute the script once a day.

This article is from "Simon bragging" blog, please be sure to keep this source http://taixin.blog.51cto.com/11725086/1828377

MySQL Database backup script

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.