Automatically backs up MySQL Data and uploads it to the FTP shell script

Source: Internet
Author: User
Tags mysql login

Linux automatically backs up the MySQL database and uploads the shell script to the FTP server.
#! /Bin/bash
# Mysql autobackup shell
#
#
# -------------- Set the mysql login parameters
Dbuser = root
Dbpasswd =
Dbserver = localhost
Dbname = fwserver2008
Dbopt = -- opt
Backupdir =/dbbackup/
# --------------- Set the FTP paramters, 0 no send to a ftp server, 1 send to a ftp server
Copytoftp = 1
Ftpserver = 172.16.25.2
Ftpuser = linux
Ftppasswd = 123456
# -------------- Set the backpfile paramters
Fileprefix = fwserver
Dumpfilename = $ backupdir $ fileprefix 'date + % F _ % H % M % s'. SQL
Newfile = $ fileprefix-'date must have f_1_h1_m1_s'.tar.gz
Keepdays = 10
# ---------------- Write the operater command to log file
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-I-n <end_ftp
Open $ ftpserver
User $ ftpuser $ ftppasswd
LCD $ backupdir
Hash
Prompt
Put $ newfile
Close
Bye
End_ftp
Fi
Fi
Echo "End ============================================ = "> $ logfile
Fi
# The end of the auto 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.