116 lines
3.1 KiB
Perl
116 lines
3.1 KiB
Perl
|
#!/usr/bin/perl -w
|
||
|
|
||
|
#---Configuration------
|
||
|
####@backe = ("/etc", "/home", "/lib", "/boot", "/var");
|
||
|
|
||
|
# wo sollen die archive hin?
|
||
|
$backupdir = "/home/backe123/ablage/";
|
||
|
# wo stehen die zu sichernden verzeichnisse (dateien, mit excludes)
|
||
|
$excludes = "/home/backe123/quellsaft/";
|
||
|
|
||
|
# mit welchem key soll das archiv verschluesselt werden?
|
||
|
$gpgkey_id = "A8969D03" ;
|
||
|
|
||
|
# an wen geht die mail?
|
||
|
$mailuser = "age";
|
||
|
|
||
|
|
||
|
#---Runtime------------
|
||
|
# welche verzeichnisse sollen gesichert werden ...
|
||
|
# dazu werden die dateien von excludes heran gezogen
|
||
|
opendir DIR, $excludes or die "Can't open dir to search for backup paths. $excludes\n";
|
||
|
# hier steht jetzt drin was gesichert werden soll
|
||
|
@backe = grep { $_ ne '.' && $_ ne '..' } readdir DIR;
|
||
|
closedir DIR;
|
||
|
|
||
|
# datum als eindeutigen namen fuer keyfile nutzen
|
||
|
$date = "0".join("-",getDate());
|
||
|
$keyfilename = "key".$date;
|
||
|
|
||
|
chdir($backupdir) || die ("Can't change to $backupdir! \n");
|
||
|
|
||
|
#---Mainloop-----------
|
||
|
foreach $dir (@backe){
|
||
|
# vorarbeit
|
||
|
print("\n----> generating random keys ..");
|
||
|
&generateRandomBits;
|
||
|
&generateSessionKey;
|
||
|
|
||
|
# die dateinamen koennten noch beser benannt werden
|
||
|
$filename = $dir."_backup";
|
||
|
|
||
|
&tar_it;
|
||
|
&crypt_it;
|
||
|
&mail_it;
|
||
|
|
||
|
print("well done!\n\n");
|
||
|
}
|
||
|
|
||
|
|
||
|
#---Subraum-------------
|
||
|
sub greeting{
|
||
|
print("[$date] Now I will make a backup of:\n");
|
||
|
foreach $dir(@backe){
|
||
|
print("\t".$dir."\n");
|
||
|
$excludes_file = $backupdir.$dir;
|
||
|
print("\t\t". $excludes_file."\n");
|
||
|
#TODO test ob dirs existieren
|
||
|
}
|
||
|
}
|
||
|
|
||
|
sub tar_it{
|
||
|
# archive erstellen
|
||
|
print("\n----> tar zcf: /$dir .. ");
|
||
|
$excludes_file = $excludes.$dir;
|
||
|
print("tar zcf $filename.tgz -C / --exclude-from $excludes_file $dir");
|
||
|
system("tar zcf $filename.tgz -C / --exclude-from $excludes_file $dir");
|
||
|
print(" tar done!\n");
|
||
|
}
|
||
|
|
||
|
sub crypt_it{
|
||
|
# endlich crypten
|
||
|
print("\n----> ccrypt $filename.tgz .. ");
|
||
|
# TODO: force ueberschreibt gnadenlos - vorher abchecken
|
||
|
system("ccrypt -k $keyfilename $filename.tgz --force");
|
||
|
system("rm $keyfilename");
|
||
|
system("mv $keyfilename.gpg $keyfilename$filename.gpg");
|
||
|
print(" ccrypt done!\n");
|
||
|
}
|
||
|
|
||
|
sub mail_it{
|
||
|
print("\n----> mail goes out .. ");
|
||
|
# mit mutt verschicken
|
||
|
# (-n -F /dev/null) umgehen die configs
|
||
|
# (-a filename) haengt datei ran; (-s subject) und body bleibt leer
|
||
|
system("mutt -n -F /dev/null -a $keyfilename$filename.gpg -i reminder.txt -s \"key-$dir\" $mailuser\@systemausfall.org < /dev/null");
|
||
|
print(" mail done!\n");
|
||
|
}
|
||
|
|
||
|
sub generateSessionKey{
|
||
|
# den key verschluesseln
|
||
|
print("\n----> delete existing key of today: ");
|
||
|
system("gpg --batch -q -r $gpgkey_id -e $keyfilename");
|
||
|
}
|
||
|
|
||
|
sub generateRandomBits{
|
||
|
# 4069 alphanumerische zeichen als key generieren
|
||
|
@chars = ("A" .. "Z", "a" .. "z", 0 .. 9);
|
||
|
$haltdenrand = join("", @chars[ map{rand @chars} (1 .. 4096) ]);
|
||
|
# datei zum schreiben oeffnen und als ausgabe auswaehlen
|
||
|
open(DERKEY, "> $keyfilename")
|
||
|
or die "$keyfilename could not be opened";
|
||
|
#alten ausgabekanal merken
|
||
|
$oldoutput = select(DERKEY);
|
||
|
print $haltdenrand;
|
||
|
select ($oldoutput);
|
||
|
close(DERKEY);
|
||
|
system("chmod 600 $keyfilename");
|
||
|
}
|
||
|
|
||
|
sub getDate{
|
||
|
use Time::localtime;
|
||
|
$tm = localtime;
|
||
|
return ($tm->year+1900, $tm->mon+1, $tm->mday);
|
||
|
}
|
||
|
|