-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmongodb_backups
58 lines (45 loc) · 2.17 KB
/
mongodb_backups
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
#!/usr/bin/perl
# backup de databases
# en ruim alles van meer dan twee maanden oud ( maand - 3) op
use Time::localtime;
$year = localtime->year()+1900;
$day = localtime->mday();
$month = localtime->mon()+1;
$vandaag = $year."_".$month."_".$day; #
my $dirname = "/backups/mongo_$vandaag";
my ($objectStoreUrl, $objectStoreUser, $objectStorePass) = @ARGV;
# maak de directory voor vandaag
$mkdircommando = "mkdir $dirname";
system($mkdircommando);
# maak een mongo backup
system("mongodump --out $dirname");
# maak er een tar bal van
my $tarbalname = qq($dirname.tgz);
my $tarcommando = qq(tar --remove-files -czf $tarbalname $dirname/);
system($tarcommando);
# copy to the object store
system("curl -v -X PUT -T $dirname.tgz --user $objectStoreUser:$objectStorePass $objectStoreUrl/$dirname.tgz");
my ( $sec, $min, $hour, $day, $month, $year, $wday, $yday, $isdst ) = localtime( time );
$month++; # is 0 based
$year += 1900; # is 0 based
use POSIX qw(mktime);
my $timestamp = time;
my $sourcedir = '/backups';
my ( $dirhandle, $file );
opendir( $dirhandle, $sourcedir ) or die( "Can't open dir $sourcedir" );
while ( $file = readdir( $dirhandle ) ) {
if ( $file =~ /mongo_(\d+)_(\d+)_(\d+).tgz/ ) {
my ( $file_year, $file_month, $file_day ) = ( $1, $2, $3 );
my $file_timestamp = mktime( 0, 0, 0, $file_day, $file_month - 1, $file_year - 1900 );
my $diff = $timestamp - $file_timestamp;
if ( ( $diff > 3600 * 24 * 90 ) || ( $diff > 3600 * 24 * 7 && $file_day ne '1' && $file_day ne '15' ) ) {
eval { system( "rm $sourcedir/$file" ); };
if ( $@ ) {
$feedback .= "\nRemove old backups FAILED \n reported: " . $@;
}
# delete from object store; ignore errors
eval { system("curl -v -X DELETE --user $objectStoreUser:$objectStorePass $objectStoreUrl/$sourcedir/$file"); }
}
}
}
closedir( $dirhandle );