Cron job - backup DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Cron job - backup DB

Post by fresh »

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 ;)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

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:

Code: Select all

> mysqldump -u user_with_access -p database_name > file_to_dump_to.sql
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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post by fresh »

yeah but will it still need to be manually?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Not if you turn that into a PHP program and cron it. ;)
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post by fresh »

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';
?>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Like a bunch of PHP code. :)
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post by fresh »

lol.. i meant did it look safe? I don't want to delete my DB or anything bad like that.. 8O
Post Reply