|
|
Joined: Jun 2001
Posts: 729
Coder
|
Coder
Joined: Jun 2001
Posts: 729 |
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 #!/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"
|
|
|
|
Joined: May 2000
Posts: 1,356
Addict
|
Addict
Joined: May 2000
Posts: 1,356 |
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: 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: 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: $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 }
|
|
|
|
Joined: Jan 2000
Posts: 5,833 Likes: 20
UBBDev / UBBWiki Owner Time Lord
|
UBBDev / UBBWiki Owner Time Lord
Joined: Jan 2000
Posts: 5,833 Likes: 20 |
quote: [root@server ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/hdv1 10G 4.4G 5.7G 44% /
|
|
|
Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.
Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
|
|
Posts: 449
Joined: February 2008
|
|
Forums63
Topics37,575
Posts293,930
Members13,823
|
Most Online6,139 Sep 21st, 2024
|
|
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
|
|
|
|
|