-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_svn.pl
149 lines (114 loc) · 3.74 KB
/
backup_svn.pl
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/perl
use strict;
use warnings;
use File::Copy;
use Getopt::Compact;
my $hot_backup_script = '/var/backup/svn/backup_svn-hot-backup.py';
sub options_validate {
my ($opts) = @_;
for my $i(qw/path-backup path-repositories/) {
if (not $opts->{$i}) {
print "Error: $i is needed.\n";
return;
}
}
for my $i(qw/path-backup path-repositories/) {
if (not -d $opts->{$i}) {
print "Error: $i '" . $opts->{$i} . "' does not exists\n";
return;
}
}
if (not -w $opts->{'path-backup'}) {
print "Error: path-backup '" . $opts->{'path-backup'} . "' is not writeable\n";
return;
}
return 1;
}
my $options = Getopt::Compact->new(
name => 'Nethead SVN backup',
struct => [
[ 'keep-revision-in-name', 'If the backups get renamed, keep the revision number in the backup name' ],
[ 'only-backup-if-new-revision', 'Only backup if there is a new revision of the repository' ],
[ 'path-backup', 'The backup folder', '=s' ],
[ 'path-repositories', 'The repositories folder', '=s' ],
[ 'rename-backups', 'Sanely rename the backups' ],
]
);
my $opts = $options->opts();
if (not $options->status() or not options_validate($opts)) {
print $options->usage();
exit 1;
}
print "---\n";
print "svn backup\n";
# directory to save backups in, must be rwx by svn user
my $dir_backup = $opts->{'path-backup'};
my $dir_repositories = $opts->{'path-repositories'};
my %old_backups;
if (opendir(DIR, $dir_backup)) {
for my $repository (sort readdir(DIR)) {
if (-f "$dir_backup/$repository" and ($repository =~ m/(.+)-(\d+)-\d+.+/ or $repository =~ m/(.+)-(\d+).+/)) {
$old_backups{$1} = $2;
}
}
closedir(DIR);
}
else {
die 'Couldn\'t open backup dir';
}
if (opendir(DIR, $dir_repositories)) {
for my $repository (sort readdir(DIR)) {
if ($repository ne '.' and $repository ne '..' and -d "$dir_repositories/$repository") {
print "-\nLooking at repository $repository\n";
if ($opts->{'only-backup-if-new-revision'} and exists $old_backups{$repository}) {
my $cmd_svnlook = "svnlook youngest $dir_repositories/$repository";
my $out_svnlook = `$cmd_svnlook`;
if ($out_svnlook =~ m/(\d+)/) {
if ($1 == $old_backups{$repository}) {
print " No need to backup. Already at revision $1\n";
next;
}
else {
print " Revision changed from $old_backups{$repository} to $1. Let's backup\n";
}
}
else {
print " ERROR: Could not fetch current revision numbert\n";
}
}
my $cmd_hot_backup = "$hot_backup_script --archive-type=bz2 --num-backups=1 $dir_repositories/$repository $dir_backup";
my $out_hot_backup = `$cmd_hot_backup`;
print $out_hot_backup;
if ($out_hot_backup =~ m/Archive created, removing backup/s) {
if ($opts->{'rename-backups'}) {
$out_hot_backup =~ m|Archiving backup to '(.+?)'|s;
my $backup_name = $1;
my ($backup_revision, $backup_version, $backup_extension);
if ($backup_name =~ m/^.+-(\d+)-(\d+)\.(.+?)$/s) {
($backup_revision, $backup_version, $backup_extension) = ($1, $2, $3);
}
elsif ($backup_name =~ m/^.+-(\d+)\.(.+?)$/s) {
($backup_revision, $backup_version, $backup_extension) = ($1, undef, $2);
}
if ($opts->{'keep-revision-in-name'}) {
print " rename backup $backup_name to $repository-$backup_revision.$backup_extension\n";
move($backup_name, "$dir_backup/$repository-$backup_revision.$backup_extension");
}
else {
print " rename backup $backup_name to $repository.$backup_extension\n";
move($backup_name, "$dir_backup/$repository.$backup_extension");
}
}
}
else {
print " ERROR: Backup was not created!\n";
}
}
}
closedir(DIR);
}
else {
die 'Couldn\'t open repository dir';
}
print "---\n";
exit(0);