#!/usr/bin/perl # Copyright (c) 2002-2004, James O'Gorman # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # Mbox backup script =head1 NAME mailbackup - mbox backup script =head1 SYNOPSYS mailbackup [options] =head1 OPTIONS --help | -h Displays help --user | -u Specifies the username --mdir | -m Specifies the user's mail directory (relative to /home/username/) eg. --mdir=Mail/ --archive | -a Specifies the archive directory (relative to /home/username/) eg. --archive=Mail/archive/ --mbox | -b Comma-separated list of mailboxes to be backed up eg. --mbox=list1,sentmail,savedmail =over 8 =head1 DESCRIPTION This program is designed to be run from a cronjob, but can be run from the command line. Its purpose is to take the username, maildir, archivedir, and mailbox files from the command line, and create backups of these files from the given options. Essentially, it just moves the mailbox files to the archivedir, and then re-creates the original files in the maildir. =cut sub usage { warn join(" ", @_)."\n" if @_; warn < \$debug, 'user|u:s' => \$username, 'archive|a:s' => \$archive, 'mdir|m:s' => \$mdir, 'mbox|b=s' => \@mbox, 'force' => \$force, 'help|h|?' => \$help ) or usage; usage if $help; @mbox = split(/,/,join(',',@mbox)); $mdir = "/home/$username/".$mdir; $archive = "/home/$username/".$archive; if ($force) { my $use_force = "--force"; } else { my $use_force = ""; } sub debug { print join(' ', @_) if $debug; } debug("Backing up to $archive\n"); debug("Username: $username\n"); debug("Mail dir: $mdir\n"); debug("Mailboxes: ".join(",",@mbox)."\n\n"); my $lockmgr = LockFile::Simple->make(-autoclean => 1); mkdir($archive, 0700); chdir($mdir); foreach my $file (@mbox) { my $archbox = "$archive/$file-".`date +%b-%Y -d "last month" | tr A-Z a-z`; chomp($archbox); my $archboxgz = $archbox.".gz"; # print $archboxgz."\n"; # print "$archbox"; if (-e $file) { if ( (-e $archboxgz) && ($force != "1") ) { print "$archboxgz already exists: skipping [enable force overwrite to move anyway]\n"; } else { debug("Moving $file to $archbox\n"); $lockmgr->lock("$file") || die "Unable to lock $file\n"; rename($file, $archbox); # Re-create moved files debug("Re-creating $mdir/$file: "); touch("$mdir/$file"); debug("Done.\n"); $lockmgr->unlock($file); debug("GZipping $archbox: "); if ($force) { `/bin/gzip --force $archbox`; } else { `/bin/gzip $archbox`; } debug("Done.\n"); } } else { debug("$file does not exist, continuing.\n"); } #print "$file\n"; }