source: trunk/scripts/backup @ 1184

Revision 1184, 3.3 KB checked in by bogdan2412, 4 weeks ago (diff)

Merged changes from live and a few extra

  • Updated backup script to include new configuration files after migration to HPHP
  • Copied over updated code from the textblock controller to the user controller in order to fix crash on invalid revision number
  • Fixed Twitter button
  • Fixed a bug with task test case grouping when string was too large to be stored in database
  • Add IA_HTTPS_ENABLED configuration option. Allows disableing of secure connection enforcement in development mode.
  • More user friendly strings for 'security' in userinfo macro.
  • Linting fixes
  • Property svn:executable set to *
Line 
1#! /usr/bin/env php
2<?php
3
4require_once(dirname($argv[0]) . '/utilities.php');
5
6define('IA_BACKUP_DIR', '/home/backup/ia-backup');
7define('IA_LIVE_DIR', '/home/infoarena');
8define('IA_BACKUP_USER', 'backup');
9// Passphrase file to use with GPG, leave blank to disable encryption.
10define('IA_BACKUP_DB_ENCRYPT_KEY', '/root/.backup-passphrase');
11
12$BACKUP_PATHS_WEEKLY = array(
13    'conf/conf-' => array(IA_LIVE_DIR.'/trac.htpasswd',
14                          IA_LIVE_DIR.'/nginx.conf',
15                          IA_LIVE_DIR.'/apache.conf',
16                          IA_ROOT_DIR.'/hphp/config.hdf',
17                          IA_ROOT_DIR.'/config.php'),
18    'hackers/svn/svn-' => array(IA_LIVE_DIR.'/svn'),
19    'hackers/trac/trac-' => array(IA_LIVE_DIR.'/trac'),
20    'hackers/reviewboard/reviewboard-' => array(IA_LIVE_DIR.'/reviewboard'),
21);
22
23function backup_init_dirs() {
24    log_assert(is_dir(IA_BACKUP_DIR), 'Invalid directory: '.IA_BACKUP_DIR);
25    log_assert(is_dir(IA_LIVE_DIR), 'Invalid directory: '.IA_LIVE_DIR);
26    $dirs = array('db', 'conf', 'hackers',
27                  'hackers/svn', 'hackers/trac', 'hackers/reviewboard');
28    foreach ($dirs as $dir) {
29        $path = IA_BACKUP_DIR . '/' . $dir;
30        if (!is_dir($path)) {
31            mkdir($path, 0755, true);
32        }
33    }
34    system(sprintf('chown -R %s:%s %s/*', IA_BACKUP_USER, IA_BACKUP_USER,
35           IA_BACKUP_DIR));
36}
37
38function backup_db() {
39    $temp_file = tempnam(temp_dir(), 'infoarena.sql.gz');
40    $backup_file = sprintf("%s/db/db-%s.sql.gz", IA_BACKUP_DIR,
41        backup_timestamp());
42    log_print("Backing up database to {$temp_file}");
43    system(sprintf("%s/scripts/db-backup %s", IA_ROOT_DIR, $temp_file));
44    if (IA_BACKUP_DB_ENCRYPT_KEY) {
45        log_assert(file_exists(IA_BACKUP_DB_ENCRYPT_KEY));
46        $backup_file .= ".gpg";
47        log_print("Encrypting database dump to {$backup_file}");
48        system(sprintf("gpg -c --no-use-agent --batch --no-tty --passphrase-file='%s' "
49                ."< %s > %s", IA_BACKUP_DB_ENCRYPT_KEY, $temp_file,
50                $backup_file));
51        unlink($temp_file);
52    } else {
53        rename($temp_file, $backup_file);
54    }
55    chown($backup_file, IA_BACKUP_USER);
56    chgrp($backup_file, IA_BACKUP_USER);
57}
58
59function backup_path($target, $paths) {
60    $target_file = sprintf("%s/%s%s.tar.gz", IA_BACKUP_DIR, $target,
61        backup_timestamp());
62    $quoted_paths = "'".join("' '", $paths)."'";
63    log_print(sprintf("Creating archive %s from (%s)", $target_file,
64        $quoted_paths));
65    system(sprintf("tar -czf %s %s", $target_file, $quoted_paths));
66    chown($target_file, IA_BACKUP_USER);
67    chgrp($target_file, IA_BACKUP_USER);
68}
69
70function backup_daily() {
71    log_print("Running daily backup");
72    backup_db();
73}
74
75function backup_weekly() {
76    log_print("Running weekly backup");
77    global $BACKUP_PATHS_WEEKLY;
78    foreach ($BACKUP_PATHS_WEEKLY as $target => $paths) {
79        backup_path($target, $paths);
80    }
81}
82
83function main() {
84    global $argv;
85    backup_init_dirs();
86    if (count($argv) != 2 || !in_array($argv[1], array('daily', 'weekly'))) {
87        log_error("Expecting 1 argument: 'daily' or 'weekly'.");
88    }
89    if ('daily' == $argv[1]) {
90        backup_daily();
91    } elseif ('weekly' == $argv[1]) {
92        backup_weekly();
93    } else {
94        log_error("Invalid args");
95    }
96}
97
98main();
99
100?>
Note: See TracBrowser for help on using the repository browser.