"Fossies" - the Fresh Open Source Software Archive

Member "usr/bin/mysql-zrm-verify-backup" (26 Aug 2013, 8240 Bytes) of package /linux/privat/MySQL-zrm-3.0-release.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/perl
    2 #
    3 # Copyright (c) 2006 Zmanda Inc.  All Rights Reserved.
    4 #
    5 # This program is free software; you can redistribute it and/or modify it
    6 # under the terms of the GNU General Public License version 2 as published
    7 # by the Free Software Foundation.
    8 #
    9 # This program is distributed in the hope that it will be useful, but
   10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   11 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   12 # for more details.
   13 #
   14 # You should have received a copy of the GNU General Public License along
   15 # with this program; if not, write to the Free Software Foundation, Inc.,
   16 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   17 #
   18 # Contact information: Zmanda Inc, 505 N Mathlida Ave, Suite 120
   19 # Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
   20 #
   21 #
   22 
   23 use strict;
   24 use warnings;
   25 use File::Find;
   26 use File::Spec::Functions;
   27 use File::Temp qw/ :POSIX /;
   28 use POSIX qw(setsid _exit);
   29 use lib "/usr/lib/mysql-zrm";
   30 use ZRM::Common;
   31 
   32 
   33 my $MD5SUM="md5sum";
   34 my $CHECKSUM_FILE="zrm_checksum";
   35 my $CHECKSUM_PENDING=".checksum_pending";
   36 
   37 #usage strings 
   38 my $USAGE_VERIFY_BACKUP_STRING=  
   39         "\t\t[--source-directory <directory name>]\n";  
   40 
   41 my @VERIFYOPT=qw/source-directory=s
   42                  create-checksum/;
   43 
   44 my %mdcheck;
   45 my $checkCompressedFile = ""; 
   46 
   47 
   48 #$_[0] specifies the directory whoes file's check sum needs to be verified
   49 sub verifyCheckSum()
   50 {
   51     my $dir = $_[0];
   52         find( {wanted => sub
   53                 {
   54             if( $_ ne "index" && $_ ne $checkCompressedFile && 
   55                 $_ ne $EXTRACTED_FILENAME && $_ ne $CHECKSUM_FILE ){
   56                 my $file = $File::Find::fullname;
   57                 if( -f $file ){
   58                     my $x = $MD5SUM." -b "."\"$file\"";
   59                         $x = &execCmdAndGetOutput($x);
   60                     if( !defined $x ){
   61                         &printError( "Could not get md5 checksum\n" );
   62                     }else{
   63                         my @a = split( / /, $x );
   64                         &verifyCMForFile( $file, $a[0],
   65                                     $dir );
   66                     }
   67                 }
   68             }
   69                 }, follow => 1}, $dir);
   70     for(keys%indexdata){
   71         &checkIfFile( $_ );
   72     }
   73 }
   74 
   75 #$_[0] file
   76 #$_[1] current checksum
   77 #$_[2] source-directory
   78 sub verifyCMForFile()
   79 {
   80     my $sep = "/";
   81     if( $onWindows == 1 ) {
   82         $sep = "\\";
   83     }
   84     my $dir = $_[2];
   85         my $newSum=$_[1];
   86     my $x = $_[0];
   87     my $file = $x;
   88     $dir=~s/\Q$sep\E*$/$sep/;# make sure $dir has exactly one separator char at the end
   89     $x=~/$dir/;
   90     $x = catfile($indexdata{"backup-directory"}, $' );
   91     my $oldSum=$indexdata{$x};
   92     if(defined $oldSum && $newSum eq $oldSum){
   93         if( $verbose ){
   94             &printLog( "checksum for file $file is correct\n" );
   95         }
   96     }else{
   97         &printError( "checksum for file $file does not match\n" );
   98     }
   99     delete $indexdata{$x};
  100 }
  101 
  102 #Checks if $_[0] is a file and if so prints an error
  103 sub checkIfFile()
  104 {
  105     my $file = $_[0];
  106     $file=~/$indexdata{"backup-directory"}/;
  107     $file=$';
  108     if( $file ){
  109         &printError( "File $_[0] not found\n" );
  110     }
  111 }
  112 
  113 #$_[0] directory name 
  114 #Calculates the md5 checksum of each;
  115 sub calculateMD5Sum()
  116 {
  117         my $dir = $_[0];
  118         find( {wanted => sub
  119                 {
  120                         if( $_ ne "index" && $_ ne $CHECKSUM_PENDING ){
  121                                 my $file = $File::Find::fullname;
  122                                 if( -f $file ){
  123                                         my $x = $MD5SUM." -b "."\"$file\"";
  124                                         $x = &execCmdAndGetOutput($x);
  125                                         if( !defined $x ){
  126                                                 &printError( "Could not get md5 checksum\n" );
  127                                         }else{
  128                                                 my @a = split( / /, $x );
  129                                                 $mdcheck{$file}=$a[0];
  130                                         }
  131                                 }
  132                         }
  133                 },
  134                 follow => 1}, $_[0]);
  135 }
  136 
  137 sub doPostBackupPlugin()
  138 {
  139         if( $verbose ){
  140                 &printLog( "Executing post-backup-plugin\n" );
  141         }
  142         my @params;
  143         push @params, "--backup-directory";
  144         push @params, $inputs{"source-directory"};
  145         push @params, "--checksum-finished";
  146         my $r = &execPlugin( "post-backup-plugin", @params );
  147         return $r;
  148 }
  149 
  150 sub doStart()
  151 {
  152         open STDIN, ">>/dev/null";
  153         open STDOUT, ">>/dev/null";
  154         open STDERR, ">>/dev/null";
  155         my $pid = fork();
  156         if(!defined $pid){
  157                 &printAndDie( "Cannot fork monitor" );
  158         }
  159         if( ! $pid ){
  160                 setsid();
  161         &main( "" );
  162         }
  163         _exit(0);
  164 }
  165 
  166 sub doCreateChecksum()
  167 {
  168     my $dir = $inputs{"source-directory"};
  169     my $fsnap = catfile( $dir, $ZRM_MOUNT_DETAILS );
  170     if( -f $fsnap ){
  171         &printError( "$dir contains snapshots as backup. Checksum is not supported on this type of backups\n" );
  172         return;
  173     }
  174     my $fn = catfile( $dir, $CHECKSUM_PENDING );
  175     my $nc = catfile( $dir, ".nochecksum" );
  176     if( -f $nc ){
  177         open TP, ">$fn";
  178         close TP;
  179         unlink( $nc );
  180     }
  181 
  182     if( ! -f $fn ){
  183         return;
  184     }
  185 
  186     my $filename = catfile( $dir, $CHECKSUM_FILE );
  187     if( -f $filename ){
  188         &printError( "Checksum file already exists\n" );
  189         return;
  190     }
  191     &calculateMD5Sum( $dir );
  192     unless( open( FH, ">$filename" ) ){
  193         &printError( "Unable to open checksum file\n" );
  194         return;
  195     }
  196     print FH "backup-directory=".$inputs{"source-directory"}."\n";
  197     if( %mdcheck ){
  198                 for(keys%mdcheck){
  199                         print FH "$_=$mdcheck{$_}\n";
  200                 }
  201         }
  202     close( FH );
  203     unlink( $fn );
  204     &doPostBackupPlugin();
  205 }
  206 
  207 sub doVerifyBackup()
  208 {
  209     if( !$inputs{"source-directory"} ) {
  210         if( !$lastBackupDir ){
  211             &printWarning( "Nothing to verify\n" );
  212             return;
  213         }
  214         $inputs{"source-directory"}=$lastBackupDir;
  215     }
  216     if( !-d $inputs{"source-directory"} ) {
  217         &printAndDie( "Cannot find source directory ".$inputs{"source-directory"}."\n" );
  218     }
  219 
  220     if( defined $inputs{"create-checksum"} ){
  221         &doCreateChecksum();
  222         return;
  223     }
  224 
  225     my $dir = $inputs{"source-directory"};
  226 
  227     my $fsnap = catfile( $dir, $ZRM_MOUNT_DETAILS );
  228     if( -f $fsnap ){
  229         &printError( "$dir contains snapshots as backup. Verification cannot be done this type of backups\n" );
  230         return;
  231     }
  232 
  233     my $nc = catfile( $dir, ".nochecksum" );
  234     if( -f $nc ){
  235         &printError( "Checksum information not yet created. If verification functionality is needed, please run the following command and then run verify.\nmysql-zrm --action verify-backup --create-checksum --source-directory $dir\n" );
  236         return;
  237     }
  238 
  239     my $fn = catfile( $dir, $CHECKSUM_PENDING );
  240     if( -f $fn ){
  241         &printError( "Checksum creation in progress. Please run the verify after the checksum creation is done\n" );
  242         return;
  243     }
  244 
  245     my $cmFile = catdir( $dir, $CHECKSUM_FILE );
  246     my $fname;
  247     if( -f $cmFile ){
  248         $fname = $cmFile;
  249         $checkCompressedFile = "";
  250         my $ext = catfile( $dir, $EXTRACTED_FILENAME );
  251         if( -f $ext ){
  252             &printAndDie( "This backup directory seems to contain extracted data. Please run 'mysql-zrm --action extract-backup --cleanup --source-directory $dir' before trying to verify the backup\n" );
  253         }
  254     }else{
  255         $fname = catfile( $dir, $INDEX_FILENAME );
  256         $checkCompressedFile = $COMPRESS_FILENAME;
  257     }
  258 
  259         my $r = &parseIndexFile( $fname );
  260     if( $r == 0 ){
  261         print "ERROR: Looks like backup data for $dir is not available\n";
  262         return;
  263     }
  264     if( defined $indexdata{"compress"} || defined $indexdata{"encrypt"} ){
  265         $r = 1;
  266         $r = &uncompressBackup( $inputs{"source-directory"} , 1 );
  267         if( $r == 0 ){
  268             &printAndDie( "Unable to uncompress backup\n" );
  269         }
  270     }
  271     &verifyCheckSum( $dir );
  272     if( $zrm_error == 0 ){
  273         &printLog( "Verification successful\n" );
  274     }else{
  275         &printError( "Errors found during verification\n" );
  276     }
  277     if( defined $indexdata{"compress"} || defined $indexdata{"encrypt"} ){
  278         &removeUncompressedBackup( $inputs{"source-directory"}, \%indexdata );
  279     }
  280 }
  281 
  282 #Sets up defaults for backup
  283 sub setupDefaults()
  284 {
  285         $action = "verify-backup";
  286         $USAGE_STRING = $USAGE_VERIFY_BACKUP_STRING.$USAGE_COMMON_OPTIONS_STRING;
  287 }
  288 
  289 sub main()
  290 {
  291     if( ! defined $_[0] && 
  292         defined $ARGV[0] && $ARGV[0] eq "--create-checksum" ){
  293         &doStart();
  294         &my_exit();
  295     }
  296         &setupDefaults();
  297         &initCommon(@VERIFYOPT);
  298     &createConfigFile();
  299         &doVerifyBackup();
  300         &my_exit();
  301 }
  302 
  303 &main();
  304 
  305