Advertisement
zentavr

MySQL incremental backup/restore script

Oct 20th, 2013
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 19.34 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Author: Andrey Miroshnichenko <zentavr.trafford.com.ua>
  4. # Created for: Silicom Internet SL                   2012, 2013
  5. #              ICSW Group: http://www.icsw.com/en/
  6. # Created for: Trafford Group                        2012, 2013
  7. # Created for: Localcircles India                    2013
  8. #
  9. # Introducion:
  10. # The script does incremental backup using Percona's xtrabackup utilities.
  11. # You can download them from this site:
  12. # http://www.percona.com/downloads/XtraBackup/LATEST/
  13. # Yum repository: http://www.percona.com/doc/percona-server/5.5/installation/yum_repo.html
  14. # APT repository: http://www.percona.com/doc/percona-server/5.5/installation/apt_repo.html
  15. #
  16. # FreeBSD users can find the percona xtrabackup in the ports (seems like rather old version):
  17. # [root@cassiopeia ~]# cd /usr/ports/
  18. # [root@cassiopeia /usr/ports]# make search key=xtrabackup
  19. # Port:   xtrabackup-1.1
  20. # Path:   /usr/ports/databases/xtrabackup
  21. # Info:   OpenSource version of InnoDB backup with support of Percona extensions
  22. # Maint:  aleksandr.kuzminsky@percona.com
  23. # B-deps: gettext-0.18_1 gmake-3.81_4 libiconv-1.13.1_1
  24. # R-deps:
  25. # WWW:    http://www.percona.com/docs/wiki/percona-xtrabackup:start
  26.  
  27. trap bashtrap INT
  28. function bashtrap() {
  29.     echo "CTRL+C Detected !..."
  30.     exit 1
  31. }
  32.  
  33. function usage() {
  34.     cat << EOF
  35. usage: $0 options
  36.  
  37. The script does incremental MySQL backup.
  38.  
  39. OPTIONS:
  40.     -h, --help  
  41.         Show this message
  42.    
  43.     -j, --job <job>
  44.         What job to do: "backup" or "restore"?
  45.         Default is "backup".
  46.        
  47.     -t, --type <type>
  48.         Backup type: "full" or "incremental".
  49.         Default is "full".
  50.        
  51.     -d, --database <database>
  52.         Database name
  53.    
  54.     -l, --lsndir <directory>
  55.         LSN (Log Seek Number) Directory Name.
  56.         Default is "/tmp"
  57.    
  58.     -T, --threads <threads>
  59.         How many threads to use?
  60.         Default is: "amount of CPUs + 1"
  61.    
  62.     --throttle <IOPS>
  63.         How many IOPS to produce
  64.    
  65.     --ionice-class <ionice_class>
  66.         ionice (1) class to set up during the operation (pass -c ionice_class to ionice):
  67.         0: none, 1: realtime, 2: best-effort, 3: idle
  68.    
  69.     --ionice-classdata <ionice_class>
  70.         ionice (1) classdata to set up during the operation (pass -n ionice_class to ionice):
  71.         class data (0-7, lower being higher prio)
  72.    
  73.     -L, --log <logfile>
  74.         Path to the logfile.
  75.         Default is "/tmp/logfile.log".
  76.    
  77.     -D, --path <path>
  78.         Where to store/pick the backup results.
  79.         Default is "Current Directory"
  80.    
  81.     -r, --dest <path>
  82.         Where to place the retored MySQL data
  83.         Default is "Current Directory"
  84.  
  85.     -x, --xtrabin <xtrabackup_binary>
  86.         Sets up the Xtrabackup binary
  87.    
  88.     -s, --stream <streamtype>
  89.         Sets what the stream to use: "tar" or "xbstream"
  90.         Default is "xbstream"
  91.  
  92.     -n, --do-not-delete
  93.         Do not delete the packed files during restore
  94.    
  95.     -e, --export
  96.         Do an export of InnoDB tablespaces to separate .exp files.
  97.         Only for Percona Server.
  98.         Review this: http://www.percona.com/doc/percona-xtrabackup/innobackupex/importing_exporting_tables_ibk.html
  99.         Set up this into your my.cnf:
  100.             innodb_file_per_table=1               # For any version of Percona Server
  101.             innodb_expand_import=1                # For Percona Sevrer to version 5.5.10-20.1
  102.             innodb_import_table_from_xtrabackup=1 # For Percona Sevrer from version 5.5.10-20.1 and after
  103.         Importing tables:
  104.         To import a table to other server, first create a new table with the same structure as the one that will be imported at that server:
  105.         OTHERSERVER|mysql> CREATE TABLE mytable (...) ENGINE=InnoDB;
  106.         then discard its tablespace:
  107.         OTHERSERVER|mysql> ALTER TABLE mydatabase.mytable DISCARD TABLESPACE;
  108.         After this, copy mytable.ibd and mytable.exp files to database’s home, and import its tablespace:
  109.         OTHERSERVER|mysql> ALTER TABLE mydatabase.mytable IMPORT TABLESPACE;
  110.         Once this is executed, data in the imported table will be available.
  111.        
  112.  
  113. EOF
  114. }
  115.  
  116. function checkpipe() {
  117.     STATUS=$1
  118.     SOFT1=$2
  119.     SOFT2=$3
  120.     if [ "$STATUS" != "0 0" ]; then
  121.         echo "The pipe of $SOFT1 and $SOFT2 was finished unseccussfully: $STATUS"
  122.         exit 1
  123.     else
  124.         echo "The pipe operation was completed successfully"
  125.     fi 
  126. }
  127.  
  128. function checkexit() {
  129.     STATUS=$1
  130.     SOFT=$2
  131.     if [ "$STATUS" != "0" ]; then
  132.         echo "The $SOFT was finished unseccussfully: $STATUS"
  133.         exit 1
  134.     else
  135.         echo "The $SOFT operation was completed successfully"
  136.     fi
  137. }
  138.  
  139. # Extracting the options
  140. # TODO: (Low) If this fails, parse only the short options with bash built-in getopt
  141. #       http://linuxwell.com/2011/07/14/getopt-in-bash/
  142. ARGS=$(getopt -o "hj:t:d:l:T:L:D:r:x:nes:" -l "help,job:,type:,database:,lsndir:,threads:,log:,path:,dest:,xtrabin:,do-not-delete,export,stream:,throttle:,ionice-class:,ionice-classdata:" -n "MyXtraBackup" -- "$@")
  143. if [ $? -ne 0 ]; then
  144.     echo "Bad argument to getopt was given"
  145.     exit 1
  146. fi
  147.  
  148. eval set -- "$ARGS"
  149.  
  150. #
  151. # Set up the defaults :)
  152. #
  153. # Job type: backup or restore
  154. JOB="backup"
  155.  
  156. # Backup type: "full" or "incremental"
  157. TYPE="full"
  158.  
  159. # Path to where LSN position will be stored
  160. LSNDIR="/tmp"
  161.  
  162. # The amount of the threads to use
  163. THREADS=$(( $(grep -c processor /proc/cpuinfo) + 1 ))
  164.  
  165. # Where to store the logfile
  166. LOGFILE="/tmp/logfile.log"
  167.  
  168. # Where to put the dump
  169. BACKUPRES=$(pwd)
  170.  
  171. # Where to restore the data
  172. DEST=$(pwd)
  173.  
  174. # Type of the stream
  175. STREAMTYPE="xbstream"
  176.  
  177. # Location of the utilities
  178. INNOBACKUPEX=$(which innobackupex)
  179. LZOP=$(which lzop)
  180. XBSTREAM=$(which xbstream)
  181. TAR=$(which tar)
  182. MYSQLDUMP=$(which mysqldump)
  183. MYSQL=$(which mysql)
  184. IONICE=$(which ionice)
  185.  
  186. OPTS=""
  187. EXPORT=""
  188. LZOP_OPTS="-dcfU"
  189. IONICE_CMD=""
  190. IONICE_CLASS=""
  191. IONICE_CLASSDATA=""
  192.  
  193. # Extract the options
  194. while true; do
  195.     case "$1" in
  196.         -h|--help)
  197.             usage
  198.             exit;;
  199.        
  200.         -j|--job)
  201.             if [ -n "$2" ];then
  202.                 JOB="$2"
  203.             else
  204.                 echo "No job type was specified (-j | --job)."
  205.                 exit 1;
  206.             fi
  207.             shift 2;;
  208.        
  209.         -t|--type)
  210.             if [ -n "$2" ];then
  211.                 TYPE="$2"
  212.             else
  213.                 echo "No backup type was specified (-t | --type)."
  214.                 exit 1;
  215.             fi
  216.             shift 2;;
  217.            
  218.         -d|--database)
  219.             if [ -n "$2" ];then
  220.                 DATABASE="$2"
  221.             else
  222.                 echo "No database was specified (-d|--database)."
  223.                 exit 1;
  224.             fi
  225.             shift 2;;
  226.        
  227.         -l|--lsndir)
  228.             if [ -n "$2" ];then
  229.                 LSNDIR="$2"
  230.             else
  231.                 echo "No LSN Directory was specified (-l|--lsndir)."
  232.                 exit 1;
  233.             fi
  234.             shift 2;;
  235.        
  236.         -T|--threads)
  237.             if [ -n "$2" ];then
  238.                 THREADS="$2" # TODO: (Low) Check, if the parameter is an integer?
  239.             else
  240.                 echo "No amount of the threads was specified (-T | --threads)."
  241.                 exit 1;
  242.             fi
  243.             shift 2;;
  244.        
  245.         -L|--log)
  246.             if [ -n "$2" ];then
  247.                 LOGFILE="$2" # TODO: (Low) Check, if the basename of logfile exists??
  248.             else
  249.                 echo "No logfile was given (-L | --log)."
  250.                 exit 1;
  251.             fi
  252.             shift 2;;
  253.        
  254.         -D|--path)
  255.             if [ -n "$2" ];then
  256.                 BACKUPRES="$2"
  257.             else
  258.                 echo "No directory for backup results was specified (-D | --path)."
  259.                 exit 1;
  260.             fi
  261.             shift 2;;
  262.        
  263.         -r|--dest)
  264.             if [ -n "$2" ];then
  265.                 DEST="$2"
  266.             else
  267.                 echo "No directory where to place restored data was specified (-r | --dest)."
  268.                 exit 1;
  269.             fi
  270.             shift 2;;
  271.        
  272.         -x|--xtrabin)
  273.             if [ -n "$2" ];then
  274.                 OPTS="$OPTS --ibbackup=$2"
  275.             else
  276.                 echo "No xtrabackup name was specified (-x | --xtrabin)."
  277.                 exit 1;
  278.             fi
  279.             shift 2;;
  280.        
  281.         --throttle)
  282.             if [ -n "$2" ];then
  283.                 OPTS="$OPTS --throttle=$2"
  284.             else
  285.                 echo "No throttle value was specified (--throttle)."
  286.                 exit 1;
  287.             fi
  288.             shift 2;;
  289.        
  290.         --ionice-class)
  291.             if [ -n "$2" ];then
  292.                 IONICE_CLASS="-c$2"
  293.             else
  294.                 echo "No ionice class value was specified (--ionice-class)."
  295.                 exit 1;
  296.             fi
  297.             shift 2;;
  298.        
  299.         --ionice-classdata)
  300.             if [ -n "$2" ];then
  301.                 IONICE_CLASSDATA="-n$2"
  302.             else
  303.                 echo "No ionice class data value was specified (--ionice-classdata)."
  304.                 exit 1;
  305.             fi
  306.             shift 2;;
  307.        
  308.         -s|--stream)
  309.             if [ -n "$2" ];then
  310.                 STREAMTYPE="$2"
  311.             else
  312.                 echo "No streamtype name was specified (-s | --stream)."
  313.                 exit 1;
  314.             fi
  315.             shift 2;;
  316.        
  317.         -n|--do-not-delete)
  318.             LZOP_OPTS="-dcf"
  319.             shift
  320.         ;;
  321.  
  322.         -e|--export)
  323.             EXPORT="--export"
  324.             shift
  325.         ;;
  326.  
  327.         --)
  328.             shift
  329.             break;;        
  330.     esac
  331. done
  332.  
  333. # Remove trailing slashes from the end
  334. BACKUPRES=$(echo "$BACKUPRES" | sed 's/\/*$//')
  335. LSNDIR=$(echo "$LSNDIR" | sed 's/\/*$//')
  336. DEST=$(echo "$DEST" | sed 's/\/*$//')
  337.  
  338. case "${STREAMTYPE}" in
  339.     "tar")
  340.         # Filename Suffix
  341.         echo "Using tar as a streamer"
  342.         FILENAMESUFFIX=".tar.lzo"
  343.         STREAMER_BIN=$TAR
  344.     ;;
  345.     "xbstream")
  346.         # Filename Suffix
  347.         echo "Using xbstream as a streamer"
  348.         FILENAMESUFFIX=".xbs.lzo"
  349.         STREAMER_BIN=$XBSTREAM
  350.     ;;
  351.     *)
  352.         echo "Unknown type of backup was specified: ${STREAMTYPE}"
  353.         exit 1
  354.     ;;
  355.    
  356. esac
  357.  
  358. # Determine for whether we need to use ionice(1)
  359. if [[ -n "$IONICE_CLASS" ]] || [[ -n "$IONICE_CLASSDATA" ]]; then
  360.     if [[ -n "$IONICE" ]] && [[ -x "$IONICE" ]]; then
  361.         IONICE_CMD="$IONICE $IONICE_CLASS $IONICE_CLASSDATA -- "
  362.     else
  363.         echo "No ionice(1) executable was found or it has no executable flag"
  364.     fi
  365. fi
  366.  
  367. # The names will look like this:
  368. # DATABASE-20120810_221058-full-169:12587456.xbs.lzo
  369. # DATABASE-20120811_221055-incremental-169:12599569.xbs.lzo
  370.  
  371. # What needs to be done?
  372. case "$JOB" in
  373.     "backup")
  374.         echo "Doing a backup."
  375.         # Check, if the database was specified?
  376.         if [ -z "$DATABASE" ]; then
  377.             echo "Database was not specified"
  378.             exit 1
  379.         fi
  380.        
  381.         # Checking if the destination path exists
  382.         if [ ! -d $BACKUPRES ]; then
  383.             echo "Creating $BACKUPRES"
  384.             mkdir -p "$BACKUPRES"
  385.             if [ $? -ne 0 ]; then
  386.                 echo "Creation of $BACKUPRES was failed"
  387.                 exit 1
  388.             fi
  389.         fi
  390.        
  391.         if [ -z "$INNOBACKUPEX" ]; then
  392.             echo "No innobackupex was found in $PATH"
  393.             exit 1
  394.         fi
  395.        
  396.         if [ -z "$MYSQL" ]; then
  397.             echo "No mysql was found in $PATH"
  398.             exit 1
  399.         fi
  400.  
  401.         if [ -z "$MYSQLDUMP" ]; then
  402.             echo "No mysqldump was found in $PATH"
  403.             exit 1
  404.         fi
  405.  
  406.         if [ -z "$LZOP" ]; then
  407.             echo "No lzop archiver was found in $PATH"
  408.             exit 1
  409.         fi
  410.        
  411.         # Inventing a name for the file
  412.         FILENAME=$DATABASE"-"$(date "+%Y%m%d_%H%M%S")      
  413.        
  414.         TMPNAME=$(mktemp --tmpdir="$BACKUPRES")
  415.         # What type of the backup do we have?
  416.         case "$TYPE" in
  417.             "full")
  418.                 echo "Doing a full backup to $TMPNAME"
  419.                
  420.                 if [ "$STREAMTYPE" == "xbstream" ]; then
  421.                     echo "...xbstream"
  422.                     $IONICE_CMD $INNOBACKUPEX --include=$DATABASE".*" --parallel=$THREADS --extra-lsndir="$LSNDIR" --stream=$STREAMTYPE --no-timestamp $OPTS "$BACKUPRES" 2>>"$LOGFILE" | $LZOP -c > $TMPNAME
  423.                     # Status of the pipe
  424.                     checkpipe "${PIPESTATUS[*]}" "innobackupex ($INNOBACKUPEX)" "lzop:($LZOP)"
  425.                 else
  426.                     echo "...tar: Doing a backup"
  427.                     rm -rf --preserve-root "$BACKUPRES"/tmp
  428.                     $IONICE_CMD $INNOBACKUPEX --include=$DATABASE".*" --parallel=$THREADS --extra-lsndir="$LSNDIR" --no-timestamp $OPTS "$BACKUPRES"/tmp 2>>"$LOGFILE"
  429.                     echo "...tar: Packing"
  430.                     ( cd "$BACKUPRES"/tmp; $TAR -cf - . | $LZOP -c > $TMPNAME )
  431.                     # TODO: Check for pipe status here
  432.                     echo "Pipe status is: ${PIPESTATUS[*]}"
  433.                     echo "...tar: Removing an old data"
  434.                     rm -rf --preserve-root "$BACKUPRES"/tmp
  435.                 fi
  436.                 ;;
  437.             "incremental")
  438.                 echo "Doing an incremental backup to $TMPNAME"
  439.                 # Check if we have full backup (check LSN, the file could be moved away to another server)?
  440.                 TOLSN=$(grep "to_lsn" "$LSNDIR/xtrabackup_checkpoints" | awk -F'=' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//')
  441.                 if [ -z "$TOLSN" ]; then
  442.                     echo "Cannot find LSN position of the previous backup in $LSNDIR"
  443.                     exit 1
  444.                 fi
  445.                
  446.                 if [ "$STREAMTYPE" == "xbstream" ]; then
  447.                     echo "...xbstream"
  448.                     $IONICE_CMD $INNOBACKUPEX --include=$DATABASE".*" --parallel=$THREADS --incremental --extra-lsndir="$LSNDIR" --incremental-lsn=$TOLSN --stream=$STREAMTYPE $OPTS "$BACKUPRES" 2>>"$LOGFILE" | $LZOP -c > $TMPNAME
  449.                     # Status of the pipe
  450.                     STATUS=${PIPESTATUS[*]}
  451.                     if [ "$STATUS" != "0 0" ]; then
  452.                         echo "The pipe of innobackupex ($INNOBACKUPEX) and lzop:($LZOP) was finished unseccussfully: $STATUS"
  453.                         exit 1
  454.                     else
  455.                         echo "The pipe operation was completed successfully"
  456.                     fi
  457.                 else
  458.                     echo "Removing ${BACKUPRES}/tmp"
  459.                     rm -rf --preserve-root "$BACKUPRES"/tmp
  460.                     echo "...tar: Doing a backup"
  461.                     echo $IONICE_CMD $INNOBACKUPEX --include=$DATABASE".*" --parallel=$THREADS --incremental --extra-lsndir="$LSNDIR" --no-timestamp --incremental-lsn=$TOLSN $OPTS "$BACKUPRES"/tmp
  462.                     $IONICE_CMD $INNOBACKUPEX --include=$DATABASE".*" --parallel=$THREADS --incremental --extra-lsndir="$LSNDIR" --no-timestamp --incremental-lsn=$TOLSN $OPTS "$BACKUPRES"/tmp 2>>"$LOGFILE"
  463.                     echo "...tar: Packing"
  464.                     ( cd "$BACKUPRES"/tmp; $TAR -cf - . | $LZOP -c > $TMPNAME )
  465.                     echo "...tar: Removing an old data"
  466.                     rm -rf --preserve-root "$BACKUPRES"/tmp
  467.                 fi
  468.                 ;;
  469.             *)
  470.                 echo "A wrong backup type was specified ($TYPE)"
  471.                 exit 1
  472.                 ;;
  473.         esac
  474.        
  475.         # Checking what we have in the logs
  476.         LOGISOK=$(tail -1 "$LOGFILE" | grep -c 'completed OK!')
  477.         if [ $LOGISOK -eq 1 ]; then
  478.             tail -1 "$LOGFILE"
  479.         else
  480.             echo '##### ALERT #####'
  481.             echo 'We don\'t have \'completed OK!\' status in the end of a logfile, so probably an error happened'
  482.             echo '##### ALERT #####'
  483.         fi
  484.        
  485.         echo "Renaming $TMPNAME"
  486.         # Fetch the latest LSN number
  487.         FROMLSN=$(grep "from_lsn" "$LSNDIR/xtrabackup_checkpoints" | awk -F'=' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//')
  488.         TOLSN=$(grep "to_lsn" "$LSNDIR/xtrabackup_checkpoints" | awk -F'=' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//')
  489.         mv -f $TMPNAME $BACKUPRES"/"$FILENAME"-"$TYPE"-"$FROMLSN"-"$TOLSN""$FILENAMESUFFIX     
  490.         echo "$TYPE backup is done. The result is in "$BACKUPRES"/"$FILENAME"-"$TYPE"-"$FROMLSN"-"$TOLSN""$FILENAMESUFFIX
  491.         # Storing table definitions
  492.         echo "Dumping table definitions"
  493.         mkdir -p $BACKUPRES/definitions
  494.         for TABLE in $(echo "show tables" | $MYSQL --defaults-extra-file=/root/.my.cnf -B -N "$DATABASE"); do
  495.             echo "Dumping table definition for ${DATABASE}.${TABLE}"
  496.             $IONICE_CMD $MYSQLDUMP --defaults-extra-file=/root/.my.cnf -q -Q --no-data "$DATABASE" "$TABLE" > $BACKUPRES/definitions/${TABLE}.sql 2>> "$LOGFILE"
  497.         done
  498.         echo "Doing a backup of $DATABASE routies"
  499.         $IONICE_CMD $MYSQLDUMP --defaults-extra-file=/root/.my.cnf -q -Q --routines --no-create-info --no-data --no-create-db --skip-opt "$DATABASE" > $BACKUPRES/definitions/__routines.sql 2>> "$LOGFILE"
  500.         echo "Archiving the definitions"
  501.         ( cd $BACKUPRES/definitions; $TAR -cf - . | $LZOP -c > $BACKUPRES/definitions-$FILENAME.tar.lzo )
  502.         rm -rf --preserve-root $BACKUPRES/definitions
  503.         echo "Done"
  504.         ;;
  505.    
  506.     "restore")
  507.         echo "Restoring $DATABASE" | tee "${LOGFILE}"
  508.        
  509.         if [ -z "$XBSTREAM" ]; then
  510.             echo "No xbstream (the part of percona's xtrabackup was found in $PATH" | tee -a "${LOGFILE}"
  511.             exit 1
  512.         fi
  513.        
  514.         # Review what files of full backups do we have (we take the latest one...)
  515.         FULLBACKUP=$(find "$BACKUPRES" -type f -name "$DATABASE-*-full-*$FILENAMESUFFIX" -exec basename {} \; | sort -r | head -1)
  516.         if [ -z "$FULLBACKUP" ]; then
  517.             echo "No full backup was found in $BACKUPRES for database $DATABASE" | tee -a "${LOGFILE}"
  518.             exit 1
  519.         else
  520.             echo "Considering $FULLBACKUP to be as a full backup for $DATABASE" | tee -a "${LOGFILE}"
  521.         fi
  522.        
  523.         # Searching for the incremental backups (We must !!!! apply the increments one by one to the base backup in the right order)
  524.         echo "Searching for the incremental backups" | tee -a "${LOGFILE}"
  525.         # We need to search files recursively. The age the files should be younger then the age of a full backup
  526.         PREV_FILE=$FULLBACKUP
  527.         while true; do
  528.             NEXT_LSN=$(basename $PREV_FILE $FILENAMESUFFIX | cut -f5 -d-) # 479:2100086046
  529.             NEXT_FILE="$DATABASE-*-incremental-$NEXT_LSN-*$FILENAMESUFFIX"
  530.             echo "   Searching for $NEXT_FILE" | tee -a "${LOGFILE}"
  531.             #INC=$(find "$BACKUPRES" -type f -name "$NEXT_FILE" -newer "$BACKUPRES/$PREV_FILE" -exec basename {} \; | sort -r | head -1)
  532.             INC=$(find "$BACKUPRES" -type f -name "$NEXT_FILE" -exec basename {} \; | sort -r | head -1)
  533.             if [ -z "$INC" ]; then
  534.                 echo "    No next incremental found" | tee -a "${LOGFILE}"
  535.                 break
  536.             fi
  537.             echo "     Found $INC" | tee -a "${LOGFILE}"
  538.             # Adding the file to the list
  539.             INCREMENTALS="$INCREMENTALS $INC"
  540.             PREV_FILE=$INC  
  541.         done
  542.        
  543.         FULLRESTORE=$DEST/full
  544.         # Restore a full backup
  545.         mkdir -p $FULLRESTORE
  546.         echo "Unpacking a full backup to $FULLRESTORE" | tee -a "${LOGFILE}"
  547.         $LZOP $LZOP_OPTS $BACKUPRES/$FULLBACKUP | $STREAMER_BIN -x --directory=$FULLRESTORE
  548.         # Status of the pipe
  549.         checkpipe "${PIPESTATUS[*]}" "lzop:($LZOP)" "$STREAMER_BIN" | tee -a "${LOGFILE}"
  550.                
  551.         # Replay the commited transactions on full (base) backup
  552.         echo "Replaying the commited transactions on full (base) backup" | tee -a "${LOGFILE}"
  553.         $IONICE_CMD $INNOBACKUPEX --apply-log --redo-only $FULLRESTORE --use-memory=1G $OPTS $EXPORT 2>>"$LOGFILE"
  554.         checkexit $? "$INNOBACKUPEX" | tee -a "${LOGFILE}"
  555.        
  556.         echo | tee -a "${LOGFILE}"
  557.         echo | tee -a "${LOGFILE}"
  558.         echo | tee -a "${LOGFILE}"
  559.         echo "====================================================================================================================" | tee -a "${LOGFILE}"
  560.         echo | tee -a "${LOGFILE}"
  561.         echo | tee -a "${LOGFILE}"
  562.         echo | tee -a "${LOGFILE}"
  563.        
  564.         # Processing the increments
  565.         for INC in $INCREMENTALS; do
  566.             echo "Processing $INC" | tee -a "${LOGFILE}"
  567.             INCRESTORE=$DEST/$(basename $INC $FILENAMESUFFIX)
  568.             mkdir -p $INCRESTORE
  569.             echo "Unpacking an incremental backup to $INCRESTORE" | tee -a "${LOGFILE}"
  570.             $LZOP $LZOP_OPTS $BACKUPRES/$INC | $STREAMER_BIN -x --directory=$INCRESTORE
  571.             # Status of the pipe
  572.             checkpipe "${PIPESTATUS[*]}" "lzop:($LZOP)" "$STREAMER_BIN" | tee -a "${LOGFILE}"
  573.                        
  574.             echo "Applying incremental backup log to a full backup" | tee -a "${LOGFILE}"
  575.             $IONICE_CMD $INNOBACKUPEX --apply-log --redo-only $FULLRESTORE --incremental-dir=$INCRESTORE --use-memory=1G $OPTS $EXPORT 2>>"$LOGFILE"
  576.             checkexit $? "$INNOBACKUPEX" | tee -a "${LOGFILE}"
  577.            
  578.             echo "Removing the folder whith incremental deltas" | tee -a "${LOGFILE}"
  579.             rm -rf $INCRESTORE
  580.             echo | tee -a "${LOGFILE}"
  581.             echo | tee -a "${LOGFILE}"
  582.             echo | tee -a "${LOGFILE}"
  583.             echo "====================================================================================================================" | tee -a "${LOGFILE}"
  584.             echo | tee -a "${LOGFILE}"
  585.             echo | tee -a "${LOGFILE}"
  586.             echo | tee -a "${LOGFILE}"
  587.         done
  588.        
  589.         # At last - prepare the baclup for usage
  590.         echo "Preparing again the full backup (base + incrementals) once again to rollback the pending transactions" | tee -a "${LOGFILE}"
  591.         $IONICE_CMD $INNOBACKUPEX --apply-log $FULLRESTORE --use-memory=1G $OPTS $EXPORT 2>>"$LOGFILE"
  592.         checkexit $? "$INNOBACKUPEX" | tee -a "${LOGFILE}"
  593.        
  594.         echo | tee -a "${LOGFILE}"
  595.         echo | tee -a "${LOGFILE}"
  596.         echo | tee -a "${LOGFILE}"
  597.         echo "====================================================================================================================" | tee -a "${LOGFILE}"
  598.         echo | tee -a "${LOGFILE}"
  599.         echo | tee -a "${LOGFILE}"
  600.         echo | tee -a "${LOGFILE}"
  601.        
  602.         echo "The restore is done. The result is in $FULLRESTORE folder" | tee -a "${LOGFILE}"
  603.         ;;
  604.     *)
  605.         echo "Wrong job-type was specified"
  606.         exit 1
  607.         ;;
  608. esac
  609.  
  610. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement