/[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 82 by dpavlin, Thu Sep 11 18:44:04 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 write_chunk {  sub git {
32          my $path = sprintf("%s/%02d%s.sql", $database, $nr++, $name);          my $cmd = shift or die "no cmd?";
33          open(my $fh, '>', $path) or die "can't open file $path: $!";          system($cmd) == 0 or die "$cmd FAILED $?";
         print $fh $d;  
         close($fh);  
         warn ">> $path ", -s $path, "\n" if $verbose;  
         system "git-add '$path'";  
34  }  }
35    
36  if ( ! -e $git ) {  sub dump_path {
37          mkdir $git or die "can't create $git: $!";          return sprintf("%s/%04d%s.sql", $database, $nr, $name);
         chdir $git;  
         system 'git-init-db';  
38  }  }
39    
40    mkdir $git if ! -e $git;
41    
42    chdir $git;
43    warn "## using $git\n" if $verbose;
44    
45    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  #       system "git-add '$database'";  #       git "git-add '$database'";
50  #       system "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                  $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'";
97    
98    git "git-repack" if $repack;
99    
 system "git-commit -m '$database' '$database'";  

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

  ViewVC Help
Powered by ViewVC 1.1.26