MYSL full + incremental Automatic backup script

Source: Internet
Author: User

1. Write the full-scale backup script code as follows: # vim /root/dbfullybak.sh //Add the following #!/bin/bash# program#     use mysqldump to fully backup mysql data per week!# history #    2015.08.25# path#     Bakdir=/home/mysql/backuplogfile=/home/mysql/backup/bak.logdate= ' date +%y%m%d ' Begin= ' date + "%Y year%m month% D-Day  %h:%m:%s "' dumpfile= $Date. sqlgzdumpfile= $Date .sql.tgzcd  $BakDir/usr/local/mysql/bin/mysqldump  -uroot -p123456 --quick --all-databases --flush-logs --delete-master-logs  --single-transaction >  $DumpFile/bin/tar czvf  $GZDumpFile   $DumpFile/bin/rm $ dumpfileecho  start: $Begin   End: $Last   $GZDumpFile  succ >>  $LOGFILECD  $ Bakdir/dailyrm -f *2. Write the incremental backup script code as follows: # cat /root/dbdailybak.sh //content is under #!/bin/bash#  Program#    use cp to backup mysql data everyday!# history#    2015.08.25# path#      bakdir=/home/mysql/backup/dailybindir=/home/mysql/datalogfile=/home/mysql/backup/bak.logbinfile=/home/mysql/ data/mysql-bin.index/usr/local/mysql/bin/mysqladmin -uroot -p123456 flush-logs# This is used to generate a new mysql-bin.00000* file counter= ' wc -l  $BinFile  |awk  ' {print $1} ' nextnum=0# This for loop is used to compare $counter, $NextNum these two values to determine whether the file is present or up to date. for file in   ' cat  $BinFile ' do        base= ' basename  $file '          #basename用于截取mysql-bin.00000* file name, remove. Mysql-bin.000005 in front of the./        nextnum= ' expr  $NextNum  + 1 '         if [  $NextNum  -eq  $Counter  ]         then                echo  $base  skip!  >> $ logfile        else                 dest= $BakDir/$base                  if (test -e  $dest)                   #test  -e is used to detect if the target file exists, and it is written exist! to $ LogFile go.                 then                          echo   $base  exist! >>  $LogFile                  else                         cp  $BinDir /$base   $BakDir                          echo  $base  copying >>  $LogFile                 fi         fidoneecho  ' date +%y year%M month%d  %h:%m:%s '   $Next   bakup succ! >>  $LogFile 3. Set up the crontab task and execute the backup script code as follows: # crontab -l //content is down # Perform a full backup script every Sunday 3:00 0 3 * * 0 /root/dbfullybak.sh >/dev/null 2>&1# Monday to Saturday 3:00 make an incremental backup 0 3 * * 1-6 /root/dbdailybak.sh >/dev/null 2>& 1 Appendix: Sh -n /root/dbfullybak.sh can be used to detect if the shell syntax is correct


This article from "Painting" blog, reproduced please contact the author!

MYSL full + incremental Automatic backup script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.