/[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 87 by dpavlin, Thu Sep 11 20:37:58 2008 UTC revision 88 by dpavlin, Thu Sep 11 22:15:25 2008 UTC
# Line 27  die $usage unless $database; Line 27  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/%04d%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  mkdir $git if ! -e $git;  mkdir $git if ! -e $git;
# Line 56  if ( ! -e $database ) { Line 50  if ( ! -e $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: (\w+);.*Schema: (\w+)/) {          if ( $sort_fh ) {
60                  write_chunk;                  print $sort_fh $_;
61                  $name = ' ' . $2 . '.' . $1;                  if ( /^\\\.$/ ) {
62                  undef $d;                          close($sort_fh);
63                  $d = '';                          undef $sort_fh;
64                            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    

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

  ViewVC Help
Powered by ViewVC 1.1.26