MySQL Backup Script Using Information_schema

Developed In: bash — Contributed by: Somasundaram

This script will take the backup of mysql databases.Additionally the script will compress the backup using gzip and also capture the backup time. You need to create the following table for capturing the backup time.

CREATE TABLE `mysql`.`backupinfo` ( `idback` int(10) unsigned NOT NULL AUTO_INCREMENT, `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `end_time` datetime DEFAULT NULL, `size` bigint(20) unsigned DEFAULT NULL, `type` enum('TEXT','RAW') DEFAULT NULL, PRIMARY KEY (`idback`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Use comments are welcome.


Somasundaram
bash
  1. #!/bin/bash
  2.  
  3. bkDate=`date +%d%m%Y` #Backup Date
  4.  
  5. bkFolder="/home/soma/$bkDate/" #Backup destination and its your choice.
  6.  
  7. mkdir $bkFolder #Create a directory for that day's backup
  8.  
  9. cp /etc/my.cnf $bkFolder #Copy the my.cnf file
  10.  
  11. #Flushing the logs and capturing the backup start time.
  12. #Capturing backup time is just for information.
  13. #Since this is a textual backup i am storing the backup type as text.
  14.  
  15. #ToDO: Need to copy the relavent binary logs.
  16.  
  17. mysql -pBackUp@123 -u backup -e "FLUSH LOGS;INSERT INTO mysql.backupinfo ( type ) VALUES ( 'TEXT' );"
  18.  
  19. #Taking backup of all databases other that mysql and information_schema.
  20.  
  21. for db in `mysql --skip-column-names -u backup -pBackUp@123 -s -e "SELECT schema_name FROM information_schema.SCHEMATA WHERE schema_name NOT IN ( 'mysql','information_schema')"`
  22.  
  23. do
  24. mkdir $bkFolder$db; #Creating a new directory for each database.
  25. mysqldump -d --triggers -u backup -R -E -pBackUp@123 -q --databases $db | gzip > $bkFolder$db/$db.sql.gz #I am using gzip to compress the backup.
  26.  
  27. done
  28.  
  29. #Finally I am storing the end time and size of the backup.
  30.  
  31. mysql -u backup -e "UPDATE mysql.backupinfo SET end_time = NOW(), size = TRIM(REPLACE(REPLACE('`du -s`',' ',''),'.','')) WHERE type = 'TEXT' AND DATE(start_time) = DATE(NOW())" -pBackUp@123
  32.  

Current Tags

backup 

You must be logged in to tag this tool

No Comments yet

Votes

Not yet rated.
You must be logged in to vote.

Watches

0 members are watching this tool
You must be logged in to track this tool.

Provide Feedback

Please note:
HTML will be purified, but we allow for a number of HTML tags so that you have the flexibility to decorate your comment text to some extent. The comments allow the following HTML tags:

strong, b, em, blockquote, a, code, pre

To put code into your comment, simply encapsulate your code with
[code language="XXX"][/code], where XXX is any common language, for instance "PHP", "SQL", "C", etc.



You must be logged in to comment