backup.php 772 Bytes
Newer Older
dmorley's avatar
backup  
dmorley committed
1
<?php
2

3 4 5 6 7 8
/**
 * Backup / dump PostgreSQL database.
 */

declare(strict_types=1);

9
if (PHP_SAPI !== 'cli') {
10 11
    header('HTTP/1.0 403 Forbidden');
    exit;
12 13
}

14 15
require_once __DIR__ . '/../config.php';

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