/[scripts]/trunk/pgsql-git-backup.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/pgsql-git-backup.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 84 by dpavlin, Thu Sep 11 19:59:52 2008 UTC revision 88 by dpavlin, Thu Sep 11 22:15:25 2008 UTC
# Line 8  use warnings; Line 8  use warnings;
8  use strict;  use strict;
9    
10  use Getopt::Long;  use Getopt::Long;
11    use Fatal qw/:void open close mkdir chdir/;
12  my $git = '/tmp/pgsql-git-backup/';  my $git = '/tmp/pgsql-git-backup/';
13  my $verbose = 0;  my $verbose = 0;
14  my $database = '';  my $database = '';
15    my $repack = 0;
16    
17  my $usage = "Usage: $0 --database name < name-dump.sql\n";  my $usage = "Usage: $0 --database name < name-dump.sql\n";
18    
19  GetOptions(  GetOptions(
20          'git=s'                 => \$git,          'git=s'         => \$git,
21          'database=s'    => \$database,          'database=s'    => \$database,
22          'verbose+'              => \$verbose,          'verbose+'      => \$verbose,
23            'repack'        => \$repack,
24  ) or die $usage;  ) or die $usage;
25    
26  die $usage unless $database;  die $usage unless $database;
27    
28  my $nr = 0;  my $nr = 0;
29  my $name = $database;  my $name = $database;
 my $d;  
30    
31  sub git {  sub git {
32          my $cmd = shift or die "no cmd?";          my $cmd = shift or die "no cmd?";
33          system($cmd) == 0 or die "$cmd FAILED $?";          system($cmd) == 0 or die "$cmd FAILED $?";
34  }  }
35    
36  sub write_chunk {  sub dump_path {
37          my $path = sprintf("%s/%02d%s.sql", $database, $nr++, $name);          return sprintf("%s/%04d%s.sql", $database, $nr, $name);
         open(my $fh, '>', $path) or die "can't open file $path: $!";  
         print $fh $d;  
         close($fh);  
         warn ">> $path ", -s $path, "\n" if $verbose;  
         git "git-add '$path'";  
38  }  }
39    
40  if ( ! -e $git ) {  mkdir $git if ! -e $git;
41          mkdir $git or die "can't create $git: $!";  
42          git 'git-init-db';  chdir $git;
43  }  warn "## using $git\n" if $verbose;
44    
45  chdir($git) or die "can't chdir $git: $!";  git 'git-init-db' if ! -e "$git/.git";
46    
47  if ( ! -e $database ) {  if ( ! -e $database ) {
48          mkdir $database or die "can't create $database: $!";          mkdir $database;
49  #       git "git-add '$database'";  #       git "git-add '$database'";
50  #       git "git-commit -m 'added $database directory' $database";  #       git "git-commit -m 'added $database directory' $database";
51  }  }
52    
53    my $sort_fh;
54    my $fh;
55    
56    open($fh, '>', dump_path);
57    
58  while(<STDIN>) {  while(<STDIN>) {
59          if (/-- Name: (.+?);/) {          if ( $sort_fh ) {
60                  write_chunk;                  print $sort_fh $_;
61                  $name = " $1";                  if ( /^\\\.$/ ) {
62                  $name =~ s/\W+/ /g;                          close($sort_fh);
63                  undef $d;                          undef $sort_fh;
64                  $d = '';                          open($fh, '>>', dump_path);
65                            print $fh $_;
66                    }
67                    next;
68            }
69    
70            if (/-- (.*)Name: (\w+);.*Schema: (\w+)/) {
71                    close($fh);
72                    my $path = dump_path;
73                    warn "> $path ", -s $path, "\n" if $verbose;
74                    git "git-add '$path'";
75    
76                    $nr++;
77                    $name = ' ' . $3 . '.' . $2;
78    #               $name .= '-data' if $1;
79                    open($fh, '>', dump_path);
80            }
81            if (/^COPY/) {
82                    print $fh $_;
83                    close($fh);
84    
85                    my $cmd = '| sort -n >> \'' . $git . '/' . dump_path . '\'';
86                    warn "## sorting using $cmd\n" if $verbose;
87                    open( $sort_fh, $cmd );
88                    next;
89            } else {
90                    print $fh $_;
91          }          }
         $d .= $_;  
92  }  }
93    
94  write_chunk;  close($fh);
95    
96  git "git-commit -m '$database' '$database'";  git "git-commit -m '$database' '$database'";
97    
98    git "git-repack" if $repack;
99    

Legend:
Removed from v.84  
changed lines
  Added in v.88

  ViewVC Help
Powered by ViewVC 1.1.26