3

I'm using the exact code that 1and1 suggests for backing up MySQL DBs through mysqldump (http://faq.1and1.co.uk/archive/43.html)

$host= 'xxxx';
$user= 'xxxx';
$pass= 'xxxx';
$db=   'xxxx';

system(sprintf(
  'mysqldump --opt -h%s -u%s -p%s %s | gzip > %s/backup/' . time() . '.sql.gz',
  $host,
  $user,
  $pass,
  $db,
  getenv('DOCUMENT_ROOT')
));

Have changed all permissions to 777 for purposes of testing. Still no luck. Continuing to get the error (errno 32 on write).

Any help is appreciated - this seems like a stupid fix.

Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
psarid
  • 450
  • 7
  • 19

1 Answers1

4

If you're doing this from a cron job, then DOCUMENT_ROOT won't exist. The DOCUMENT_ROOT and other $_SERVER variables are effectively set up by the web server (see the $_SERVER manual page), and if you're running the script directly from cron, there's no web server involved. See this earlier, similar question.

Try setting the path to your backup directory relative to the location of the PHP script. For example, if your script is in .../your_folder/scripts/backup_script.php, and your (pre-created) backup directory is .../your_folder/backup/, then

$backup_dir = dirname(__FILE__) . '/../backup';

...and then use $backup_dir instead of getenv('DOCUMENT_ROOT').

It looks like your hosts's instructions are correct, but only if running the script from the web server, not from the command line.

Community
  • 1
  • 1
Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
  • 1
    This makes the error make more sense: errno 32 is "broken pipe", meaning that gzip exited before mysqldump finished output. gzip falling over because it could not write to the output file sounds like a good culprit. – Charles Mar 21 '11 at 16:28
  • @Charles Indeed. In the case of DOCUMENT_ROOT being empty, gzip will be trying to write to `/backup/...`, which you'd very much hope a normal user wouldn't have permission to do :) – Matt Gibson Mar 21 '11 at 16:32