MySQL backup script in Linux

Source: Internet
Author: User
Tags mysql backup

MySQL backup script in Linux:

#! /Bin/bash

# Mysql autobackup shell # write by tuspark.cn

# ------------------- User name and password related to the database, database name to be backed up, backup directory, etc.

Dbuser = root

Dbpasswd = XXXXX

Dbserver = localhost

Dbname = XXXXX

Dbopt = -- opt

Backupdir =/dcbackup/

# ------------------- Whether to enable FTP remote backup. The value 0 indicates no, and the value 1 indicates yes.

Copytoftp = 1

Ftpserver = XXXXX

Ftpuser = XXXXX

Ftppasswd = XXXXX

# ------------------- The following parameters

Fileprefix = dcradiusdump

Filename = $ backupdir 'date + % F'. SQL

Newfile = $ fileprefix-'date already created f'.tar.gz

Keepdays = 10

# ------------------- The following are the logs for backup.
Logfile =/var/log/mysqlbackup. log
Logtmp =/var/log/mybackup. tmp
#===================================================== ==========
If [! -D $ backupdir]
Then
Echo "$ backupdir is not exist, then make..."> $ logfile
Mkdir-p $ backupdir
Fi
Echo "start ===========================================> ">>> $ logfile
Echo "Beginning backup 'date' + % F % t'"> $ logfile
Echo "Delete $ keepdays days ago files..."> $ logfile
Find $ backupdir-name $ fileprefix *-mtime + $ keepdays-fls $ logtmp-exec rm {}\;
Echo "Deleted Backup file is:" >>$ logfile
Cat $ logtmp> $ logfile
Echo "Delete old file Success! ">>> $ Logfile
If [-f $ backupdir $ newfile]
Then
Echo "$ newfile backup exist, backup stop..."> $ logfile
Else
If [-z $ dbpasswd]
Then
Mysqldump-u $ dbuser-h $ dbserver $ dbopt $ dbname> $ dumpfilename
Else
Mysqldump-u $ dbuser-p $ dbpasswd-h $ dbserver $ dbopt $ dbname> $ dumpfilename
Fi
Tar czvf $ backupdir $ newfile $ dumpfilename >>$ logfile 2> & 1
Echo "$ backupdir $ newfile Backup Success! ">>> $ Logfile
Rm-fr $ dumpfilename
If [$ copytoftp = 1]; then
If [-z $ ftpserver]; then
Echo "Ftp Server not set, Copy to Ftp Failed..."> $ logfile
Exit 1
Elif [-z $ ftpuser]; then
Echo "Ftp user not set, Copy to Ftp Failed..."> $ logfile
Exit 2
Elif [-z $ ftppasswd]; then
Echo "Ftp password not set, Copy to Ftp Failed..."> $ logfile
Exit 3
Else
Echo "Start copy to Ftp server..."> $ logfile
Ftp-n> $ logfile
Fi

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.