Hello, fellow perl monks. I have a question for the wise ones! I have a small mail server I use to supply email for familly, friends and myself. Nothing serious, so I have never really had any true backups. The server I can setup quickly since it is bare bones setup just to do mail and act as a firewall. Only thing important on it is the email messages and some webpages.

I want to write a perl script to automate the process. I have written a preliminary script to backup selected mail accounts to a zip drive.

Questions I have:

  1. Can I run this from cron without opening some security hole on my server
  2. I was unsure about how to be sure that Im not backing up the file while the mail program (EXIM) is updating the spool. I checked exims docs it doesnt mention anything. Do I just need to use flock??
  3. Any suggestion!
Here is the code:

#!/usr/bin/perl -w # simple script to backup specified mail accounts # to a zip disk. use strict; my $zip_drive = "/dev/hdc4"; my $mnt_dir = "/mnt"; my $tmp_dir = "/tmp/"; my $bkup_root = "/var/spool/mail/"; my @bkups = ( 'kirk', 'spock', 'mrmister', 'you' ); my $todays_date = join '-', (('jan','feb','mar','apr','may','jun', 'jul','aug','sep','oct','nov','dec')[(localtime)[4]]), ((localtime)[3]), ((localtime)[5]+1900); $ENV{PATH} = "/bin:/usr/bin"; print "Mounting zip drive for backup...\n"; die "Couldn't mount zip drive: $?\n" if system ("mount","-t","vfat",$zip_drive,$mnt_dir); my $old_dir = `pwd`; chomp $old_dir; chdir "$bkup_root" or die "Can't change working directory to [$bkup_root]: $!\n"; foreach my $bkup (@bkups){ my $arch = "$tmp_dir$bkup-$todays_date.tar"; if (system("tar","-cvvf",$arch,$bkup)){ warn "Could not tar file [$bkup]:$!\n"; next; }else { print "File has been tarred.\n"; if (system("gzip","-9",$arch)){ warn "Could not gzip file [$arch]:$!\n"; }else { print "File has been gziped.\n"; if (system("mv","$arch.gz","$mnt_dir/mail/")){ warn "Could not move file from temp directory", " to zip drive: $!\n"; }else{ print "File has been moved to zip drive for" " backup\n"; } } } } chdir $old_dir or die "Can't restore working directory to [$old_dir].\n"; print "Unmounting zip drive...\n"; die "Couldn't unmount zip drive: $?\n" if system ("umount",$mnt_dir); print "\nZip disk can be removed.\n";

Thanks for any suggestions!!
zzspectrez


In reply to Using perl to automate mail backup by zzspectrez

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.