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;
May 9th, 2005 at 10:30 am
It’s pretty and colorful now! xD
May 9th, 2005 at 3:48 pm
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.
May 10th, 2005 at 2:28 am
look for http://dev.wp-plugins.org/wiki/GeshiSyntaxColorer
maybe you’ll like my coloring plugin in advance
May 10th, 2005 at 10:05 pm
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.
October 17th, 2005 at 1:32 am
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?
October 17th, 2005 at 3:47 am
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.
April 12th, 2006 at 2:56 pm
[...] 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 [...]
April 20th, 2006 at 3:22 am
[...] 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. [...]