thanks in advance
Cron job - backup DB
Moderator: General Moderators
Cron job - backup DB
Hey, I have been reading a little about cron jobs and setting them up to do some useful things, but none have covered the concept of backing up a DB.. I know how to setup the server to run the PHP page at specific times, but the exporting to my local drive part has went over my head lol 
thanks in advance
thanks in advance
You don't need PHP to backup your database (probably). MySQL has a program called [mysql_man]mysqldump[/mysql_man] that can be used to backup a database. I'm not 100% sure on the syntax, but it's something like this:
It will ask for your password, and then start backing up. If you just want the structure and not the data, use a "-d" flag.
Code: Select all
> mysqldump -u user_with_access -p database_name > file_to_dump_to.sqlReal programmers don't comment their code. If it was hard to write, it should be hard to understand.
hey, I found this, hows this look?
Code: Select all
<?php
// Enter your MySQL access data
$host= 'dbhost';
$user= 'dbuser';
$pass= 'dbpassword';
$db= 'db';
$backupdir = 'backups';
// Compute day, month, year, hour and min.
$today = getdate();
$day = $today[mday];
if ($day < 10) {
$day = "0$day";
}
$month = $today[mon];
if ($month < 10) {
$month = "0$month";
}
$year = $today[year];
$hour = $today[hours];
$min = $today[minutes];
$sec = "00";
// Execute mysqldump command.
// It will produce a file named $db-$year$month$day-$hour$min.gz
// under $DOCUMENT_ROOT/$backupdir
system(sprintf(
'mysqldump --opt -h %s -u %s -p%s %s | gzip > %s/%s/%s-%s%s%s-%s%s.gz',
$host,
$user,
$pass,
$db,
getenv('DOCUMENT_ROOT'),
$backupdir,
$db,
$year,
$month,
$day,
$hour,
$min
));
echo '+DONE';
?>