Linux automatically backs up the MySQL database and uploads the shell script to the FTP server #! /Bin/bash # Mysqlautobackupshell ### -------------- setthemysqlloginparametersdbuserrootdbpasswddbserverlocalhostdbnamefwserve
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 < 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