summaryrefslogtreecommitdiffstats
path: root/modules-available/backup/hooks/cron.inc.php
blob: c4a4b1dcb0ce3fe650befea2b01a076566d4edcb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php

(function () {
	// Sunday midnight, between 0-4 minutes of hour
	if (date('Hw') !== '000' || (int)date('i') >= 5)
		return;
	$mode = Property::get(BackupRestore::PROP_AUTO_BACKUP_MODE, BackupRestore::BACKUP_MODE_OFF);
	if ($mode === BackupRestore::BACKUP_MODE_OFF)
		return;
	// DO IT
	$password = trim(Property::get(BackupRestore::PROP_AUTO_BACKUP_PASS, ''));
	if (empty($password)) {
		$password = null;
	}
	if ($mode === BackupRestore::BACKUP_MODE_VMSTORE) {
		if (file_exists('/srv/openslx/nfs/.notmounted')) {
			EventLog::failure("Could not create automatic backup on VMstore as it's not mounted");
			return;
		}
		$destination = '/srv/openslx/nfs/auto_backups/';
	} else {
		$destination = '/root/auto_backups/';
	}
	$destination .= 'sat-' . Property::getServerIp() . '-' . date('Y-m-d');
	$task = Taskmanager::submit('BackupRestore', [
		'mode' => 'backup',
		'password' => $password,
		'destination' => $destination,
	]);
	if (!isset($task['id'])) {
		EventLog::failure("Could not create automatic backup, Taskmanager down");
		return;
	}
	$task = Taskmanager::waitComplete($task, 90000);
	if (!Taskmanager::isFinished($task) || Taskmanager::isFailed($task) || !isset($task['data']['backupFile'])) {
		EventLog::failure("Creating backup failed", print_r($task, true));
		return;
	}
	Property::set(BackupRestore::PROP_LAST_BACKUP, time());
})();