I've made a simple Perl backup script. Here are the features:

  • It will tar a directory you specify in a variable.
  • The filename/s of the archive/s created will have a prefix specified by you in a variable, and it will include the date when the archive was created. A sample filename is "Michael.Balcos-2013-06-23.tar.gz"
  • As for the destination directory for the archive/s, you can also set that.
  • Backup archives older than a specified number of days in the script will be deleted.

To download the script, please click this: backupScript.txt

Be very careful in setting the $backupDir variable (it sets the destination directory of your backup archive/s). Files which are older than the maximum age specified in the script will be deleted in the directory specified by the $backupDir variable.

If you'd like to use this script, please rename the backupScript.txt file to backupScript.pl, do a "chmod 710 backupScript.pl". I recommend that you use the script as root, thus please change the ownership by issuing a "chown root:root backupScript.pl". You can use this script in a cron job. Currently, I've tested this script in Slackware64 14.0. It should work in other distributions.

$timeStampFinal=sprintf("%04d-%02d-%02d", $timeStampTmp[5]+1900,$timeStampTmp[4]+1,$timeStampTmp[3]);
chomp $timeStampFinal;
$maxAgeInDays=2; # Backups older than this value (in days) will be deleted.
$backupDir='/backup'; # This is where your backup archives/tarballs will go.
$dirToBackup='/directory/To/Backup'; # This is the directory to be backup.
$backupFilePrefix='Michael.Balcos'; # Format of backup filename is "<$backupFilePrefix>-<$timeStampFinal>.tar.gz". Note that the whole absolute path ( except for the leading "/" ) will be stored in your archive/tarball.
$command='tar czpf '.$backupDir.'/'.$backupFilePrefix.'-'.$timeStampFinal.'.tar.gz '.$dirToBackup.' > /dev/null 2>&1';
$command='find '.$backupDir.' -type f -maxdepth 1 -mtime +'.$maxAgeInDays.' -exec rm -f {} \;';