summaryrefslogtreecommitdiff
blob: a812774125743e8f97cb8fad41de8927d1eb6907 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/perl -w

use strict;

my $repos_path  = $ARGV[0];
my $dumpfile    = $ARGV[1];
my $type        = $ARGV[2];

my $bin_svnadmin = `which svnadmin`;
my $bin_svnlook  = `which svnlook`;
my $bin_bz2      = `which bzip2`;

$bin_svnlook  =~ s/\n//;
$bin_svnadmin =~ s/\n//;
$bin_bz2      =~ s/\n//;

if ($bin_svnadmin eq "") {$bin_svnadmin = "/usr/bin/svnadmin"};
if ($bin_svnlook  eq "") {$bin_svnlook  = "/usr/bin/svnlook"};
if ($bin_bz2      eq "") {$bin_bz2      = "/bin/bzip2"};

# Figure out the starting revision. Use 0 if we cannot read the
# last-dumped file, else use the revision in that file incremented
# by 1.
my $new_start = 0;
if (open LASTDUMPED, "$dumpfile.last")
{
    my $line = <LASTDUMPED>;
    if (defined $line and $line =~ /^(\d+)/)
    {
        $new_start = $1 + 1;
    }
    close LASTDUMPED;
}

# Query the youngest revision in the repos.
my $youngest = `$bin_svnlook youngest $repos_path`;
defined $youngest && $youngest =~ /^\d+$/
    or die "$0: 'svnlook youngest $repos_path' cannot get youngest revision.\n";
chomp $youngest;

if ($type eq "incremental")
{
    if ($new_start > $youngest) 
    {
        print "Nothing to do!\n";
    } else {
        ## Do the backup.
        system("$bin_svnadmin dump $repos_path --revision $new_start:$youngest --incremental >> $dumpfile.tmp") == 0
            or die "$0: svnadmin dump to '$dumpfile.tmp' failed.\n";

        # Store a new last-dumped revision.
        open LASTDUMPED, "> $dumpfile.last.tmp"
            or die "$0: cannot open '$dumpfile.last.tmp' for writing: $!\n";
        print LASTDUMPED "$youngest\n";
        close LASTDUMPED
            or die "$0: error in closing '$dumpfile.last.tmp' for writing: $!\n";

        # Rename to final locations.
        rename("$dumpfile.tmp", "$dumpfile.$new_start.$youngest")
            or die "$0: cannot rename '$dumpfile.tmp' to '$dumpfile': $!\n";

        rename("$dumpfile.last.tmp", "$dumpfile.last")
            or die "$0: cannot rename '$dumpfile.last.tmp' to '$dumpfile.last': $!\n";

        system("$bin_bz2 $dumpfile.$new_start.$youngest") == 0
            or die "$0: compressing dump file $dumpfile.$new_start.$youngest failed.\n";
    }
} else {
    
    system("$bin_svnadmin dump $repos_path >> $dumpfile.full.tmp") == 0
        or die "$0: svnadmin dump to '$dumpfile.tmp' failed.\n";
    
    rename("$dumpfile.full.tmp", "$dumpfile.full")
        or die "$0: cannot rename '$dumpfile.full.tmp' to '$dumpfile.full': $!\n";
    
    system("$bin_bz2 -f $dumpfile.full") == 0
        or die "$0: compressing dump file $dumpfile.full failed.\n";
}
# All done!