/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 88 - (show annotations)
Thu Sep 11 22:15:25 2008 UTC (14 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 1795 byte(s)
rewrite to be real pipe, and re-sort copy data to minimize diff

1 #!/usr/bin/perl
2
3 # split PostgreSQL dump into individual files for backup with git
4 #
5 # 09/11/08 20:10:39 CEST Dobrica Pavlinusic <dpavlin@rot13.org>
6
7 use warnings;
8 use strict;
9
10 use Getopt::Long;
11 use Fatal qw/:void open close mkdir chdir/;
12 my $git = '/tmp/pgsql-git-backup/';
13 my $verbose = 0;
14 my $database = '';
15 my $repack = 0;
16
17 my $usage = "Usage: $0 --database name < name-dump.sql\n";
18
19 GetOptions(
20 'git=s' => \$git,
21 'database=s' => \$database,
22 'verbose+' => \$verbose,
23 'repack' => \$repack,
24 ) or die $usage;
25
26 die $usage unless $database;
27
28 my $nr = 0;
29 my $name = $database;
30
31 sub git {
32 my $cmd = shift or die "no cmd?";
33 system($cmd) == 0 or die "$cmd FAILED $?";
34 }
35
36 sub dump_path {
37 return sprintf("%s/%04d%s.sql", $database, $nr, $name);
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 ) {
48 mkdir $database;
49 # git "git-add '$database'";
50 # 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>) {
59 if ( $sort_fh ) {
60 print $sort_fh $_;
61 if ( /^\\\.$/ ) {
62 close($sort_fh);
63 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 }
92 }
93
94 close($fh);
95
96 git "git-commit -m '$database' '$database'";
97
98 git "git-repack" if $repack;
99

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26