--- trunk/pgsql-git-backup.pl 2008/09/11 20:37:58 87 +++ trunk/pgsql-git-backup.pl 2008/09/11 22:15:25 88 @@ -27,20 +27,14 @@ my $nr = 0; my $name = $database; -my $d; sub git { my $cmd = shift or die "no cmd?"; system($cmd) == 0 or die "$cmd FAILED $?"; } -sub write_chunk { - my $path = 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'"; +sub dump_path { + return sprintf("%s/%04d%s.sql", $database, $nr, $name); } mkdir $git if ! -e $git; @@ -56,17 +50,48 @@ # git "git-commit -m 'added $database directory' $database"; } +my $sort_fh; +my $fh; + +open($fh, '>', dump_path); + while() { - if (/--.*Name: (\w+);.*Schema: (\w+)/) { - write_chunk; - $name = ' ' . $2 . '.' . $1; - undef $d; - $d = ''; + if ( $sort_fh ) { + print $sort_fh $_; + if ( /^\\\.$/ ) { + close($sort_fh); + undef $sort_fh; + open($fh, '>>', dump_path); + print $fh $_; + } + next; + } + + if (/-- (.*)Name: (\w+);.*Schema: (\w+)/) { + close($fh); + my $path = dump_path; + warn "> $path ", -s $path, "\n" if $verbose; + git "git-add '$path'"; + + $nr++; + $name = ' ' . $3 . '.' . $2; +# $name .= '-data' if $1; + open($fh, '>', dump_path); + } + if (/^COPY/) { + print $fh $_; + close($fh); + + my $cmd = '| sort -n >> \'' . $git . '/' . dump_path . '\''; + warn "## sorting using $cmd\n" if $verbose; + open( $sort_fh, $cmd ); + next; + } else { + print $fh $_; } - $d .= $_; } -write_chunk; +close($fh); git "git-commit -m '$database' '$database'";