backup.php 743 Bytes
Newer Older
dmorley's avatar
backup  
dmorley committed
1
<?php
2 3 4 5 6 7

if (PHP_SAPI !== 'cli') {
  header('HTTP/1.0 403 Forbidden');
  exit;
}

8 9
require_once __DIR__ . '/../config.php';

10
$keep      = (60 * 60 * 6) * 1;
11
$dump_date = date('Ymd_Hs');
David Morley's avatar
David Morley committed
12
$file_name = __DIR__ . '/..' .$backup_dir . '/dump_' . $dump_date . '.sql';
13 14
system("export PGPASSWORD=$pgpass && $pg_dump_dir/pg_dump --username=$pguser $pgdb >> $file_name");
echo "pg backup of $pgdb made";
David Morley's avatar
David Morley committed
15
$dirh = dir(__DIR__ . '/..' .$backup_dir);
16
while ($entry = $dirh->read()) {
17
  $old_file_time = (date('U') - $keep);
David Morley's avatar
David Morley committed
18
  $file_created  = filectime(__DIR__ . '/..' . "$backup_dir/$entry");
dmorley's avatar
cleanup  
dmorley committed
19
  if ($file_created < $old_file_time && !is_dir($entry)) {
David Morley's avatar
David Morley committed
20
    if (unlink(__DIR__ . '/..' . "$backup_dir/$entry")) {
21
      echo 'Cleaned up old backups';
dmorley's avatar
cleanup  
dmorley committed
22 23
    }
  }
dmorley's avatar
backup  
dmorley committed
24
}