This script implements the following functions: 1. Backing up mysql databases online; 2. Saving backup logs to log files; 3. compressing data backup files; 4. Remotely ftp the compressed file to the backup server; 5. deleting data that has been backed up locally for more than seven days is mainly to prevent the abnormal growth of the disk space of the production server from leading to performance bottlenecks. The specific implementation of shell is as follows :#! /Bin/sh
#---------------------------------------------------------
# Scriptname: erpbackup. sh
# Version: 1.0 for linux
# Method: backup mysqldb
# Author: fengzhanhai
# History: create program V1.1 20110429 by Fengzhanhai
UserName = 'root'
AdminPwd = 'passw0rd'
DbName = erp
BakDir =/home/imu/db-backup/vpnback
LogFile =/home/imu/db-backup/vpnback/mysqlbak. log
DATE = 'date + % Y % m % d % H % m'
DumpFile = vpndata $ DATE. SQL
GZDumpFile = $ DumpFile. tgz
FtpServer = 10.19.98.235
FtpTargetPath =/erp_backup/mysql # main ---------------------------------------------------------
Echo $ (date + "% y-% m-% d % H: % M: % S") backup begin >>$ LogFile
Cd $ BakDir # Back Up Files of the current day
/Usr/local/mysql/bin/mysqldump-u $ UserName-p $ AdminPwd $ DbName> $ DumpFile
Echo $ (date + "% y-% m-% d % H: % M: % S") Dump Done >>$ LogFile # tar compressed file
Tar czvf $ GZDumpFile $ DumpFile> $ LogFile
# Rm-f $ DumpFile
Echo $ (date + "% y-% m-% d % H: % M: % S") $ GZDumpFile compress done >>$ LogFile # upload to the ftp server
Ftp-v-n $ FtpServer <END
User databack passw0rd
Bin
Cd $ FtpTargetPath
Put $ GZDumpFile
Bye
ENDecho $ (date + "% y-% m-% d % H: % M: % S") ftp $ GZDumpFile compress done >>$ LogFilerm-f $ GZDumpFile
# Delete the file backup files in the current directory 7 days ago
Find $ BakDir-name "vpndata *"-mtime + 7-exec rm {}\;
Echo $ (date + "% y-% m-% d % H: % M: % S") delete old file done >>$ LogFile # Verification Result
Ls-al $ DumpFile> $ LogFile