Guest User

Untitled

a guest
Jun 20th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. ##################################################################
  4. # dbcopy.pl 1.0
  5. #
  6. # Copyright (C) 2008 Phil Christensen
  7. ##################################################################
  8.  
  9. use strict;
  10. use warnings;
  11.  
  12. use Options;
  13. use File::HomeDir;
  14. use File::Spec;
  15. use Data::Dumper;
  16.  
  17. my $home = File::HomeDir->my_home;
  18. my $prefs_dir = File::Spec->join($home, '.dbcopy');
  19.  
  20. sub load_prefs{
  21. my $prefs = {};
  22.  
  23. unless(-e $prefs_dir){
  24. return $prefs;
  25. }
  26.  
  27. opendir(PREFS_DIR, $prefs_dir);
  28. foreach my $config (readdir(PREFS_DIR)){
  29. if($config =~ m/\.{1,2}/){
  30. next;
  31. }
  32. my $config_path = File::Spec->join($prefs_dir, $config);
  33. open(CONFIG, $config_path);
  34. my @data = <CONFIG>;
  35. my $data = join('', @data);
  36. close(CONFIG);
  37.  
  38. no strict;
  39. my $result = eval($data);
  40. use strict;
  41.  
  42. if(defined($result)){
  43. $prefs->{$config} = $result;
  44. }
  45. }
  46. closedir(PREFS_DIR);
  47. return $prefs;
  48. }
  49.  
  50. sub save_prefs{
  51. my $prefs = shift;
  52.  
  53. unless(-e $prefs_dir){
  54. mkdir($prefs_dir);
  55. }
  56.  
  57. foreach my $config (keys %{$prefs}){
  58. my $config_path = File::Spec->join($prefs_dir, $config);
  59.  
  60. my $d = Data::Dumper->new([$prefs->{$config}]);
  61. $d->Indent(0)->Purity(1)->Useqq(1);
  62.  
  63. open(CONFIG, ">$config_path");
  64. print CONFIG $d->Dump();
  65. close(CONFIG);
  66. }
  67. }
  68.  
  69. sub get_options {
  70. my $prefs = shift;
  71. my $options = new Options(params => [
  72. ['host', 'h', '', 'A DB host to ssh to before dumping.'],
  73. ['database', 'd', '', 'The name of the database to dump.'],
  74. ['password', 'p', '', 'The database password.'],
  75. ['user', 'u', 'root', 'The database user.'],
  76. ['exclude', 'e', '', 'A table to exclude.'],
  77. ['include', 'i', '', 'A table to include.'],
  78. ['config', 'c', '', 'The dbcopy configuration to use.']
  79. ],
  80. flags =>[
  81. ['help', '?', 'Display this usage guide.'],
  82. ['all', 'a', 'Ignore saved excludes.']
  83. ]);
  84. }
  85.  
  86. sub main {
  87. my $prefs = load_prefs();
  88. my $options = get_options($prefs);
  89. $options->get_options();
  90.  
  91. if($options->get_result('help')){
  92. $options->print_usage();
  93. exit();
  94. }
  95.  
  96. my $config_name = $options->get_result('config');
  97. if($config_name eq '' && $options->{'unrecognized'}){
  98. $config_name = $ARGV[0];
  99. }
  100.  
  101. my $config;
  102. if($config_name && exists($prefs->{$config_name})){
  103. $config = $prefs->{$config_name};
  104. }
  105. else{
  106. $config = { 'host' => $options->get_result('host'),
  107. 'database' => $options->get_result('database'),
  108. 'exclude' => ($options->get_result('exclude')),
  109. 'include' => ($options->get_result('include')),
  110. 'user' => $options->get_result('user'),
  111. 'password' => $options->get_result('password')};
  112. }
  113.  
  114. my $exclude_line = '';
  115. if($config->{'exclude'} && !$options->get_result('all')){
  116. foreach my $table ($config->{'exclude'}){
  117. $exclude_line .= sprintf('--ignore-table=%s.%s ', $config->{'database'}, $table);
  118. }
  119. }
  120. elsif($config->{'include'}){
  121. foreach my $table ($config->{'include'}){
  122. $exclude_line .= "$table ";
  123. }
  124. }
  125.  
  126. my $command = "ssh %s \"mysqldump -u %s --password=%s %s %s | gzip -c -\" | gunzip -c | mysql -u root %s";
  127. $command = sprintf($command, $config->{'host'}, $config->{'user'}, $config->{'password'}, $exclude_line,
  128. $config->{'database'}, $config->{'database'});
  129.  
  130. if($config_name){
  131. $prefs->{$config_name} = $config;
  132. save_prefs($prefs);
  133. }
  134.  
  135. #print "\n\n$command\n\n";
  136. exit(system($command));
  137. }
  138.  
  139. main();
Add Comment
Please, Sign In to add comment