Guest User

Untitled

a guest
Apr 12th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.35 KB | None | 0 0
  1. #@author Andrey Knupp Vital, Projects Utils
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use File::Spec::Functions qw( catfile );
  7. use File::Copy::Recursive qw(fcopy rcopy dircopy fmove rmove dirmove);
  8.  
  9. system ( "cls" ) ;
  10.  
  11. print STDOUT "Initializing .... [ OK ] \n";
  12.  
  13. print "Full project path: " ;
  14. chomp ( my $path = <STDIN> ) ;
  15.  
  16. if ( $path eq '' ) {
  17.     print STDOUT "\nInvalid path....: Ok \n" ;
  18.     message ( 25 , 5 , '.' , "Exiting Now" , ': Ok' ) ;
  19.     exit 1 ;
  20.  
  21. } else {
  22.     my $dbname ;
  23.     my $from;
  24.     print "Assuming the <$path> path to the main course for the project ..\n";
  25.  
  26.     print "Project dir in <$path> ? dir name: " ;
  27.     chomp ( my $dir = <STDIN> ) ;
  28.     if ( ! $dir eq '' ) {
  29.         print "Assuming the <$dir> name to the database, you can change it \n";
  30.     }
  31.    
  32.     print "Backup database ? (Y/N) " ;
  33.     chomp ( my $dbc = <STDIN> ) ;
  34.    
  35.    
  36.     if ( ! $dir eq '' && $dbc eq 'Y' ) {
  37.         print "Database name ? assumed <$dir>, set: " ;
  38.         chomp ( my $db = <STDIN> ) ;
  39.         if ( ! $db eq '' ) {
  40.             $dbname = $db ;
  41.             print "Assumed schema name as <$db> \n" ;
  42.         } else {
  43.             $dbname = $dir ;
  44.             print "Assumed schema name as <$dir> \n" ;
  45.         }
  46.     }
  47.     print "Initializing validations ... [ OK ] \n" ;
  48.     print "Now checking if <$path/$dir> exists ... [ OK ] \n" ;
  49.     if ( -d "$path/$dir" ) {
  50.         print "Yeah, directory <$path/$dir> exists ... [ OK ] \n" ;
  51.         $from = "$path/$dir";
  52.     } else {
  53.         print "Opz, directory <$path/$dir> does not exists ... [ Fail ] \n" ;
  54.         message ( 25 , 5 , '.' , "Exiting Now" , ': Ok' ) ;
  55.         exit 1 ;
  56.     }
  57.    
  58.     our $bckPath = "";
  59.    
  60.    
  61.     sub dir {
  62.         my ( $sec , $min , $hour , $mday , $mon , $year , $wday , $yday , $isdst ) = localtime( time );
  63.        
  64.         $year += 1900 ; $mon++ ;
  65.         $mday = '0' . $mday if ($mday < 10 );
  66.         $mon = '0' . $mon if ($mon < 10 );
  67.        
  68.         my $dirName = "$year-$mon-$mday";
  69.        
  70.         print "Ok .. now we have to set the backup dir .. [ OK ] \n" ;
  71.         print "I have a suggest for you, use dir name as <$dirName> ? ( Y/N ) ";
  72.        
  73.         chomp ( my $choise = <STDIN> ) ;
  74.         if ( $choise eq '' or $choise eq 'N' ) {
  75.             print "You choise <NO> .. now you have to set it manually ... [ OK ]\n";
  76.             print "Enter backup directory name: " ;
  77.             chomp ( my $dirName = <STDIN> ) ;
  78.             if ( ! $dirName eq '' ) {
  79.                 print "Well .. now backup directory name as <$dirName> \n" ;
  80.                 $dir = $dirName ;
  81.             } else {
  82.                 print STDOUT "\nInvalid directory name ....: [ Ok ] \n" ;
  83.                 message ( 25 , 5 , '.' , "Exiting Now " , " [ Ok ] \n" ) ;
  84.                 print "Bye, bye !" ;
  85.                 exit 1 ;
  86.             }
  87.         } else {
  88.             if ( $choise eq 'Y' ) {
  89.                 print "Well .. now backup directory name as <$dirName> \n" ;
  90.                 $dir = $dirName ;
  91.             } else {
  92.                 print "Please .. answer Y/N or press enter for <N> \n";
  93.                 dir();
  94.             }
  95.         }
  96.        
  97.         print "Finally, you have to set now the backup path/<$dir> .. \n" ;
  98.         print "Sure .. now we have to set the backup path, set as: " ;
  99.         chomp ( my $backupPath = <STDIN> ) ;
  100.         if ( ! $backupPath eq '' ) {
  101.             mkdir( "$backupPath/$dir" ) or die( "Cannot create a folder called '$backupPath/$dir' ... [ Error ] \n");
  102.             print "Creating the directory ... [ OK ] \n" ;
  103.             print "Directory <$backupPath/$dir> created succesfully ! .. \n" ;
  104.         } else {
  105.             print "Opz, path can't be null ... [ Fail ] \n" ;
  106.             message ( 25 , 5 , '.' , "Exiting Now" , ': Ok' ) ;
  107.             exit 1 ;
  108.         }
  109.         $bckPath = "$backupPath/$dir" ;
  110.     }
  111.    
  112.     dir();
  113.    
  114.     if ( $dbc eq 'Y' ) {
  115.         print "Dumping database $dbname .. initializing ... [ OK ] \n" ;
  116.         print "UserName for schema <$dbname>: " ;
  117.        
  118.         chomp ( my $username = <STDIN> ) ;
  119.         print "PassWord for schema <$dbname>: " ;
  120.         chomp ( my $password = <STDIN> ) ;
  121.        
  122.         `mysqldump -u $username -p$password $dbname > $bckPath/$dbname.sql`;
  123.        
  124.         print "Database <$dbname:$bckPath/$dbname.sql> dumped sucessfully ! [ OK ] \n";
  125.     }
  126.    
  127.     print "Copying directory <$from> to <$bckPath> .... \n" ;
  128.     if ( dircopy ( $from , $bckPath ) ) {
  129.         print "Done ....... [ OK ] \n";
  130.         if ( $dbc eq 'Y' ) {
  131.             message ( 25 , 0 , '.' , 'Database backup' , '[ OK ]' );
  132.         }
  133.         message ( 25 , 0 , '.' , 'Project backup' , '[ OK ]' );
  134.         print "Bye, bye";
  135.     }
  136. }
  137.  
  138.  
  139. sub message {
  140.     my ( $count ,  $start , $char , $append , $prepend ) = @_ ;
  141.     print STDOUT "$append" ;
  142.     for ( my $i = $start ; $i < $count ; ++ $i ) {
  143.         if ( ! $char eq '' ) {
  144.             if ( $i eq ( $count - 1 ) ) {
  145.                 print STDOUT "$prepend \n";
  146.             } else {
  147.                 print STDOUT $char ;
  148.             }
  149.         } else {
  150.             exit 1;
  151.         }
  152.     }
  153. }
Add Comment
Please, Sign In to add comment