Backup A Directory With Email

I wasn’t able to sleep, so I wrote this Perl script to backup a directory and mail yourself a copy. Change the variables in CAPS to suit your needs. It’s best to run this script as a cronjob.

Download a plaintext copy here.

Updated 5/8/2005: I’ve modified the code to use the File::Find library instead of a glob. My previous iteration of the code ignored hidden files (easily fixable by adding a second glob) and sub-directories. This new version fixes those two issues.

#!/usr/bin/perl

# This script creates an in-memory gzipped tar archive of the $BACKUP_DIR
# and emails the archive to an email account.

# Copyright Stephen Le
# Last Modified: May 8, 2005
# http://stephen.evilcoder.com

use MIME::Lite;
use Archive::Tar;
use Compress::Zlib;
use File::Find;
use strict;

my $SENDER      = 'sender@domain.com';
my $RECIPIENT   = 'recipient@domain.com';
my $BACKUP_DIR  = '/directory/to/backup';

my $tar = Archive::Tar->new;
find(\&wanted, $BACKUP_DIR);
sub wanted {
        $tar->add_files($File::Find::name);
}

my $msg = MIME::Lite->new(
    From    => $SENDER,
    To      => $RECIPIENT,
    Subject => "Backup of " . $BACKUP_DIR . " at " . localtime,
    Type    => "multipart/mixed",
);

$msg->attach(
    'Type'     => 'application/octet-stream',
    'Encoding' => 'base64',
    'Filename' => "backup.tar.gz",
    'Data'     => Compress::Zlib::memGzip( $tar->write )
);

$msg->send;

8 Responses to “Backup A Directory With Email”

  1. Kath Says:

    It’s pretty and colorful now! xD

  2. Stephen Says:

    Yup! Unfortunately, the plugin doesn’t cache the pretty code formatting — every time this post is loaded, it must re-generate the markup. Under normal loads, this is acceptable, but I’m afraid that, with a steady stream of visits, it’ll lag down my server. Unfortunately I can’t come up with a good way to cache the formatting. *sigh*

    This script has proven to be especially useful in recent days, as I’ve been able to have automatic backups of important directories on my server and laptop automatically saved on gmail. Combined with the + tagging — your_email_addy+some_random_tag@gmail.com — this script is a lifesaver.

  3. mk Says:

    look for http://dev.wp-plugins.org/wiki/GeshiSyntaxColorer
    maybe you’ll like my coloring plugin in advance :-)

  4. Stephen Says:

    Thanks for the link, mk!

    It looks like GeSHi might be more efficient than Enscript. I’ll test the plugin when I get a chance.

  5. Dave Says:

    Hi Stephen, thanks for the script. I tried changing it around a little but was unsuccessful (not well versed in perl quite yet). What I’m trying to do is get the script to only include files with a .php, .css, .html file extensions. Any ideas?

  6. Stephen Says:

    The ‘wanted’ function determines which files are added to the tar archive. It basically acts as a filter. Since this script was designed to add all files in a directory, the filter is wide open — it performs no match/check.

    You can add a match/check by using logical operations as so:

    sub wanted {
    /(php|css|html)$/i && $tar->add_files($File::Find::name);
    }

    The above subroutine uses a case-insensitive regular expression to check if the filename ends in either php, css, or html.

    I haven’t actually tried this code, but it should work. If not, tell me and I’ll figure out why.

  7. Python Email Backup Script at not.upbylunch Says:

    [...] EvilCoder’s Perl Email Backup script now has competition! import os import smtplib import tarfile from email import Encoders from email.MIMEBase import MIMEBase from email.MIMEMultipart import MIMEMultipart [...]

  8. Misunderestimated Email Backup Script at not.upbylunch Says:

    [...] Another email backup script to rival EvilCoder’s script. So have fun and use it with a cronjob to make it easier. And please change the information in the file so I don’t get a bunch of your backups. [...]

Leave a Reply