| #!/bin/bash # Check Host If [! $];then Echo ' Usage:mysql_data_backup [HostID] dbname ' Exit Else If [$1-eq 110]; Then db_host=192.168.1.110 Db_user=xxxx Db_passwd=xxx elif [$1-eq 112]; Then db_host=192.168.1.112 Db_user=xxx Db_passwd=xxx Else Echo ' Invalid Host ID ' Exit Fi Fi # Check Database name If [! $];then Echo ' Usage:mysql_data_backup HostID [dbname] ' Exit Else Echo ' Backup begin ' Fi Begin_time= ' Date ' +%s ' Db_name=$2 # Get Table Info Tables_content= ' mysqlshow-u $db _user-p$db_passwd-h $db _host $db _name | Sed ' s/|//g ' | Sed ' s///g ' | Sed ' 1,4d ' | Sed ' $d ' # Check Content if emptyed tables_content_check= ' echo-n $tables _content >/tmp/mysql_backup_check.tmp ' if [!-s/tmp/mysql_backup_check.tmp];then Echo ' Backup stop ' Exit Fi # Plan Backup of SQL tables Tables= (${tables_content}) # Get tables Length tables_len=${#tables [*]} # backup file (TAR) number Backupfilenum=3 # Backup of directory do not bring/ Backupdir= "/backup1/sqldata/$db _name" # Backup of directory for date datestamp=$ (date "+%y%m%d") # log file Logfile= '/backup1/sqldata/backup.log ' # Final Backup Directroy Filedir= "$backupDir/$datestamp" # Auto Create target directory if [!-D $backupDir];then mkdir $backupDir Fi if [!-D $fileDir];then mkdir $fileDir Fi if [!-D $logfile];then Touch $logfile Fi echo "Total $tables _len tables." For ((i=0;i< $tables _len;i++)) Todo Tablename= "${tables[$i]}" Filename= "${tables[$i]}.sql" Filepath= "$fileDir/$fileName" echo "Table [${tables[$i]}] Backup ..." Mysqldump-e-H $db _host-u $db _user-p$db_passwd $db _name $tableName > $filePath Done # tar Files CD $backupDir Tar cvf ${datestamp}.tar./${datestamp} # Delete Source Archive Rm-rf./${datestamp} End_time= ' Date ' +%s ' Total_time=$[end_time-begin_time] Echo ' Backup done ' echo "Total time: ${total_time} second" Now_date= ' Date ' +%y-%m-%d%k:%m:%s ' # Save Log echo "${now_date}: Backup Database [${db_name}]: Total time [${total_time}s]" >> $logfile # Begin clean excess of backup file # count Backup dir name of length; Backupdir_length= ' expr length ' $backupDir ' # Get tar file list Backupfile_list= ' Find $backupDir-name "*.tar" | Sort-n-r-k 1. $backupDir _length ' # set Array Backupfiles= (${backupfile_list}) backupfile_length=${#backupFiles [*]} If [$backupFile _length-gt $backupFileNum];then for (i=$ backupfile_length;i> $backupFileNum i--)) do filename= "${backupfiles[$i-1]}" RM-RF $fileName #save Log echo "File: $fileName deleted " echo" File: $fileName deleted ">> $logfile done Else echo "backup file number normal." Fi |