Share a MySQL sub-library backup script (original)
Development ideas:
1. Path: Specify the location of the backup, the path (the first to determine whether there is no creation of a directory) first defined, my path:/mysql/backup, each backup with compression to improve efficiency, with time convenient to organize
2. Fetch the database: Fetch the database name, I use awk and grep to take the name of the database (if you want to follow the table backup can be refined again) note to use the MYSQL-E option to make a script
3. System environment variables: Because of the use of functions, non-system-built commands are best used in scripts. /etc/profile the current environment variables of the system.
4. Implementation method: Using mysqldump command +for Loop to realize the sub-library backup
5. Backup check: If the backup file has a size of not 0 is determined to be successful, return success, otherwise failed, if placed in the scheduled task execution recommendations output to the log file, convenient for later viewing
6. Note the difference between the single quote ' and the double quotation mark ' when defining a variable, or there will be an error
1 #!/bin/bash 2 #define VAR 3 user= "root" 4 pass= "1314520" 5 path= "/mysql/backup" 6 cmd= "Mysql-u${user}-p${pass}" 7 dump = "Mysqldump-u${user}-p${pass}-B--events-x--master-data=2" 8 #system function 9. /etc/init.d/functions10. /etc/profile11 #judge dir12 function Jdir () {if [!-e $path];then14 mkdir $path-p15 fi16}17 #dump database18 Fun Ction BK () {dbname in ' $cmd-e ' show databases; ' | awk ' Nr>1{print $} ' |grep-v "Performance_schema" ' Do21 $dump $dbname |gzip >${path}/${dbname}_$ (date +%f). Sql.gz22 Sleep 123 If [-S ${path}/${dbname}_$ (date +%f). sql.gz];then24 Action "dump $dbname success!"/bin /true25 else26 action "dump $dbname failed"/bin/false27 fi28 Done
}30 function Main () {Jdir32 bk33}34 main
Want to share with everyone is the development of ideas, technology is very basic.
After testing can be done, if there are errors, please correct.
PS attached script for table backup for reference
1 #!/bin/bash 2 #define VAR 3 user= "root" 4 pass= "1314520" 5 path= "/mysql/backup" 6 cmd= "Mysql-u${user}-p${pass}" 7 dump = "Mysqldump-u${user}-p${pass}--events-x--master-data=2" 8 #system function 9. /etc/init.d/functions10. /etc/profile11 #judge dir12 function Jdir () {if [!-e $path];then14 mkdir $path-p15 fi16}17 #dump database18 Fun Ction BK () {dbname in ' $cmd-e ' show databases; ' | awk ' Nr>1{print $} ' |grep-v "Performance_schema" ' Do21 for tname in ' $cmd-e "show tables from ${dbname}" |sed "1 D "' do23 $dump $dbname $tname |gzip >${path}/${dbname}_${tname}_$ (date +%f). sql.gz24 If [-E $ {path}/${dbname}_${tname}_$ (date +%f). sql.gz];then26 echo "${dbname}_${tname}" >> $path/mysql_ Table.log27 fi29 done30 done31}32 function Main () {$ jdir34 bk35}36 Main
Share a MySQL sub-database sub-table backup script (original)