<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Eddie On Everything &#187; perl</title>
	<atom:link href="http://www.eddieoneverything.com/tag/perl/feed" rel="self" type="application/rss+xml" />
	<link>http://www.eddieoneverything.com</link>
	<description>Tips &#38; tricks on things that interest me</description>
	<lastBuildDate>Wed, 11 Jan 2012 08:29:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Managing Server Load when unzipping many large files</title>
		<link>http://www.eddieoneverything.com/articles/managing-server-load-when-unzipping-many-large-files.php</link>
		<comments>http://www.eddieoneverything.com/articles/managing-server-load-when-unzipping-many-large-files.php#comments</comments>
		<pubDate>Fri, 22 Apr 2011 17:32:37 +0000</pubDate>
		<dc:creator>eddie</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[red hat]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.eddieoneverything.com/articles/managing-server-load-when-unzipping-many-large-files.php</guid>
		<description><![CDATA[So, I have about 500 zipped up files that I needed to unzip.&#160; 
Each of these files is about 100M when decompressed.&#160; 
I ran into a big problem when unzipping the files all at once – when I did a simple “unzip \*zip&#34;, the server load eventually skyrocketed.&#160; 
The load got so high, in fact, [...]]]></description>
			<content:encoded><![CDATA[<p>So, I have about 500 zipped up files that I needed to unzip.&#160; </p>
<p>Each of these files is about 100M when decompressed.&#160; </p>
<p>I ran into a big problem when unzipping the files all at once – when I did a simple “<a href="http://www.eddieoneverything.com/linux/caution-filename-not-matched-error-when-unzipping-multiple-files-workaround.php" target="_blank">unzip \*zip</a>&quot;, the server load eventually skyrocketed.&#160; </p>
<p>The load got so high, in fact, that mySQL and httpd couldn’t even keep up with the incoming web requests, and the mySQL database eventually crashed.  Well, it didn&#8217;t exactly crash, but the requests got so stacked up that it stopped accepting new connections, so it might as well have been crashed.</p>
<p>I assume that this is because the Linux server (Red Hat, in my case) has to do a bunch of “behind the scenes” type things when allocating large chunks of space for new files.&#160; You know, file system record keeping, delayed disk I/O, things like that.</p>
<p>&#8220;nice -n19&#8243; wasn&#8217;t doing the job, so I tried doing a simple batch file that would sleep 10 seconds before unzipping each file.&#160; This worked, for the most part, but the server load eventually got higher than I like to see. Left alone, the whole thing could have cascaded out of control again, especially given that I wasn’t taking into account the *other* things the server may have been doing at the time.  And I wanted to go to lunch, so hanging out and watching it wasn&#8217;t an attractive option.</p>
<p>I decided to write a simple script that checked the server load via /proc/loadavg, then only performed the unzip operation if the load was below a certain value. </p>
<p>Here’s my script, written in Perl.</p>
<pre name="code" class="perl">
#!/usr/bin/perl

$MAX_LOAD = .9;

open hIN, "&lt;files";

while (&lt;hIN&gt;){
   $filename = $_;
   chomp($filename);

   $cmd = "unzip \"$filename\"";

   print "$cmd\n";
   print `$cmd`;

   # Get the load average
   do {
      $load = `cat /proc/loadavg  | cut -f1 -d " "`;
      chomp($load);

      if ($load > $MAX_LOAD){
         print "Load is $load - Sleeping\n";
         sleep (5);
      }

   }while ($load > $MAX_LOAD);

}
close hIN;
</pre>
<p>I chose to keep the server load below 0.9, a nice number, IMHO.</p>
<p><b>Update:</b> I&#8217;ve done a little more <a href="http://www.commandlinefu.com/commands/view/49/run-a-command-only-when-load-average-is-below-a-certain-threshold" target="_blank">checking around</a>, and I see that there&#8217;s a Unix command that will do a lot of this for you &#8211; the &#8220;batch&#8221; command will only execute a command when the server load drops below a certain threshold.  The default is 0.8.  Good to know.</p>
<p>
The method I outline above is still useful in other instances where &#8220;batch&#8221; can&#8217;t be used.  For instance, I&#8217;m now using this method of checking the server load in a data loading script.  I parse my input files, generate sql &#038; execute it.  Then, every couple thousand queries or so I check the server load.  If it&#8217;s getting too high, the script sleeps until the load falls to an acceptable level.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.eddieoneverything.com/articles/the-zen-of-taco-bell-programming-using-unix-tools-to-prevent-reinventing-the-wheel.php" title="The Zen of Taco Bell Programming &#8211; Using Unix Tools to Prevent Reinventing the Wheel">The Zen of Taco Bell Programming &#8211; Using Unix Tools to Prevent Reinventing the Wheel</a></li><li><a href="http://www.eddieoneverything.com/linux/rotating-backup-directories-using-cp-al-hardlinks-to-save-disk-space.php" title="Rotating Backup Directories using cp -al (hardlinks) to Save Disk Space">Rotating Backup Directories using cp -al (hardlinks) to Save Disk Space</a></li><li><a href="http://www.eddieoneverything.com/linux/using-rsync-ssh-to-backup-files-between-linux-machines-perl.php" title="Using rsync &#038; ssh to Backup Files Between Linux Machines (Perl)">Using rsync &#038; ssh to Backup Files Between Linux Machines (Perl)</a></li><li><a href="http://www.eddieoneverything.com/linux/argument-list-too-long-from-rm-command-a-perl-script-to-get-around-the-problem.php" title="&#8220;Argument list too long&#8221; from rm command.   A Perl Script to Get Around the Problem">&#8220;Argument list too long&#8221; from rm command.   A Perl Script to Get Around the Problem</a></li><li><a href="http://www.eddieoneverything.com/articles/linux-forward-file-not-working-make-sure-you-have-the-permissions-set-correctly.php" title="Linux .forward file not working?  Make sure you have the permissions set correctly">Linux .forward file not working?  Make sure you have the permissions set correctly</a></li><li><a href="http://www.eddieoneverything.com/linux/caution-filename-not-matched-error-when-unzipping-multiple-files-workaround.php" title="Caution: filename not matched error when unzipping multiple files workaround">Caution: filename not matched error when unzipping multiple files workaround</a></li><li><a href="http://www.eddieoneverything.com/articles/how-to-scan-a-computer-for-open-ports.php" title="How to Scan a Computer for Open Ports">How to Scan a Computer for Open Ports</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.eddieoneverything.com/articles/managing-server-load-when-unzipping-many-large-files.php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To Use Global Variables From Another File In A Perl Module or Package</title>
		<link>http://www.eddieoneverything.com/programming/how-to-use-global-variables-from-another-file-in-a-perl-module-or-package.php</link>
		<comments>http://www.eddieoneverything.com/programming/how-to-use-global-variables-from-another-file-in-a-perl-module-or-package.php#comments</comments>
		<pubDate>Sun, 07 Mar 2010 22:42:33 +0000</pubDate>
		<dc:creator>eddie</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[perl modules]]></category>
		<category><![CDATA[sharing variables]]></category>

		<guid isPermaLink="false">http://www.eddieoneverything.com/?p=991</guid>
		<description><![CDATA[I&#8217;ve been doing a lot of Perl development for my latest project, and I wanted to use a single config file for many scripts.
Using a single config file in a basic Perl script is pretty straightforward &#8211; you simply use a require statement to read the external file, and the variables contained within are automatically [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing a lot of Perl development for my latest project, and I wanted to use a single config file for many scripts.</p>
<p>Using a single config file in a basic Perl script is pretty straightforward &#8211; you simply use a require statement to read the external file, and the variables contained within are automatically imported.  (Though if you&#8217;re using strict, you&#8217;ll need to declare them using the &#8220;our&#8221; keyword.)</p>
<p>A problem arises when you want to use those global variables in a package file, though &#8211; because a Package has its own namespace, variables declared in your external config file are not imported into your module, even if you use the &#8220;our&#8221; keyword.</p>
<p>Fortunately, I was able to find a solution <a href="http://home.arcor.de/pangj/share_variables_between_perl_scripts.txt" target="_blank">here</a>, courtesy of Jeff Pang.</p>
<p>Here are a couple of examples.</p>
<p><strong>The simple way to share variables between Perl scripts using &#8220;require&#8221;</strong></p>
<p>File test.include.pl</p>
<pre name="code" class="perl">
#!/usr/bin/perl
$var = ".j.a.p.h.";
</pre>
<p>File test.pl</p>
<pre name="code" class="perl">
#!/usr/bin/perl
use strict;
require "test.include.pl";
our $var;
print "VAR is $var\n";
</pre>
<p>After creating the two files above and running test.pl, the output is:<br />
<code><br />
eddie@widmers:~/wrk/$ ./test.pl<br />
VAR is .j.a.p.h.<br />
</code></p>
<p><strong>The more advanced way of sharing variables with a Perl Package</strong><br />
This method involves declaring the config file as a package, and then explicitly exporting the variables you&#8217;re interested in.   Your other scripts and modules will then import those variables, making them available in that scope. </p>
<p>In this case, the config variable package will look something like this:</p>
<pre name="code" class="perl">
package SiteConfig;
require Exporter;

our @ISA = qw(Exporter);
our @EXPORT=qw($var $vare);

our $var = ".j.a.p.h.";

1;
</pre>
<p>While the package you&#8217;re looking to use the variables in will look something like this:</p>
<pre name="code" class="perl">
package TestTest;
use TestConfig;
print "Var is $var\n";

sub new
{
   my $package = shift;
   print "VAR is $var\n";
  return bless({}, $package);
}
1;
</pre>
<p>Of course, your particular needs will vary, so you&#8217;ll need to use your noodle to modify the above as necessary.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.eddieoneverything.com/linux/argument-list-too-long-from-rm-command-a-perl-script-to-get-around-the-problem.php" title="&#8220;Argument list too long&#8221; from rm command.   A Perl Script to Get Around the Problem">&#8220;Argument list too long&#8221; from rm command.   A Perl Script to Get Around the Problem</a></li><li><a href="http://www.eddieoneverything.com/articles/managing-server-load-when-unzipping-many-large-files.php" title="Managing Server Load when unzipping many large files">Managing Server Load when unzipping many large files</a></li><li><a href="http://www.eddieoneverything.com/articles/how-to-scan-a-computer-for-open-ports.php" title="How to Scan a Computer for Open Ports">How to Scan a Computer for Open Ports</a></li><li><a href="http://www.eddieoneverything.com/articles/the-zen-of-taco-bell-programming-using-unix-tools-to-prevent-reinventing-the-wheel.php" title="The Zen of Taco Bell Programming &#8211; Using Unix Tools to Prevent Reinventing the Wheel">The Zen of Taco Bell Programming &#8211; Using Unix Tools to Prevent Reinventing the Wheel</a></li><li><a href="http://www.eddieoneverything.com/programming/getting-phps-print_r-function-to-return-a-string.php" title="Getting PHP&#8217;s print_r Function To Return A String">Getting PHP&#8217;s print_r Function To Return A String</a></li><li><a href="http://www.eddieoneverything.com/programming/calibre-recipe-for-minneapolis-star-tribune-startribune-com.php" title="Calibre Recipe for Minneapolis Star Tribune (StarTribune.com)">Calibre Recipe for Minneapolis Star Tribune (StarTribune.com)</a></li><li><a href="http://www.eddieoneverything.com/linux/rotating-backup-directories-using-cp-al-hardlinks-to-save-disk-space.php" title="Rotating Backup Directories using cp -al (hardlinks) to Save Disk Space">Rotating Backup Directories using cp -al (hardlinks) to Save Disk Space</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.eddieoneverything.com/programming/how-to-use-global-variables-from-another-file-in-a-perl-module-or-package.php/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rotating Backup Directories using cp -al (hardlinks) to Save Disk Space</title>
		<link>http://www.eddieoneverything.com/linux/rotating-backup-directories-using-cp-al-hardlinks-to-save-disk-space.php</link>
		<comments>http://www.eddieoneverything.com/linux/rotating-backup-directories-using-cp-al-hardlinks-to-save-disk-space.php#comments</comments>
		<pubDate>Sun, 15 Nov 2009 20:13:16 +0000</pubDate>
		<dc:creator>eddie</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[system administration]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.eddieoneverything.com/?p=356</guid>
		<description><![CDATA[The copy command &#8220;cp -al&#8221; found on all versions of Unix/Linux creates what&#8217;s called a &#8220;hard link&#8221; to a file. The nice thing about this command is that it doesn&#8217;t create an actual copy of the file on disk &#8211; instead, it creates a &#8220;link&#8221; or pointer to the file data on the disk.  [...]]]></description>
			<content:encoded><![CDATA[<p>The copy command &#8220;cp -al&#8221; found on all versions of Unix/Linux creates what&#8217;s called a &#8220;hard link&#8221; to a file. The nice thing about this command is that it doesn&#8217;t create an actual copy of the file on disk &#8211; instead, it creates a &#8220;link&#8221; or pointer to the file data on the disk.  Basically a &#8220;snapshot&#8221; of that directory in-time.   The net result is that you can have 10 &#8220;copies&#8217; of a 10G file that only take up a total of 10G. </p>
<p>This nifty behavior makes cp -al, when combined with rsync,  ideal for backup systems.  One can use the cp -al command to take a &#8220;snapshot&#8221; of a given directory tree at a given time, at the expense of very little additional disk space.  I use this script in concert with my <a href="http://www.eddieoneverything.com/linux/using-rsync-ssh-to-backup-files-between-linux-machines-perl.php">rsync_backup.pl</a> script to keep 21 days of &#8220;snapshot&#8221; backups of each of my machines.</p>
<pre name="code" class="perl">

#!/usr/bin/perl
use POSIX;

# Rotates backup directories w/ cp -al (hardlinks)
# Deletes directories older than $KEEP_DAYS
# Runs each night ahead of backup process
# (c) 2009 eddie@eddieoneverything.com

$KEEP_DAYS=21;
$LOGFILE = "/var/log/rotate_backups";

@BACKUP_DIRS=(
   '/mnt/backup/hansel',
   '/mnt/backup/tiger',
   '/mnt/backup/june'
);

$ts = get_timestamp();

open hLOG, ">>$LOGFILE";
print hLOG "=" x 80, "\n";
print hLOG "Run START at " . `date` . "\n";
print hLOG "=" x 80, "\n";

## Do the rotation
print hLOG "Do today's rotation\n";
foreach $dir ( @BACKUP_DIRS ){
   print hLOG "\t" , `date`;
   $newfn = $dir ."_" .  $ts;
   $cmd = "cp -al $dir $newfn";
   print hLOG "Execute Command: $cmd\n";
   `$cmd`;
}

## Delete old directories
print hLOG "Delete Old Directories\n";
foreach $dir ( @BACKUP_DIRS ){
   $dir=~m/^(.+)\/(.+?)$/;
   $base= $1;
   $stub=$2;
   #print "dir is $dir\nBASE: $base\nSTUB:$stub\n";

   opendir hDIR, "$base" or die "can't open directory $base";
   @dirlist=grep { /^$stub\_/ &#038;&#038; !/^$stub$/  } readdir(hDIR);
   closedir hDIR;

   foreach $d (@dirlist){
      print hLOG "\t" , `date`;
      $d=~/$stub\_([0-9]+)_([0-9]+)_([0-9]+)_.+$/;
      $year= $1;
      $month = $2;
      $day = $3;

      if (dirIsOlder($year, $month, $day)){
         #print "$d\n";
         $remove_dir= $base . '/' . $d;
         $cmd = "rm -Rf \"$remove_dir\"";
         print hLOG "Execute command: $cmd\n";
         `$cmd`;
      }else{
         print hLOG "Keep $d\n";
      }
   }
}
print hLOG "=" x 80, "\n";
print hLOG "Run END at " . `date` . "\n";
print hLOG "=" x 80, "\n";
close hLOG;

# ------------- Subroutines &#038; functions -----------------

sub get_timestamp {
   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
   $mon+=1;

   if ($mon < 10) { $mon = "0$mon"; }
   if ($mday < 10) { $mday = "0$mday"; }
   if ($hour < 10) { $hour = "0$hour"; }
   if ($min < 10) { $min = "0$min"; }
   if ($sec < 10) { $sec = "0$sec"; }
   $year=$year+1900;

   return $year . '_' . $mon . '_' . $mday . '__' . $hour . '_' . $min . '_' . $sec;
}

sub dirIsOlder{
   ($fyear, $fmonth, $fday) = @_;
   #print "Check $year - $month - $day \n";

   $now  = mktime(localtime());
   $then =  mktime (0, 0 , 0, $fday-1, $fmonth-1, $fyear-1900 , 0, 0);

   $diff_sec = $now - $then;
   $days_since = $diff_sec / 24 / 60 / 60;
   #print "n: $now. t: $then. Diff ($diff_sec) = $days_since\n";
   #print "$days_since $fyear-$fmonth-$fday\n";

   ## Subtract days from today

   if ($days_since > $KEEP_DAYS){
      return 1;
   }else{
      return 0;
   }
}
</pre>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.eddieoneverything.com/linux/using-rsync-ssh-to-backup-files-between-linux-machines-perl.php" title="Using rsync &#038; ssh to Backup Files Between Linux Machines (Perl)">Using rsync &#038; ssh to Backup Files Between Linux Machines (Perl)</a></li><li><a href="http://www.eddieoneverything.com/linux/argument-list-too-long-from-rm-command-a-perl-script-to-get-around-the-problem.php" title="&#8220;Argument list too long&#8221; from rm command.   A Perl Script to Get Around the Problem">&#8220;Argument list too long&#8221; from rm command.   A Perl Script to Get Around the Problem</a></li><li><a href="http://www.eddieoneverything.com/articles/managing-server-load-when-unzipping-many-large-files.php" title="Managing Server Load when unzipping many large files">Managing Server Load when unzipping many large files</a></li><li><a href="http://www.eddieoneverything.com/articles/how-to-scan-a-computer-for-open-ports.php" title="How to Scan a Computer for Open Ports">How to Scan a Computer for Open Ports</a></li><li><a href="http://www.eddieoneverything.com/articles/the-zen-of-taco-bell-programming-using-unix-tools-to-prevent-reinventing-the-wheel.php" title="The Zen of Taco Bell Programming &#8211; Using Unix Tools to Prevent Reinventing the Wheel">The Zen of Taco Bell Programming &#8211; Using Unix Tools to Prevent Reinventing the Wheel</a></li><li><a href="http://www.eddieoneverything.com/articles/linux-forward-file-not-working-make-sure-you-have-the-permissions-set-correctly.php" title="Linux .forward file not working?  Make sure you have the permissions set correctly">Linux .forward file not working?  Make sure you have the permissions set correctly</a></li><li><a href="http://www.eddieoneverything.com/linux/caution-filename-not-matched-error-when-unzipping-multiple-files-workaround.php" title="Caution: filename not matched error when unzipping multiple files workaround">Caution: filename not matched error when unzipping multiple files workaround</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.eddieoneverything.com/linux/rotating-backup-directories-using-cp-al-hardlinks-to-save-disk-space.php/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using rsync &amp; ssh to Backup Files Between Linux Machines (Perl)</title>
		<link>http://www.eddieoneverything.com/linux/using-rsync-ssh-to-backup-files-between-linux-machines-perl.php</link>
		<comments>http://www.eddieoneverything.com/linux/using-rsync-ssh-to-backup-files-between-linux-machines-perl.php#comments</comments>
		<pubDate>Sun, 15 Nov 2009 19:27:32 +0000</pubDate>
		<dc:creator>eddie</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[rsync]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[system administration]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.eddieoneverything.com/?p=342</guid>
		<description><![CDATA[This script performs a network backup using unix&#8217;s rsync command over ssh.  It performs an incremental backup, which means that it only updates new files and files that have changed since the last backup, thus minimizing network traffic.  
This script runs on the source machine (SOURCE) and connects to the backup machine (BACKUP). [...]]]></description>
			<content:encoded><![CDATA[<p>This script performs a network backup using unix&#8217;s rsync command over ssh.  It performs an incremental backup, which means that it only updates new files and files that have changed since the last backup, thus minimizing network traffic.  </p>
<p>This script runs on the source machine (SOURCE) and connects to the backup machine (BACKUP).  (SOURCE->BACKUP). It assumes a user named backup-SOURCE on the backup machine and connects as that user. </p>
<p>One advantage of using SOURCE->BACKUP setup rather than a BACKUP->SOURCE method is that if your BACKUP server is compromised, the attacker will not be able to gain access to all of your production machines.</p>
<p>There are admittedly disadvantages to running a SOURCE->BACKUP setup, rather than a BACKUP->SOURCE setup.  One, you&#8217;ll need a copy of the script on each of the machines that you want to backup, making maintenance a bit more involved.  Second, if your SOURCE machine becomes compromised, the attacker will be able to gain access to your backup server. This second issue can be mitigated by setting the permissions properly on the BACKUP machine.  (This is why I use a separate user account for each machine that I backup.)</p>
<p>How to use this script:</p>
<ol>
<li>Edit the configuration options in the script.</li>
<li>Add a user called &#8220;backup-{$SOURCE} to the BACKUP machine.</li>
<li>Set up the appropriate SSH keys for the user to bypass password prompts.</li>
<li>Set up a cron job to run the script nightly.</li>
<li>If you want Apple-ish &#8220;time machine style&#8221; backups, set up a script to rotate the backups on the BACKUP machine.  I use this <a href="http://www.eddieoneverything.com/linux/rotating-backup-directories-using-cp-al-hardlinks-to-save-disk-space.php">rotate_backups.pl</a> script to make backups using cp -al (hardlinks).</li>
</ol>
<pre name="code" class="pl">

#!/usr/bin/perl
# Filename: rsync_backup.pl
# Will back things up from one computer to another over SSH using rsync.
# Use in conjunction with a rotate script on the destination machine for
# time-machine-like incremental backups.
# Note: SSH SSL auth keys must be installed on each end to avoid passwd prompt
# Assumes a user named "backup-$THIS_COMPUTER" on remote machine.
# All output will go to Logfile
#
# (c) 2009 eddie@eddieoneverything.com 

use Data::Dumper;

# This computer's name.  Used to name the backup files.
$THIS_COMPUTER = "yoakam";

# The remote machine's URL or IP address.
$REMOTE_MACHINE = "127.0.0.1";

# The backup directory on the remote machine
$REMOTE_BACKUP_LOCATION="/mnt/backup";

# Log things
$LOGFILE = "/var/log/rsync_backup";

# Directories to backup.
@BACKUP_DIRS=(
   '/home',
   '/root',
   '/etc',
   '/var/log',
   '/var/lib/mysql',
   '/var/spool/cron'
);

# Directories to ignore.
@IGNORE_DIRS=(
   '/home/lost+found',
   '/home/tmp',
   '/home/virtfs'
);

#####################################################
########### END Configuration options ###############
#####################################################

open hLOG, "&gt;&gt;$LOGFILE";
$now = `date`;
print hLOG "-" x 80, "\n";
print hLOG "START BACKUP $THIS_COMPUTER at $now\n" ;
print hLOG "-" x 80, "\n";
# Get a list of dirs to back up
foreach (@BACKUP_DIRS){
   $dir = $_;
   opendir(DIR, $dir) || alert ("cant opendir $dir: $!");
   while ($file = readdir(DIR)){
      if ($file ne '.' &amp;&amp; $file ne '..'){
         $my_file = $dir . "/" .  $file;
         if ( -d $my_file){
            if (grep {/$my_file/} @IGNORE_DIRS){
               print hLOG "Ignore Dir: $my_file\n";
            }else{
               print hLOG "Dir: $my_file\n";
               push @BACKUP_LIST, {'dir' =&gt; $dir, 'name'=&gt;$file};
            }
         }else{
            print hLOG "file is $my_file -- \n";
            @arr_temp =  ($dir, $my_file);
            push @BACKUP_LIST, {'dir' =&gt; $dir, 'name'=&gt;$file};
         }
      }
   }
   closedir DIR;
}

#print Dumper(@BACKUP_LIST);

#Back up each dir in the backup list
foreach $my_entry (@BACKUP_LIST){
   $dir = $my_entry-&gt;{'dir'};
   $b = $my_entry-&gt;{'name'};

   $cmd = "rsync -avz --delete-after -e ssh \"$dir/$b\" backup-$THIS_COMPUTER\@$REMOTE_MACHINE:\"/$REMOTE_BACKUP_LOCATION/$THIS_COMPUTER$dir\"";
   $now=`date`;
   print hLOG "\n$now\t$cmd ";
   #print "$cmd\n";
   `$cmd &gt;&gt; $LOGFILE 2&gt;&amp;1`;
}

$now = `date`;
print hLOG "-" x 80, "\n";
print hLOG "END BACKUP of $THIS_COMPUTER at $now\n" ;
print hLOG "-" x 80, "\n";
close hLOG;

###################### Subs #########################
sub alert(){
   $a=shift(@_);
   print hLOG "#############################\n";
   print hLOG "#############################\n";
   print hLOG "#############################\n";
   print hLOG "Cannot backup $a $!\n";
   print hLOG "#############################\n";
   print hLOG "#############################\n";
   print hLOG "#############################\n";
}</pre>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.eddieoneverything.com/linux/rotating-backup-directories-using-cp-al-hardlinks-to-save-disk-space.php" title="Rotating Backup Directories using cp -al (hardlinks) to Save Disk Space">Rotating Backup Directories using cp -al (hardlinks) to Save Disk Space</a></li><li><a href="http://www.eddieoneverything.com/articles/managing-server-load-when-unzipping-many-large-files.php" title="Managing Server Load when unzipping many large files">Managing Server Load when unzipping many large files</a></li><li><a href="http://www.eddieoneverything.com/linux/argument-list-too-long-from-rm-command-a-perl-script-to-get-around-the-problem.php" title="&#8220;Argument list too long&#8221; from rm command.   A Perl Script to Get Around the Problem">&#8220;Argument list too long&#8221; from rm command.   A Perl Script to Get Around the Problem</a></li><li><a href="http://www.eddieoneverything.com/articles/how-to-scan-a-computer-for-open-ports.php" title="How to Scan a Computer for Open Ports">How to Scan a Computer for Open Ports</a></li><li><a href="http://www.eddieoneverything.com/articles/the-zen-of-taco-bell-programming-using-unix-tools-to-prevent-reinventing-the-wheel.php" title="The Zen of Taco Bell Programming &#8211; Using Unix Tools to Prevent Reinventing the Wheel">The Zen of Taco Bell Programming &#8211; Using Unix Tools to Prevent Reinventing the Wheel</a></li><li><a href="http://www.eddieoneverything.com/programming/how-to-easily-install-a-perl-module-in-ubuntu-linux.php" title="How to Easily Install a Perl Module In Ubuntu Linux">How to Easily Install a Perl Module In Ubuntu Linux</a></li><li><a href="http://www.eddieoneverything.com/articles/linux-forward-file-not-working-make-sure-you-have-the-permissions-set-correctly.php" title="Linux .forward file not working?  Make sure you have the permissions set correctly">Linux .forward file not working?  Make sure you have the permissions set correctly</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.eddieoneverything.com/linux/using-rsync-ssh-to-backup-files-between-linux-machines-perl.php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8220;Argument list too long&#8221; from rm command.   A Perl Script to Get Around the Problem</title>
		<link>http://www.eddieoneverything.com/linux/argument-list-too-long-from-rm-command-a-perl-script-to-get-around-the-problem.php</link>
		<comments>http://www.eddieoneverything.com/linux/argument-list-too-long-from-rm-command-a-perl-script-to-get-around-the-problem.php#comments</comments>
		<pubDate>Sun, 15 Nov 2009 09:28:34 +0000</pubDate>
		<dc:creator>eddie</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rm]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.eddieoneverything.com/?p=329</guid>
		<description><![CDATA[You&#8217;ve seen the problem &#8211; many Unix commands don&#8217;t like very long argument lists.  For instance, my Linux machine complains when I try to &#8220;rm&#8221; 10,000 files at once:
[eddie@yoakam test]$ rm *
-bash: /bin/rm: Argument list too long
Here&#8217;s a simple script that will delete all of the files in a given directory, even if the argument [...]]]></description>
			<content:encoded><![CDATA[<p>You&#8217;ve seen the problem &#8211; many Unix commands don&#8217;t like very long argument lists.  For instance, my Linux machine complains when I try to &#8220;rm&#8221; 10,000 files at once:</p>
<div class="q"><strong>[eddie@yoakam test]</strong>$ rm *<br />
-bash: /bin/rm: Argument list too long</div>
<p>Here&#8217;s a simple script that will delete all of the files in a given directory, even if the argument list gets really long. I prefer to keep it in my ~/bin directory named &#8220;bigrm&#8221;.</p>
<p>My bigrm Perl script:</p>
<pre name="code" class="perl">
#!/usr/bin/perl

if (!$ARGV[0]){
   print "Usage:  $0 \n";
   print "Will delete all files in specified directory\n";
   exit(1);
}

print "deleting files from $d\n";
$OUTDIR=$ARGV[0];

opendir(DIR, $OUTDIR) || die ("cant opendir $IMAGE_DIR: $!");
print "OPENED $OUTDIR\n";
while ($file = readdir(DIR)){
   if ($file!~/^\./){
      print "unlink $file\n";
      unlink ("$OUTDIR/$file");
   }
}
closedir(DIR)</pre>
<p>This is useful for cleaning out directories of temp files, of snapshot directories, etc.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.eddieoneverything.com/articles/how-to-scan-a-computer-for-open-ports.php" title="How to Scan a Computer for Open Ports">How to Scan a Computer for Open Ports</a></li><li><a href="http://www.eddieoneverything.com/articles/the-zen-of-taco-bell-programming-using-unix-tools-to-prevent-reinventing-the-wheel.php" title="The Zen of Taco Bell Programming &#8211; Using Unix Tools to Prevent Reinventing the Wheel">The Zen of Taco Bell Programming &#8211; Using Unix Tools to Prevent Reinventing the Wheel</a></li><li><a href="http://www.eddieoneverything.com/linux/rotating-backup-directories-using-cp-al-hardlinks-to-save-disk-space.php" title="Rotating Backup Directories using cp -al (hardlinks) to Save Disk Space">Rotating Backup Directories using cp -al (hardlinks) to Save Disk Space</a></li><li><a href="http://www.eddieoneverything.com/articles/managing-server-load-when-unzipping-many-large-files.php" title="Managing Server Load when unzipping many large files">Managing Server Load when unzipping many large files</a></li><li><a href="http://www.eddieoneverything.com/linux/using-rsync-ssh-to-backup-files-between-linux-machines-perl.php" title="Using rsync &#038; ssh to Backup Files Between Linux Machines (Perl)">Using rsync &#038; ssh to Backup Files Between Linux Machines (Perl)</a></li><li><a href="http://www.eddieoneverything.com/programming/how-to-easily-install-a-perl-module-in-ubuntu-linux.php" title="How to Easily Install a Perl Module In Ubuntu Linux">How to Easily Install a Perl Module In Ubuntu Linux</a></li><li><a href="http://www.eddieoneverything.com/articles/linux-forward-file-not-working-make-sure-you-have-the-permissions-set-correctly.php" title="Linux .forward file not working?  Make sure you have the permissions set correctly">Linux .forward file not working?  Make sure you have the permissions set correctly</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.eddieoneverything.com/linux/argument-list-too-long-from-rm-command-a-perl-script-to-get-around-the-problem.php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Easily Install a Perl Module In Ubuntu Linux</title>
		<link>http://www.eddieoneverything.com/programming/how-to-easily-install-a-perl-module-in-ubuntu-linux.php</link>
		<comments>http://www.eddieoneverything.com/programming/how-to-easily-install-a-perl-module-in-ubuntu-linux.php#comments</comments>
		<pubDate>Fri, 13 Nov 2009 21:25:40 +0000</pubDate>
		<dc:creator>eddie</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.eddieoneverything.com/?p=244</guid>
		<description><![CDATA[Wow, I think I just found the best tip EVER.
I had to install a perl module (WWW::Mechanize) and was stuck in a serious dispute with CPAN over dependencies.  Wow, what a mess.
Fortunately I found this post by David Yin about how to use Debian&#8217;s totally awesome apt-get command to install the module.
Steps:
1.  Search the apt-get [...]]]></description>
			<content:encoded><![CDATA[<p>Wow, I think I just found the best tip EVER.</p>
<p>I had to install a perl module (WWW::Mechanize) and was stuck in a serious dispute with CPAN over dependencies.  Wow, what a mess.</p>
<p>Fortunately I found <a href="http://www.yinfor.com/blog/archives/2007/04/perl_module_install_under_ubun.html" target="_blank">this post</a> by David Yin about how to use Debian&#8217;s totally awesome apt-get command to install the module.</p>
<p>Steps:</p>
<p>1.  Search the apt-get cache to find the package name of the module.</p>
<blockquote><p><strong>eddie@yoakam#</strong> sudo apt-cache search perl WWW::Mechanize<br />
libwww-mechanize-shell-perl &#8211; An interactive shell for WWW::Mechanize</p></blockquote>
<p>2.   Once found, install the module using apt-get.</p>
<blockquote><p><strong>eddie@yoakam#</strong> sudo apt-get install libwww-mechanize-shell-perl</p></blockquote>
<p>Presto &#8211; this simple trick took just 30 seconds to accomplish what I&#8217;d just spent half an hour trying to get CPAN to do.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://www.eddieoneverything.com/linux/argument-list-too-long-from-rm-command-a-perl-script-to-get-around-the-problem.php" title="&#8220;Argument list too long&#8221; from rm command.   A Perl Script to Get Around the Problem">&#8220;Argument list too long&#8221; from rm command.   A Perl Script to Get Around the Problem</a></li><li><a href="http://www.eddieoneverything.com/articles/managing-server-load-when-unzipping-many-large-files.php" title="Managing Server Load when unzipping many large files">Managing Server Load when unzipping many large files</a></li><li><a href="http://www.eddieoneverything.com/articles/how-to-scan-a-computer-for-open-ports.php" title="How to Scan a Computer for Open Ports">How to Scan a Computer for Open Ports</a></li><li><a href="http://www.eddieoneverything.com/articles/the-zen-of-taco-bell-programming-using-unix-tools-to-prevent-reinventing-the-wheel.php" title="The Zen of Taco Bell Programming &#8211; Using Unix Tools to Prevent Reinventing the Wheel">The Zen of Taco Bell Programming &#8211; Using Unix Tools to Prevent Reinventing the Wheel</a></li><li><a href="http://www.eddieoneverything.com/linux/rotating-backup-directories-using-cp-al-hardlinks-to-save-disk-space.php" title="Rotating Backup Directories using cp -al (hardlinks) to Save Disk Space">Rotating Backup Directories using cp -al (hardlinks) to Save Disk Space</a></li><li><a href="http://www.eddieoneverything.com/linux/using-rsync-ssh-to-backup-files-between-linux-machines-perl.php" title="Using rsync &#038; ssh to Backup Files Between Linux Machines (Perl)">Using rsync &#038; ssh to Backup Files Between Linux Machines (Perl)</a></li><li><a href="http://www.eddieoneverything.com/articles/linux-forward-file-not-working-make-sure-you-have-the-permissions-set-correctly.php" title="Linux .forward file not working?  Make sure you have the permissions set correctly">Linux .forward file not working?  Make sure you have the permissions set correctly</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.eddieoneverything.com/programming/how-to-easily-install-a-perl-module-in-ubuntu-linux.php/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

