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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 88 - (hide annotations)
Thu Sep 11 22:15:25 2008 UTC (14 years, 4 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 dpavlin 82 #!/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 dpavlin 85 use Fatal qw/:void open close mkdir chdir/;
12 dpavlin 82 my $git = '/tmp/pgsql-git-backup/';
13     my $verbose = 0;
14     my $database = '';
15 dpavlin 87 my $repack = 0;
16 dpavlin 82
17     my $usage = "Usage: $0 --database name < name-dump.sql\n";
18    
19     GetOptions(
20 dpavlin 86 'git=s' => \$git,
21 dpavlin 82 'database=s' => \$database,
22 dpavlin 86 'verbose+' => \$verbose,
23     'repack' => \$repack,
24 dpavlin 82 ) or die $usage;
25    
26     die $usage unless $database;
27    
28     my $nr = 0;
29     my $name = $database;
30    
31 dpavlin 83 sub git {
32     my $cmd = shift or die "no cmd?";
33     system($cmd) == 0 or die "$cmd FAILED $?";
34     }
35    
36 dpavlin 88 sub dump_path {
37     return sprintf("%s/%04d%s.sql", $database, $nr, $name);
38 dpavlin 82 }
39    
40 dpavlin 85 mkdir $git if ! -e $git;
41 dpavlin 82
42 dpavlin 85 chdir $git;
43     warn "## using $git\n" if $verbose;
44 dpavlin 83
45 dpavlin 85 git 'git-init-db' if ! -e "$git/.git";
46    
47 dpavlin 82 if ( ! -e $database ) {
48 dpavlin 85 mkdir $database;
49 dpavlin 83 # git "git-add '$database'";
50     # git "git-commit -m 'added $database directory' $database";
51 dpavlin 82 }
52    
53 dpavlin 88 my $sort_fh;
54     my $fh;
55    
56     open($fh, '>', dump_path);
57    
58 dpavlin 82 while(<STDIN>) {
59 dpavlin 88 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 dpavlin 82 }
69 dpavlin 88
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 dpavlin 82 }
93    
94 dpavlin 88 close($fh);
95 dpavlin 82
96 dpavlin 83 git "git-commit -m '$database' '$database'";
97 dpavlin 86
98     git "git-repack" if $repack;
99    

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26