An automatic Mysql backup script

Source: Internet
Author: User
Tags mysql backup

I wrote a Mysql backup script. Let's see what's wrong.

This is the first Shell script I wrote,

I can send it to anyone who needs it.

You can put this script into crontab and execute it once every morning to automatically back up data.

This script can be executed only once a day, and only the backups of the last five days are retained on the server.

Code:


#! /Bin/bash
# This is a ShellScript For Auto DB Backup
# Powered by aspbiz
#2004-09

# Setting
# Set the database name, database login name, password, backup path, Log Path, data file location, and backup mode
# By default, the backup method is tar, mysqldump, or mysqldotcopy.
# By default, log on to the mysql database with root (empty) and back up to/root/dbxxxxx. tgz
DBName = mysql
DBUser = root
DBPasswd =
BackupPath =/root/
LogFile =/root/db. log
DBPath =/var/lib/mysql/
# BackupMethod = mysqldump
# BackupMethod = mysqlhotcopy
# BackupMethod = tar
# Setting End


NewFile = "$ BackupPath" db $ (date + % y % m % d). tgz
DumpFile = "$ BackupPath" db $ (date + % y % m % d)
OldFile = "$ BackupPath" db $ (date + % y % m % d -- date = 5 days ago). tgz

Echo "-----------------------------------------"> $ LogFile
Echo $ (date + "% y-% m-% d % H: % M: % S") >>$ LogFile
Echo "--------------------------"> $ LogFile
# Delete Old File
If [-f $ OldFile]
Then
Rm-f $ OldFile >>$ LogFile 2> & 1
Echo "[$ OldFile] Delete Old File Success! ">>> $ LogFile
Else
Echo "[$ OldFile] No Old Backup File! ">>> $ LogFile
Fi

If [-f $ NewFile]
Then
Echo "[$ NewFile] The Backup File is exists, Cant Backup! ">>> $ LogFile
Else
Case $ BackupMethod in
Mysqldump)
If [-z $ DBPasswd]
Then
Mysqldump-u $ DBUser -- opt $ DBName> $ DumpFile
Else
Mysqldump-u $ DBUser-p $ DBPasswd -- opt $ DBName> $ DumpFile
Fi
Tar czvf $ NewFile $ DumpFile >>$ LogFile 2> & 1
Echo "[$ NewFile] Backup Success! ">>> $ LogFile
Rm-rf $ DumpFile
;;
Mysqlhotcopy)
Rm-rf $ DumpFile
Mkdir $ DumpFile
If [-z $ DBPasswd]
Then
Mysqlhotcopy-u $ DBUser $ DBName $ DumpFile >>$ LogFile 2> & 1
Else
Mysqlhotcopy-u $ DBUser-p $ DBPasswd $ DBName $ DumpFile >>$ LogFile 2> & 1
Fi
Tar czvf $ NewFile $ DumpFile >>$ LogFile 2> & 1
Echo "[$ NewFile] Backup Success! ">>> $ LogFile
Rm-rf $ DumpFile
;;
*)
/Etc/init. d/mysqld stop>/dev/null 2> & 1
Tar czvf $ NewFile $ DBPath $ DBName >>$ LogFile 2> & 1
/Etc/init. d/mysqld start>/dev/null 2> & 1
Echo "[$ NewFile] Backup Success! ">>> $ LogFile
;;
Esac
Fi

Echo "-----------------------------------------"> $ LogFile





_________________
Do
{
Goodgoodstudy ();
Daydayup ();
}
While (! Died)

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.