Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 25th, 2001, 07:31 AM
kjedwards kjedwards is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 76 kjedwards User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m
Reputation Power: 8
Hi

Does anyone have a simple perl script I could use to backup a mySQL database ie to automate the mysqldump routine?

Many Thanks

Kevin

Reply With Quote
  #2  
Old March 29th, 2001, 10:04 AM
JonLed JonLed is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2000
Location: Indiana
Posts: 614 JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 10
Well you can do it a few ways. One way is to just backup the physical directory that olds all the databases (each database has its own directory within the main one). This is how mysql stores data, so if it became courpted or delete, you could literally just move the old directories into the place and everything would work fine again. Anther way is to pipe a myslq dump into a compression program to archive it.

I've done both ways before, but I find that just tar-ing up the mysql directory is the easiest way.

(note, this script tars the directory, ftp's it to a remote site and delete's the local copy)

Code:
#!/usr/bin/perl

# Backup stuff to a remote server

use Net::FTP;

# Create tmp tar file of the $ARGV[0] directory
my $dir = shift;
my $host = shift || 'ftp.remoteserver.com';
my $user = shift || 'username';
my $pass = shift || 'password';
my $remote_dir = shift || 'backups';

die "No directory to backup! - $dir\n" unless ($dir && -d $dir);

# Times
#  We need this so we can allow a weeks worth of backups
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time());

my $filename;

if($dir =~ m|([^/]+)$|) {
	$filename = $1 . '.' . $wday . '.tar.gz';
} else {
	$filename = 'tmp.'.$wday.'.tar.gz';
}

# To the tar
system("tar zcf /tmp/$filename $dir");

 my $ftp = Net::FTP->new($host, Debug => 0);
		$ftp->login($user, $pass);
			$ftp->cwd($remote_dir);
				$ftp->delete($filename);
			$ftp->binary;
		$ftp->put('/tmp/'.$filename);
	$ftp->quit;

unlink('/tmp/'.$filename);

Usage: /path/to/backup.pl /path/to/mysql/data/dir [ftpserver] [username] [password] [remote_dir] 2>/dev/null

the [ftpserver] [username] [password] and [remote] are optional (if you setup the script correctly.)

For example, this is what I have setup in the crontab (backs up 2 directories):
0 * * * * /usr/bin/perl /home/ledjon/crons/backupdir.pl /var/www 2>/dev/null
1 * * * * /usr/bin/perl /home/ledjon/crons/backupdir.pl /var/lib/mysql 2>/dev/null

the 2>/dev/null with put all the errors it might get (tar likes to throw errors on pointless stuff). This keeps you from getting crontab e-mails everyday with the tar messages.

Also note that this will keep backups for 7 days (7 days in a week!)

Reply With Quote
  #3  
Old March 29th, 2001, 03:03 PM
kjedwards kjedwards is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 76 kjedwards User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m
Reputation Power: 8
Backup Perl Scrit Please

Hi

Many thanks for that, but it don't think I have access to the mySQL directories where the database files are stored - it is hosted by an ISP (or I certainly don't know where they are!)

It is better for me to use mysqldump.

Sorry but my knowledge of perl is almost zero.

Can you post the equivalent script for using mysqldump?

I would be very grateful

Yours

Kevin

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Backup Perl Scrit Please


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway