LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[CLUG] script

[CLUG] script

adam adam at iewebs.com
Fri Jul 20 01:47:47 IST 2001


Hi Robert,

> I'm trying to write a script to backup a mysql database that I can then
> stick into my crontab to run regularily
>
> the command I want to execute is along the lines of this one:
>
> mysqldump -h myhost -u root -pmypass dbname > dbname_backup
>
> the trick is - I want multiple bacups and therefore a different filename
> each time, preferably related to the date.  so rather than dbname_backup
> it would be something like 20010720 and tomorrow it would be 20010721
> and so on.  Is there a way to do this in a bash script? (or any way at
> all for that matter)
>
> I have little to no experience with shell scripts so no doubt there is a
> profoundly easy way to do this.
>
I'm no good with shell scripts - never have been, never took the time to
learn - but I'm a total PHP (not PCP) addict and that's led me to do most of
my shell scripting in PHP these days. Oddly enough, it serves me well.
Here's what I'd do:

------------------------------------------------------------
#!/usr/bin/php
<?

// Configure options:
$host = "host";
$user = "root";
$pass = "pass";
$db   = "dbname";

// Generate a 12-character date in the format YYMMDDHHMMSS:
$date = date("ymdhis");

// (Or you could use a Unix timestamp:)
//$date = time();

// Give it a meaningful suffix
$file = "$date.sql";

// Execute the command.
exec("mysqldump -h $host -u $user -p$pass $db > $file");

?>
------------------------------------------------------------

All of which could be scrunched up in true Perl-fan fashion to this (may
wrap just to annoy me):

------------------------------------------------------------
#!/usr/bin/php
<? exec("mysqldump -h host -u user -ppass db > " . date("ymdhis") . ".sql");
?>
------------------------------------------------------------

With a little work, you could also get it to backup all databases on the
server, and if you're feeling clever you could transfer them to another
machine for safety using FTP or some other mechanism. Don't forget to set
your permissions properly though - you don't want some luser coming along
and getting an eyeful of your MySQL root password.

I love PHP.

HTH,
adam

____________________________________________

Adam Beecher - CEO & Core Geek - ieWebs™

e  ...  adam at iewebs.com
w  ...  http://www.iewebs.com
m  ...  Box 81, Eglinton St, Cork, Ireland
____________________________________________






More information about the Cork mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell