Skip to content

Instantly share code, notes, and snippets.

@sebva
Last active January 12, 2016 17:52
Show Gist options
  • Save sebva/80a1064dbf4d9ae14f79 to your computer and use it in GitHub Desktop.
Save sebva/80a1064dbf4d9ae14f79 to your computer and use it in GitHub Desktop.
Backup script using rsnapshot for Thecus N5550
#!/raid/module/Perl514/system/bin/perl -w
# Call this script weekly on Sunday from crontab
use strict;
use warnings;
use DateTime;
my $root = '/raid/rsnapshot';
#my $rsnap = "/bin/echo";
my $rsnap = "$root/rsnapshot";
my @config_files = ("$root/rsnapshot-critical.conf", "$root/rsnapshot-single.conf");
my $today = DateTime->today();
call_rsnapshot('weekly');
# First Sunday of the month
if ($today == nth_day_of_month(1, 7, $today->year(), $today->month())) {
call_rsnapshot('monthly');
if ($today->month() % 3 == 0) {
call_rsnapshot('quarterly');
}
if ($today->month() == 12) {
call_rsnapshot('yearly');
}
}
system('/raid/module/ModBase1/system/bin/sync');
#system('/raid/rsnapshot/shutdown.sh');
sub call_rsnapshot {
my $interval = shift;
foreach my $config (@config_files) {
system($rsnap, ('-c', $config, $interval));
}
}
# Here $nth is 1, 2, 3... for first, second, third, etc.
# Or -1, -2, -3... for last, next-to-last, etc.
# $dow is 1-7 for Monday-Sunday
# $month is 1-12
# http://stackoverflow.com/a/18908507
sub nth_day_of_month {
my ($nth, $dow, $year, $month) = @_;
my ($date, $delta);
if ($nth > 0) {
# For 1st etc. we want the last day of that week (i.e. 7, 14, 21, 28, "35")
$date = DateTime->new(year => $year, month => $month, day => 1);
$delta = $nth * 7 - 1;
} else {
# For last etc. we want the last day of the month
# (minus a week if next-to-last, etc)
$date = DateTime->last_day_of_month(year => $year, month => $month);
$delta = 7 * ($nth + 1); # $nth is negative
}
# Back up to the first $dow on or before $date + $delta
$date->add(days => $delta - ($date->day_of_week + $delta - $dow) % 7);
# If we're not in the right month, then that month doesn't have the
# specified date (e.g. there's no 5th Tuesday in Sept. 2013).
return (($date->month == $month) ? $date : undef);
}
#!/bin/sh
root=/raid/rsnapshot
filename=$root/rsnapshot.`date -u -Ihours`.log
echo $filename
$root/backup_script.pl > $filename
cat $filename | /raid/module/ModBase1/system/bin/sendmail --host=pi.home.vaucher.org -f backup@vaucher.org -- seb.vaucher+backup@gmail.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment