Advertisement
Guest User

Untitled

a guest
Oct 24th, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.93 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. ##############################################################################
  3. ##
  4. ## Written by: Jared Cheney <jared.cheney@gmail.com>
  5. ##
  6. ## Original Template written by:
  7. ## Brandon Zehm <caspian@dotconf.net> and Jared Cheney <elph@leph.net>
  8. ##
  9. ## License:
  10. ##
  11. ## This <programName> (hereafter referred to as "program") is free software;
  12. ## you can redistribute it and/or modify it under the terms of the GNU General
  13. ## Public License as published by the Free Software Foundation; either version
  14. ## 2 of the License, or (at your option) any later version.
  15. ## Note that when redistributing modified versions of this source code, you
  16. ## must ensure that this disclaimer and the above coder's names are included
  17. ## VERBATIM in the modified code.
  18. ##
  19. ## Disclaimer:
  20. ## This program is provided with no warranty of any kind, either expressed or
  21. ## implied. It is the responsibility of the user (you) to fully research and
  22. ## comprehend the usage of this program. As with any tool, it can be misused,
  23. ## either intentionally (you're a vandal) or unintentionally (you're a moron).
  24. ## THE AUTHOR(S) IS(ARE) NOT RESPONSIBLE FOR ANYTHING YOU DO WITH THIS PROGRAM
  25. ## or anything that happens because of your use (or misuse) of this program,
  26. ## including but not limited to anything you, your lawyers, or anyone else
  27. ## can dream up. And now, a relevant quote directly from the GPL:
  28. ##
  29. ## NO WARRANTY
  30. ##
  31. ## 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
  32. ## FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
  33. ## OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
  34. ## PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
  35. ## OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  36. ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
  37. ## TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
  38. ## PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
  39. ## REPAIR OR CORRECTION.
  40. ##
  41. ##############################################################################
  42. # Written by: Jared Cheney <jared.cheney@gmail.com>
  43. #
  44. # Purpose: This program will extract the necessary portions from a full
  45. # database mysqldump file required to restore a single table.
  46. #
  47. # Creation Date: 2008-05-23
  48. #
  49. # Changelog:
  50. # 2008-05-23 v1.0 Jared Cheney
  51. # - initial release
  52. #
  53. #############################################################################
  54.  
  55. ## FIXME's:
  56.  
  57. use strict;
  58.  
  59.  
  60. ## Global Variable(s)
  61. my %conf = (
  62. "programName" => $0, ## The name of this program
  63. "version" => '1.0', ## The version of this program
  64. "authorName" => 'Jared Cheney', ## Author's Name
  65. "authorEmail" => 'jared.cheney@gmail.com', ## Author's Email Address
  66. "debug" => 0, ## Default debug level
  67. "mode" => '',
  68.  
  69.  
  70. ## PROGRAM VARIABLES
  71. "logFile" => '', ## default log file, if none specified on command line
  72. "prepend" => '', ## Something that gets added to every msg that the script outputs
  73. "alertCommand" => '', ## cmd to run if printmsg() contains the string 'ERR' or 'CRIT' or 'WARN'
  74. "noExtras" => 0, ## if 1, then we'll skip extra cmds for disabling foreign key checks, etc. at top of file
  75. "listTables" => 0, ## if 1, then return a list of tables contained in the restore file
  76. );
  77.  
  78. $conf{'programName'} =~ s/(.)*[\/,\\]//; ## Remove path from filename
  79. $0 = "[$conf{'programName'}]";
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89. #############################
  90. ##
  91. ## MAIN PROGRAM
  92. ##
  93. #############################
  94.  
  95. ## Initialize
  96. initialize();
  97.  
  98. ## Process Command Line
  99. processCommandLine();
  100.  
  101.  
  102. ## get current timestamp for use later
  103. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
  104. $mon +=1;
  105. if ($mon < 10) {$mon = "0" . $mon}
  106. if ($mday < 10) {$mday = "0" . $mday}
  107. $year +=1900;
  108. printmsg ("current date is $mon/$mday/$year",3);
  109.  
  110.  
  111. if ($conf{'mode'} eq "running") {
  112. printmsg ("INFO => PROGRAM STARTED",1);
  113.  
  114. #############################
  115. ######## MAIN CODE ####
  116. #############################
  117.  
  118. if ($conf{'restoreFile'}) {
  119. ## open the mysqldump file
  120. open(STDIN, "<$conf{'restoreFile'}") || quit("ERROR => Couldn't open file $conf{'restoreFile'}: $!", 3);
  121. }
  122.  
  123. my $flag = 0;
  124.  
  125. ## go through the file one line at a time
  126. while (my $line = <STDIN>) {
  127.  
  128. if ($conf{'listTables'}) {
  129. if ($line =~ /^-- Table structure for table `(.*)`/) {
  130. print $1 . "\n";
  131. }
  132. }
  133. else {
  134.  
  135. ## if we're not ignoring extra lines, and we haven't set the flag, and if it's not a 40000 code, then print
  136. if (!$conf{'noExtras'} && !$flag) {
  137. if ($line =~ /^\/\*!(.....).*\*\//) { print $line unless ($1 == 40000); }
  138. }
  139.  
  140. ## set a flag when we encounter the table we want
  141. if ($line =~ /^-- Table structure for table `$conf{'tableName'}`/) {
  142. $flag = 1;
  143. printmsg("Turning flag on", 1);
  144. }
  145. ## turn flag off as soon as we encounter next table definition
  146. elsif ($line =~ /^-- Table structure for table/) {
  147. $flag = 0;
  148. printmsg("Turning flag off", 1);
  149. }
  150.  
  151. ## if flag is set, then print to STDOUT, otherwise just move on
  152. if ($flag) {
  153. print $line;
  154. }
  155. }
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164. #############################
  165. ######## END MAIN CODE ####
  166. #############################
  167.  
  168. }
  169.  
  170. ## Quit
  171. quit("",0);
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190. ######################################################################
  191. ## Function: help ()
  192. ##
  193. ## Description: For all those newbies ;)
  194. ## Prints a help message and exits the program.
  195. ##
  196. ######################################################################
  197. sub help {
  198. print <<EOM;
  199.  
  200. $conf{'programName'}-$conf{'version'} by $conf{'authorName'} <$conf{'authorEmail'}>
  201.  
  202. This program will parse a full mysqldump file and
  203. extract the necessary portions required to restore
  204. a single table. The output is printed to STDOUT, so you'll
  205. want to redirect to a file from the command line, like so:
  206. $conf{'programName'} > somefile.sql
  207.  
  208. Brought to you by the fine tech folk at www.tsheets.com - Time Is Money, Track It!
  209.  
  210. Usage: $conf{'programName'} -t <table name> -r <restore file> [options]
  211.  
  212. Required:
  213. -t <table name> table name to extract from the file
  214.  
  215.  
  216. Optional:
  217. -r <restore file> mysqldump file that you want to parse. If not specified,
  218. then it reads from STDIN
  219. --listTables If set, then a list of tables existing in your restore file is returned,
  220. and no other actions are taken
  221. --noExtras If set, then extra cmds at top of mysqldump file
  222. will not be included (such as disabling foreign key checks).
  223. Usually you will want these things changed before restoring a
  224. table, so the default is for these to be included.
  225. -v verbosity - use multiple times for greater effect
  226. -h Display this help message
  227.  
  228.  
  229.  
  230. EOM
  231. exit(1);
  232. }
  233.  
  234.  
  235.  
  236.  
  237.  
  238. ######################################################################
  239. ## Function: initialize ()
  240. ##
  241. ## Does all the script startup jibberish.
  242. ##
  243. ######################################################################
  244. sub initialize {
  245.  
  246. ## Set STDOUT to flush immediatly after each print
  247. $| = 1;
  248.  
  249. ## Intercept signals
  250. $SIG{'QUIT'} = sub { quit("$$ - $conf{'programName'} - EXITING: Received SIG$_[0]", 1); };
  251. $SIG{'INT'} = sub { quit("$$ - $conf{'programName'} - EXITING: Received SIG$_[0]", 1); };
  252. $SIG{'KILL'} = sub { quit("$$ - $conf{'programName'} - EXITING: Received SIG$_[0]", 1); };
  253. $SIG{'TERM'} = sub { quit("$$ - $conf{'programName'} - EXITING: Received SIG$_[0]", 1); };
  254.  
  255. ## ALARM and HUP signals are not supported in Win32
  256. unless ($^O =~ /win/i) {
  257. $SIG{'HUP'} = sub { quit("$$ - $conf{'programName'} - EXITING: Received SIG$_[0]", 1); };
  258. $SIG{'ALRM'} = sub { quit("$$ - $conf{'programName'} - EXITING: Received SIG$_[0]", 1); };
  259. }
  260.  
  261. return(1);
  262. }
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271. ######################################################################
  272. ## Function: processCommandLine ()
  273. ##
  274. ## Processes command line storing important data in global var %conf
  275. ##
  276. ######################################################################
  277. sub processCommandLine {
  278.  
  279.  
  280. ############################
  281. ## Process command line ##
  282. ############################
  283.  
  284. my $x;
  285. my @ARGS = @ARGV;
  286. my $numargv = scalar(@ARGS);
  287. help() unless ($numargv);
  288. for (my $i = 0; $i < $numargv; $i++) {
  289. $x = $ARGS[$i];
  290. if ($x =~ /^-h$|^--help$/) { help(); }
  291. elsif ($x =~ /^-v+/i) { my $tmp = (length($&) - 1); $conf{'debug'} += $tmp; }
  292. elsif ($x =~ /^-l$/) { $i++; $conf{'logFile'} = $ARGS[$i];}
  293. elsif ($x =~ /^-p$/) { $i++; $conf{'policyName'} = $ARGS[$i];}
  294. elsif ($x =~ /^-t$/) { $i++; $conf{'tableName'} = $ARGS[$i];}
  295. elsif ($x =~ /^-r$/) { $i++; $conf{'restoreFile'}= $ARGS[$i];}
  296. elsif ($x =~ /^--noExtras$/i) { $conf{'noExtras'} = 1; }
  297. elsif ($x =~ /^--listTables$/i) { $conf{'listTables'} = 1; }
  298. else {
  299. printmsg("Error: \"$x\" is not a recognised option!", 0);
  300. help();
  301. }
  302. }
  303.  
  304. my @required = (
  305. 'tableName',
  306. );
  307.  
  308. if ($conf{'listTables'}) {
  309. $conf{'mode'} = 'running';
  310. return(1);
  311. }
  312.  
  313. foreach (@required) {
  314. if (!$conf{$_}) {
  315. quit("ERROR: Value [$_] was not set after parsing command line arguments!", 1);
  316. }
  317. }
  318. $conf{'mode'} = 'running';
  319. return(1);
  320. }
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337. ###############################################################################################
  338. ## Function: printmsg (string $message, int $level)
  339. ##
  340. ## Description: Handles all messages - logging them to a log file,
  341. ## printing them to the screen or both depending on
  342. ## the $level passed in, $conf{'debug'} and wether
  343. ## $conf{'mode'}.
  344. ##
  345. ## Input: $message A message to be printed, logged, etc.
  346. ## $level The debug level of the message. If
  347. ## not defined 0 will be assumed. 0 is
  348. ## considered a normal message, 1 and
  349. ## higher is considered a debug message.
  350. ## $leaveCarriageReturn Whether or not to strip carriage returns (always will strip, unless other than 0)
  351. ##
  352. ## Output: Prints to STDOUT, to LOGFILE, both, or none depending
  353. ## on the state of the program.
  354. ##
  355. ## Example: printmsg ("WARNING: We believe in generic error messages... NOT!", 1);
  356. ###############################################################################################
  357. sub printmsg {
  358. my %incoming = ();
  359. (
  360. $incoming{'message'},
  361. $incoming{'level'},
  362. $incoming{'leaveCarriageReturn'},
  363. ) = @_;
  364. $incoming{'level'} = 0 if (!defined($incoming{'level'}));
  365. $incoming{'leaveCarriageReturn'} = 0 if (!defined($incoming{'leaveCarriageReturn'}));
  366. $incoming{'message'} =~ s/\r|\n/ /sg unless ($incoming{'leaveCarriageReturn'} >= 1);
  367.  
  368. ## Add program name and PID
  369. ## $incoming{'message'} = "- $conf{'programName'} [$$]: " . $incoming{'message'};
  370. ## add prepend info
  371. ## $incoming{'message'} = "$conf{'prepend'} : $incoming{'message'}";
  372.  
  373. ## Continue on if the debug level is >= the incoming message level
  374. if ($conf{'debug'} >= $incoming{'level'}) {
  375. ## Print to the log file
  376. if ($conf{'logFile'}) {
  377. open (LOGFILE, ">>$conf{'logFile'}");
  378. print LOGFILE "$conf{'programName'}:[". localtime() . "] $incoming{'message'}\n";
  379. close (LOGFILE);
  380. }
  381. if ($conf{'alertCommand'} && ($conf{'debug'} == 0) && ($incoming{'message'} =~ /ERR|CRIT|WARN/) ) {
  382. my $tmpAlert = $conf{'alertCommand'};
  383. $tmpAlert =~ s/MESSAGE/$incoming{'message'}/g;
  384. system ($tmpAlert);
  385. }
  386. ## Print to STDOUT
  387. if ($conf{'debug'} >= 1) {
  388. print STDOUT "$conf{'programName'}:[" . localtime() . "]($incoming{'level'}): $incoming{'message'}\n";
  389. }
  390. else {
  391. print STDOUT "$conf{'programName'}:[" . localtime() . "] $incoming{'message'}\n";
  392. }
  393. }
  394.  
  395. ## Return
  396. return(0);
  397. }
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407. ######################################################################
  408. ## Function: quit (string $message, int $errorLevel)
  409. ##
  410. ## Description: Exits the program, optionally printing $message. It
  411. ## returns an exit error level of $errorLevel to the
  412. ## system (0 means no errors, and is assumed if empty.)
  413. ##
  414. ## Example: quit("Exiting program normally", 0);
  415. ######################################################################
  416. sub quit {
  417. my %incoming = ();
  418. (
  419. $incoming{'message'},
  420. $incoming{'errorLevel'}
  421. ) = @_;
  422.  
  423.  
  424. $incoming{'errorLevel'} = 0 if (!defined($incoming{'errorLevel'}));
  425.  
  426. ## Print exit message
  427. if ($incoming{'message'}) {
  428. printmsg($incoming{'message'}, 0);
  429. }
  430.  
  431. ## Exit
  432. exit($incoming{'errorLevel'});
  433. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement