#!/usr/bin/perl # ##################################################################### # init variables & paths $backup_path = "/BACKUP/SERVERNAME/"; # with trailing "/" ! $bcount = 0; $now = time; $day_nr = int($now/(3600*24)); # static-backups # format: $backup[$bcount++]="NAME (unique):SOURCE DIR:LIFETIME (in days)"; $backup[$bcount++]="etc:/etc:20"; $backup[$bcount++]="boot:/boot:5"; $backup[$bcount++]="usr_local_etc:/usr/local/etc:15"; $backup[$bcount++]="system_bin:/bin:2"; $backup[$bcount++]="system_sbin:/sbin:2"; $base = $backup_path; $base_path = $backup_path; $start = time; ########################################################################## print "System Backup - Day: $day_nr \n\n"; print "\n\n"; system("/bin/df"); print "\n\n"; $total_size_uncomp = 0; $total_size_comp = 0; if (!(-e $base_path)) { print "\nCreating $base_path...\n"; system("mkdir $base_path"); } for ($i = 0; $i < $bcount; $i++) { if (!($backup[$i])) { print "$i empty - skipped...\n"; next; } @targs = split(/:/, $backup[$i]); print "Saving '$targs[0]'... "; $backup_path = $base_path; # uncomp size $logsize = ((split(/total/,`du -s -k $targs[1]`))[0]+1)-1 ; $uncomp = $logsize ; $total_size_uncomp = $total_size_uncomp + $logsize; if ($logsize > 999999) { $sizemb = int(($logsize/1000000)+0.5); $filesize = $sizemb . " GBytes"; } elsif ($logsize > 999) { $sizekb = int(($logsize/1000)+0.5); $filesize = $sizekb . " MBytes"; } else { $filesize = $logsize . " kBytes"; } print "$filesize -> "; if (!$targs[2]) { $day_path = $day_nr + 1; } else { $day_path = $day_nr + $targs[2]; } $tmp_path = $backup_path . $day_path; if (!(-e "$tmp_path")) { print "\nCreating $tmp_path...\n"; system("mkdir -p $tmp_path"); } $tarfile = $backup_path . $day_path . "/" . $targs[0] . ".tar.gz"; $oldtarfile = $backup_path . $day_nr . "/" . $targs[0] . ".tar.gz"; ### look in current day's backup dir, and try to remove last ### file, if available (-> to make space) $tmp_path = $backup_path . $day_nr; if (-e "$oldtarfile") { print "found old backup for this file to be removed:\n"; # print "* new file: \n"; # system("/bin/ls -l $tarfile"); print "* old file: \n"; system("/bin/ls -l $oldtarfile"); print "removing the old file...\n\n"; system("/bin/rm $oldtarfile"); } # BACKUP $backuptar = `tar czpf $tarfile $targs[1] /tmp/backup.err 2>&1`; $logsize = (-s $tarfile); # size of file $comp = $logsize / 1000; $total_size_comp = $total_size_comp + $comp; if ($logsize > 999999) { $sizemb = int(((-s $tarfile)/1000000)+0.5); $filesize = $sizemb . " MBytes"; } elsif ($logsize > 999) { $sizekb = int(((-s $tarfile)/1000)+0.5); $filesize = $sizekb . " kBytes"; } else { $filesize = $logsize . " Bytes"; } if ($uncomp) { $pourcent = 100- ( ( int( ( (100 * $comp) / $uncomp) *10) ) /10) ; } else { $pourcent = "undef"; } print "$filesize (- $pourcent %).\n"; } # foreach $backup $time = localtime; $end = time; $duration = $end - $start; print "\n\nTerminated : $time (Duration : $duration seconds).\n"; $logsize = $total_size_uncomp; if ($logsize > 999999) { $sizemb = int(($logsize/1000000)+0.5); $filesize = $sizemb . " GBytes"; } elsif ($logsize > 999) { $sizekb = int(($logsize/1000)+0.5); $filesize = $sizekb . " MBytes"; } else { $filesize = $logsize . " kBytes"; } $total_uncomp = $filesize; $logsize = $total_size_comp; if ($logsize > 999999) { $sizemb = int(($logsize/1000000)+0.5); $filesize = $sizemb . " GBytes"; } elsif ($logsize > 999) { $sizekb = int(($logsize/1000)+0.5); $filesize = $sizekb . " MBytes"; } else { $filesize = $logsize . " kBytes"; } $total_comp = $filesize; $pourcent = 100- ( ( int( ( (100 * $total_size_comp) / $total_size_uncomp) *10) ) /10) ; print "\nTotal size uncompressed : $total_uncomp => "; print "Backup size : $total_comp (-$pourcent %).\n\n"; print "\n\n"; system("/bin/df"); print "\n\n"; ### remove old backup # check if $backup_dir/$today_nr exists # if yes, remove each .tar file # and then directory. $tmp_path = $backup_path . $day_nr; if (-e "$tmp_path") { print "\n\n Old backup directory $tmp_path exists: cleaning up...\n"; system("/bin/rm $tmp_path/*.tar.gz"); system("/bin/rmdir $tmp_path"); } else { print "\n\n No old backup directory ($tmp_path) to be removed today...\n"; } ######## exit(0);