MySQL backup script

Source: Internet
Author: User
Tags mysql backup

Touch mysql_backup.sh

chmod +x mysql_backup.sh

Vim mysql_backup.sh

#!/bin/bash

Id= "Root"

Pwd= "123456"

dbs= "Test Rockoa"

Backuppath= "/root/mysqlbackup" backup path self-setting

Day=15

[!-D $backpath] &&mkdir-p $backuppath

CD $backuppath

backupname=mysql_$ (Date +%y-%m-%d)

for DB in $dbs;

Do

Mysqldump-u$id-p$pwd-s/var/lib/mysql/mysql.sock $db > $backupname _$db.sql

If ["$?" = = "0"]

Then

echo $ (date +%y-%m-%d) "$db mysqldump sucess" >>mysql.log

Else

echo $ (date +%y-%m-%d) "$db MySQL dump failed" >>mysql.log

Exit 0

Fi

Done

Tar-czf $backupname. tar.gz *.sql

If ["$?" = = "0"]

Then

echo $ (date +%y-%m-%d) "Tar sucess" >>mysql.log

Else     

echo $ (date +%y-%m-%d) "Tar failed" >>mysql.log

Exit 0

Fi

Rm-f *.sql

delname=mysql_$ (date-d "$day day Ago" +%y-%m-%d). tar.gz

Rm-f $delname


SH mysql_backup.sh

ll/root/mysqlbackup/

Total 56

-rw-r--r--1 root root 52346 Sep 16:44 mysql_2016-09-24.tar.gz

-rw-r--r--1 root root 184 Sep 16:44 Mysql.log


cd/root/mysqlbackup/

Tar zxvf mysql_2016-09-24.tar.gz

Rockoa.sql

Test.sql


Vim/etc/crontab

5 9-6 * * 1-7 root/root/mysql_backup.sh

/etc/rc.d/init.d/crond restart

This article from "Mailfile" blog, declined reprint!

MySQL backup script

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.