source: trunk/scripts/db-backup-delete @ 1184

Revision 1121, 2.9 KB checked in by bogdan2412, 2 years ago (diff)

Updated with changes from live.

  • Updated SMF to 1.1.11
  • Added Summify to site and forum
  • Small change to job detail view
Line 
1#! /usr/bin/env php
2<?php
3
4require_once(dirname($argv[0]) . "/utilities.php");
5
6// This script deletes unnecessary backups.
7// It assumes that backups are made daily.
8// This script should be run after every backup.
9// Backups will be held from every 2^x days before(approximately).
10
11// Parameters:  1. The path to the backups directory (compulsory) ex: /backups/
12//              2. Normal/Safe mode (optional) ex: safe
13
14// Important: Set the first date when the script will be run.
15define('START', mktime(0, 0, 0, 3, 21, 2010));
16
17
18function compare_by_timestamp($first, $second) {
19    if ($first["timestamp"] == $second["timestamp"]) {
20        return 0;
21    } else {
22        return ($first["timestamp"] > $second["timestamp"]) ? -1 : 1;
23    }
24}
25
26function days_between($timestamp1, $timestamp2) {
27    return round(abs($timestamp1 - $timestamp2) / 60 / 60 / 24);
28}
29
30// Validate the directory
31log_assert(isset($argv[1]), "Specify backup directory!");
32$dir_path = $argv[1];
33if ($dir_path[strlen($dir_path) - 1] != '/') {
34    $dir_path .= "/";
35}
36log_assert(is_dir($dir_path), "The argument is not a valid directory!\n");
37$dir = dir($dir_path);
38
39// Today's timestamp at 00:00
40$now = mktime(0, 0, 0);
41
42// Find the existing backups
43$backups = array();
44$backup_today = false;
45while (false !== ($file = $dir->read())) {
46    $matches = array();
47    if (is_file($dir->path . $file) && is_backup_filename($file, $matches)) {
48        $year = $matches[1];
49        $month = $matches[2];
50        $day = $matches[3];
51        $time = mktime(0, 0, 0, $month, $day, $year);
52        if ($time >= START) {
53            if ($now == $time) {
54                $backup_today = true;
55            } else {
56                $backups[] = array("timestamp" => $time, "filename" => $file);
57            }
58        }
59    }
60}
61usort($backups, "compare_by_timestamp");
62
63// Verifying previous backups
64log_assert($backup_today == true, "There was no backup made today.");
65if (count($backups) == 0) {
66    log_print("There are no previous backups to delete.");
67    exit;
68}
69for ($index = 1; $index < count($backups); $index++) {
70    $dday = days_between($backups[$index]["timestamp"], $now);
71    log_assert((pow(2, $index - 1) <= $dday && $dday <= pow(2, $index)),
72            "The current backups do not respect the backup schedule.");
73}
74
75// Finding any unnecessary backup
76$days_passed = days_between($now, START);
77if ($days_passed % pow(2, count($backups) - 1) == 0) {
78    $dfile = null;
79} else {
80    for ($index = count($backups) - 1; $index > 0; $index--) {
81        if ($days_passed % pow(2, $index - 1) == 0) {
82            $dfile = $backups[$index]["filename"];
83            break;
84        }
85    }
86}
87
88// Deleting the unnecessary backup
89if (isset($dfile)) {
90    log_print("Deleting file: ".$dfile);
91    if (!(isset($argv[2]) && $argv[2] == "safe")) {
92        unlink($dir->path . $dfile);
93    } else {
94        log_print("No files deleted. (safe mode)");
95    }
96} else {
97    log_print("Not deleting anything.");
98}
99
100?>
Note: See TracBrowser for help on using the repository browser.