-
Notifications
You must be signed in to change notification settings - Fork 8
/
dhsnapshot.pl
executable file
·147 lines (132 loc) · 3.33 KB
/
dhsnapshot.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
#!/usr/bin/perl -w
use strict;
require File::Spec;
our %conf;
$conf{'basedir'} = get_basedir();
$conf{'emptydir'} = "$conf{'basedir'}/__emptydir/";
require 'dhsnapshot.conf';
my $lowest_interval = 'daily';
my %rotation;
$rotation{'daily'} = "
-rmdir daily.6
-rename daily.5 daily.6
-rename daily.4 daily.5
-rename daily.3 daily.4
-rename daily.2 daily.3
-rename daily.1 daily.2
-rename daily.0 daily.1
";
$rotation{'monthly'} = "
-rmdir monthly.5
-rename monthly.4 monthly.5
-rename monthly.3 monthly.4
-rename monthly.2 monthly.3
-rename monthly.1 monthly.2
-rename monthly.0 monthly.1
-rename weekly.3 monthly.0
";
$rotation{'weekly'} = "
-rmdir weekly.3
-rename weekly.2 weekly.3
-rename weekly.1 weekly.2
-rename weekly.0 weekly.1
-rename daily.6 weekly.0
";
#
# Check which action was called and execute it.
#
my $action = $ARGV[0] ? $ARGV[0] : "";
if ($action eq "daily") {
rotate("daily", 6);
sync();
} elsif ($action eq "weekly") {
rotate("weekly", 3);
} elsif ($action eq "monthly") {
rotate("monthly", 5);
} elsif ($action eq "sync") {
sync();
} else {
print "\n";
print "Invalid argument.\n" if $action ne "";
print "Use: $0 [daily|weekly|monthly|sync]\n\n";
exit;
}
# sync()
#
# Runs rsync to update the lowest interval
sub sync {
my $interval = $lowest_interval;
my @exclude_filter_settings = get_exclude_filter_settings();
system(
$conf{'nice_path'},'-19',
$conf{'rsync_path'},
'-e', "ssh -oIdentityFile=$conf{'private_key'}",
'-az', '--delete',
@exclude_filter_settings,
"--link-dest='../${interval}.1'",
$conf{'backup_source'},
"$conf{'backup_dest'}/${interval}.0/"
);
}
# rotate(interval, oldest_copy)
#
# Rotates directories, removing the oldest one.
# oldest_copy is the one being discarded/removed
sub rotate {
my $interval = shift;
my $oldest_copy = shift;
sync_to_empty($interval, $oldest_copy);
sftp_rotate($interval);
}
# sync_to_empty(interval, oldest_copy)
#
#Since DreamHost doesn't allow SSH access into the backup server,
#we must find an alternative way to delete a directory.
#We do this by rsync-ing it to an empty dir
sub sync_to_empty {
my $interval = shift;
my $oldest_copy = shift;
mkdir $conf{'emptydir'};
system(
$conf{'nice_path'},'-19',
$conf{'rsync_path'},
'-e', "ssh -oIdentityFile=$conf{'private_key'}",
'-az', '--delete',
$conf{'emptydir'},
"$conf{'backup_dest'}/${interval}.${oldest_copy}/"
);
rmdir $conf{'emptydir'};
}
# sftp_rotate(interval)
#
#Opens an SFTP connection to the server
#and issues rmdir/rename to rotate
#backup directories
sub sftp_rotate {
my $interval = shift;
open(
my $sftp_handle, "|-", $conf{'sftp_path'},
(
"-oIdentityFile=$conf{'private_key'}",
'-b', '-',
$conf{'backup_dest'}
)
);
print $sftp_handle $rotation{$interval};
close $sftp_handle;
}
# get_basedir()
#
# Figures out and returns the full path to the directory where this file lives
sub get_basedir {
my ($volume,$dir,$filename) = File::Spec->splitpath( File::Spec->rel2abs(__FILE__ ) );
return $dir;
}
# get_exclude_filter_settings()
#
# Checks if an exclude filter file is configured and returns the appropriate
# parameters to be passed to rsync
sub get_exclude_filter_settings {
return () unless ($conf{'exclude_filter'});
return ('--delete-excluded', "--exclude-from=$conf{'exclude_filter'}");
}