source: trunk/scripts/utilities.php @ 1184

Revision 1113, 2.8 KB checked in by victorsb, 2 years ago (diff)

Added a script to delete old backups in such a manner as to maintain
backups to every 2x days (approximately) before the current one.
It has to be run every day after the daily backup script.

REVIEW URL http://reviewboard.infoarena.ro/r/136/

  • Property svn:eol-style set to native
Line 
1<?php
2
3// Find path.
4$curdir = dirname($argv[0]);
5
6require_once($curdir . "/../config.php");
7require_once(IA_ROOT_DIR . "common/log.php");
8require_once(IA_ROOT_DIR . "common/common.php");
9require_once(IA_ROOT_DIR . "common/db/db.php");
10
11if (realpath(IA_ROOT_DIR . 'scripts') != realpath($curdir)) {
12    log_error("You should only include this file from scripts");
13}
14
15check_requirements();
16
17// Asks the user a question.
18// $default is the default answer
19function read_line($question, $default = null) {
20    if ($default === null) {
21        echo "$question ";
22    } else {
23        echo "$question"." (default:$default) ";
24    }
25    $r = trim(fgets(STDIN));
26    if ($r == "") {
27        $r = $default;
28    }
29    return $r;
30}
31
32// Same as read_line, but returns true/false.
33// default must be true/false, or null.
34function read_bool($question, $default = null) {
35    while (true) {
36        if ($default === null) {
37            $answer = read_line($question);
38        } else if ($default) {
39            $answer = read_line($question, "yes");
40        } else {
41            $answer = read_line($question, "no");
42        }
43        if (preg_match("/^(true|y|yes|da)$/i", $answer)) {
44            return true;
45        }
46        if (preg_match("/^(false|n|no|nu)$/i", $answer)) {
47            return false;
48        }
49        echo "Answer with true/false/yes/no/etc.\n";
50    }
51}
52
53// Magically execute a $cmd.
54// $cmd takes over the entire process.
55// This is a lot harder than it sounds.
56function magic_exec($cmd)
57{
58    $argv = preg_split("/\s/", $cmd, -1, PREG_SPLIT_NO_EMPTY);
59
60    $prog = $argv[0];
61    // Only search path if no slashes.
62    if (strstr($prog, "/") === false) {
63        foreach (explode(':', getenv("PATH")) as $dir) {
64            $exe = realpath($dir . '/' . $prog);
65            //log_print("Try $exe.");
66            if ($exe !== false && is_executable($exe)) {
67                break;
68            }
69            $exe = null;
70        }
71    } else {
72        $exe = realpath($prog);
73        if (!is_executable($exe)) {
74            $exe = null;
75        }
76    }
77
78    if ($exe === null) {
79        log_error("Couldn't find '$prog' executable.");
80    } else {
81        pcntl_exec($exe, array_slice($argv, 1), $_ENV);
82    }
83}
84
85function temp_dir() {
86    if (function_exists('sys_get_temp_dir')) {
87        return sys_get_temp_dir();
88    } else {
89        return '/tmp';
90    }
91}
92
93function backup_timestamp() {
94    return date("Ymd");
95}
96
97function remove_old_files($dir, $keep_newest = 10) {
98    log_assert(is_dir($dir));
99    exec(sprintf("ls -t1 '%s'", $dir), $files);
100    $delete_files = array_slice($files, $keep_newest);
101    foreach ($delete_files as $file) {
102        unlink($file);
103    }
104}
105
106function is_backup_filename($filename, &$matches) {
107    $pattern = "/^db-(\d{4})(\d{2})(\d{2})\.sql\.gz\.gpg$/";
108    return preg_match($pattern, $filename, $matches);
109}
110
111?>
Note: See TracBrowser for help on using the repository browser.