131 MySQL Automatic backup scripts

Source: Internet
Author: User
Tags mysql automatic backup mysql backup

Wrote a MySQL backup script, everybody look, what's wrong with the place.

This is the first shell script I wrote, let's see,

I can send it to someone who needs it.

You can put this script in crontab, do it every morning, and automatically back up

This script executes at most one time per day and retains only the last five days of backup on the server.

Code:


#!/bin/bash
#This is a shellscript for Auto DB Backup
#Powered by Aspbiz
#2004-09

#Setting
#设置数据库名, database login name, password, backup path, log path, data file location, and backup mode
#默认情况下备份方式是tar, it could be mysqldump,mysqldotcopy.
#默认情况下, log in 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)

Transfer from http://www.2cto.com/database/200410/1079.html

131 MySQL Automatic backup scripts

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.