13.07.2015 Views

Linux System Administration Recipes A Problem-Solution Approach

Linux System Administration Recipes A Problem-Solution Approach

Linux System Administration Recipes A Problem-Solution Approach

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 4 ■ TAKING BACKUPS AND MANAGING DATA06 use MIME::Lite;07 use Net::SMTP;0809 sub sendmail();10 sub wanted();1112 my $email = 'my.name@gmail.com';13 my $from = 'my.address@example.com';14 my $smtpserver = "smtp.example.com";15 my @archive_list;16 my $backup_dir = "/home/jkemp/personal/";17 my $tarfile = "/home/jkemp/gmailtar.tgz";1819 find ( \&wanted, $backup_dir );20 Archive::Tar->create_archive($tarfile, "1", @archive_list);21 sendmail();2223 sub wanted() {24 return unless -f $File::Find::name;25 push @archive_list, $File::Find::name;26 }2728 sub sendmail() {29 my $msg = MIME::Lite->new(To => $email,30 From => $from,31 Subject => "Backup email",32 Type => "multipart/mixed");33 $msg->attach(Type => "application/gzip",34 Path => $tarfile,35 Filename => "gmailtar.tgz");36 $msg->send('smtp', $smtpserver);37 }Before you run it, change the permissions:chmod u+x gmail.plLines 04–10 import the other required modules and declare the subroutines that come later in thecode. Lines 12–16 set your variables up, which is another good coding practice.Line 18 uses the find command from File::Find to recurse over the directory you want to back up(here /home/jkemp/personal/). It calls the wanted subroutine (defined in lines 22–25) to check that thename that has been passed in (here /home/jkemp/personal corresponds to an existing file and, if so, addsthe file to the array of files to be archived).■ Note When using subroutines, you must either define them before they are first used or declare them first anddefine them at the end. I’ve done the latter here.111Download at WoweBook.Com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!