/[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 87 - (show annotations)
Thu Sep 11 20:37:58 2008 UTC (15 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 1410 byte(s)
better parse Name: line to split data into it's own files

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 my $d;
31
32 sub git {
33 my $cmd = shift or die "no cmd?";
34 system($cmd) == 0 or die "$cmd FAILED $?";
35 }
36
37 sub write_chunk {
38 my $path = sprintf("%s/%04d%s.sql", $database, $nr++, $name);
39 open(my $fh, '>', $path) or die "can't open file $path: $!";
40 print $fh $d;
41 close($fh);
42 warn ">> $path ", -s $path, "\n" if $verbose;
43 git "git-add '$path'";
44 }
45
46 mkdir $git if ! -e $git;
47
48 chdir $git;
49 warn "## using $git\n" if $verbose;
50
51 git 'git-init-db' if ! -e "$git/.git";
52
53 if ( ! -e $database ) {
54 mkdir $database;
55 # git "git-add '$database'";
56 # git "git-commit -m 'added $database directory' $database";
57 }
58
59 while(<STDIN>) {
60 if (/--.*Name: (\w+);.*Schema: (\w+)/) {
61 write_chunk;
62 $name = ' ' . $2 . '.' . $1;
63 undef $d;
64 $d = '';
65 }
66 $d .= $_;
67 }
68
69 write_chunk;
70
71 git "git-commit -m '$database' '$database'";
72
73 git "git-repack" if $repack;
74

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26