Linux automatically backs up MySQL data and uploads the shell script _linux shell on FTP

Source: Internet
Author: User
Tags mysql login
Copy Code code as follows:

#!/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 an 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 +%f_%h%m%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 isn't 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.