MySQL Automatic backup script

Source: Internet
Author: User
Tags mysql automatic backup

#!/bin/bash

#auto backup MySQL Shell

#by Csy 2015-11-11

#自动备份数据库, and add a task plan script

#定义变量 ===============

Bak_cmd=/usr/bin/mysqldump

Bak_host=localhost

Bak_db=csy

Bak_user=root

Bak_pass= "123456"

bak_dir=/tmp/mysqlbackup/

Bak_date= ' DATE +%f '


#环境测试 .....

If [$UID-ne 0];then

ECHO-E "\033[32m must use the root user to execute this script. \033[0m"

Exit

Fi


if [!-D $BAK _dir];then

Mkdir-p $BAK _dir

Echo-e "\033[35m $BAK _dir This directory was created successfully!!! \033[1m "

Else

ECHO-E "\033[35m$bak_dir This directory already exists ... \033[0m"

Fi


#正式备份 .....

$BAK _cmd-h$bak_host-u$bak_user-p$bak_pass-d $BAK _db > $BAK _dir/csy_$bak_date.sql

If [$?-eq 0];then

ECHO-E "\033[34m database backup has been completed .... \033[1m"

echo "Backup directory: $BAK _dir"

Else

ECHO-E "\033[34m Data backup error, please check .... \033[0m"

Fi


#自动删除30天以前的备份数据

Find $BAK _dir/-mtime +30 |xargs rm-rf {} \;

If [$?-eq 0];then

Echo-e "\033[37m Delete 30 days ago Backup data Complete ... \033[1m"

Else

Echo-e "\033[37m command execution error or directory not found, please check \033[0m"

Fi


#创建自动备份计划任务

grep "Back_mysql"/var/spool/cron/root >>/dev/null

If [$?-ne 0];then

echo "* * * 5/root/shell/back_mysql >/tmp/mysql.log 2>&1" >>/var/spool/cron/root

Fi


This article is from "Life is insight, not live" blog, please be sure to keep this source http://linuxshengyu.blog.51cto.com/8810066/1711912

MySQL Automatic 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.