php forum
php mysql forum
php mysql smarty
 
Topic Options
#105050 - 11/15/05 12:53 PM Help with script
1QuickSI Offline
Coder

Registered: 06/10/01
Posts: 705
Loc: CT
I run a small Perl script to back up my UBB directory and MySQL database. I would like to make an addition that would chack for space so that if X amount of space is not free the back up would abort then send me an email.

Here is what I have so far... Can someone give me a hand or point me in the right direction? Still very poor in writting code frown

Code:
 
#!/usr/bin/perl
 
# System backup
 
#------ Variables ------
$backupdir = "/usr/local/pwrhouse"; # "." for current
$other_dirs = "/usr/local/hostboard/mainboard/cgi-bin /usr/local/hostboard/mainboard/htdocs";
#-----------------------
print "Backup Initiated...n";
@timenow = localtime(time);
 
$newfilename = sprintf("%02d",$timenow[4]+1) .
sprintf("%02d",$timenow[3]) . ($timenow[5] + 1900);
print "Using Filename: $newfilename.sql n";
print "Starting database backup... n";
system "mysqldump -qce -r $newfilename.sql -u root ubb";
print "Starting Compression... n";
system "tar czf $backupdir/$newfilename.tar.gz $other_dirs $newfilename.sql";
unlink "$newfilename.sql" || print "Error: Cannot delete file specified: $newfilename - $_ n";
print "Done. n"
_________________________
...Steven
Hostboard.com
Running UBB v6.1.0.4
Doing what NO other UBB in the world is doing.
The first & only production UBB powered by MySQL.

Top
#105051 - 11/21/05 11:07 AM Re: Help with script
Burak Offline
Addict

Registered: 05/29/00
Posts: 1820
Loc: Istanbul, Turkey
you need to know the disk quota for that operation. Currently I don't have access to a linux system and I don't remember the exact command, but there may be some Quota modules on CPAN. And to calculate the approximate dump size, you have to calculate row lengths. I don't know how to get that value from command line, but this query will return the data:

Code:
SHOW TABLE STATUS FROM <DATABASE_NAME>;
this'll return a list with a lot of keys. "Data_length" is the one you are looking for. Here is a perl/DBI example:

Code:
use DBI;
my $dbname = "your_db_name";
my $dbh = DBI->connect('DBI:mysql:'.$dbname, 'root', '',{RaiseError => 1});
my $sth = $dbh->prepare("SHOW TABLE STATUS FROM $dbname");
$sth->execute;
my $size = 0;
while (my $stat = $sth->fetchrow_hashref) {
   $size += $stat->{Data_length};
}
$dbh->disconnect;
printf "Total data size: %.2f KB", $size / 1024;
here $size gives you an approximate value, because a db dump will include more information than data and a lot of insert() etc statements... But it'll give you a value to compare:

Code:
$space = $quota - $disk_space_I_currently_use;
# $xtra can be a constant value for insert() statements
if ($space > $size+$xtra) {
   # do backup
} else {
   # don't
}
_________________________
one bytecode to rule them all

Top
#105052 - 11/21/05 09:36 PM Re: Help with script
Gizmo Online   shocked

Wizard

Registered: 01/10/00
Posts: 5354
Loc: Portland, OR, USA
Quote:
quote:
[root@server ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/hdv1 10G 4.4G 5.7G 44% /
_________________________
UBB.Dev - Where you too can render your UBB install completely useless...
UGN Security, Elite Web Gamers & VNC Web Design & Development President
UBB.Threads: My UBBSkins, UBB.Sitemaps

Top



Latest Posts
[7.2.1] - Naked shoutbox
by bellaonline
05/05/12 05:00 PM
[7.x] Stop Forum Spam Integration v0.4
by bellaonline
05/05/12 03:53 PM
Shout Box

(Views)Popular Topics
Known public proxy servers 1689885
Integrated Index Page (IIP) 5.3.1 555705
Finished-[6.5.2] Games Arcade Deluxe v1.9 501236
Integrated Index Page (IIP) 5.1.1 415112
TLD Bv2.1 Released - Threads Links Directory 396822
[6.0x] Who's Online 4.0.0 [Finished] 389412
Finished-[6.5.1] Integrated Index Page (IIP) 6.5 330423
Q & A 298663
Slash UBB 266936
[6.3.x] [beta] Hit Hack 2.0 227970
Forum Stats
13621 Members
59 Forums
37191 Topics
295716 Posts

Max Online: 686 @ 06/28/07 07:04 AM

 

 

 
fusionbb message board php hacks