schedules.inc
13 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
/**
* @file
* All of the schedule handling code needed for Backup and Migrate.
*/
backup_migrate_include('crud');
/**
* Run the preconfigured schedules. Called on cron.
*/
function backup_migrate_schedules_run() {
backup_migrate_include('profiles');
foreach (backup_migrate_get_schedules() as $schedule) {
$schedule->cron();
}
backup_migrate_cleanup();
}
/**
* Get all the available backup schedules.
*/
function backup_migrate_get_schedules() {
static $schedules = NULL;
// Get the list of schedules and cache them locally.
if ($schedules === NULL) {
$schedules = backup_migrate_crud_get_items('schedule');
}
return $schedules;
}
/**
* Get the schedule info for the schedule with the given ID, or NULL if none exists.
*/
function backup_migrate_get_schedule($schedule_id) {
$schedules = backup_migrate_get_schedules();
return @$schedules[$schedule_id];
}
/**
* A schedule class for crud operations.
*/
class backup_migrate_schedule extends backup_migrate_item {
var $db_table = "backup_migrate_schedules";
var $type_name = 'schedule';
var $singular = 'schedule';
var $plural = 'schedules';
var $default_values = array();
/**
* This function is not supposed to be called. It is just here to help the po extractor out.
*/
function strings() {
// Help the pot extractor find these strings.
t('Schedule');
t('Schedules');
t('schedule');
t('schedules');
}
/**
* Get the default values for this item.
*/
function get_default_values() {
return array(
'name' => t("Untitled Schedule"),
'source_id' => 'db',
'enabled' => 1,
'keep' => 0,
'period' => 60 * 60 * 24,
'storage' => BACKUP_MIGRATE_STORAGE_NONE
);
}
/**
* Get the columns needed to list the type.
*/
function get_list_column_info() {
$out = parent::get_list_column_info();
$out = array(
'name' => array('title' => t('Name')),
'destination_name' => array('title' => t('Destination'), 'html' => TRUE),
'profile_name' => array('title' => t('Profile'), 'html' => TRUE),
'frequency_description' => array('title' => t('Frequency')),
'keep_description' => array('title' => t('Keep')),
'enabled_description' => array('title' => t('Enabled')),
'last_run_description' => array('title' => t('Last run')),
) + $out;
return $out;
}
/**
* Get a row of data to be used in a list of items of this type.
*/
function get_list_row() {
drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css');
$row = parent::get_list_row();
if (!$this->is_enabled()) {
foreach ($row as $key => $field) {
$row[$key] = array('data' => $field, 'class' => 'schedule-list-disabled');
}
}
return $row;
}
/**
* Is the schedule enabled and valid.
*/
function is_enabled() {
$destination = $this->get_destination();
$profile = $this->get_profile();
return (!empty($this->enabled) && !empty($destination) && !empty($profile));
}
/**
* Get the destination object of the schedule.
*/
function get_destination() {
backup_migrate_include('destinations');
return backup_migrate_get_destination($this->get('destination_id'));
}
/**
* Get the name of the destination.
*/
function get_destination_name() {
if ($destination = $this->get_destination()) {
return check_plain($destination->get_name());
}
return '<div class="row-error">'. t("Missing") .'</div>';
}
/**
* Get the destination of the schedule.
*/
function get_profile() {
backup_migrate_include('profiles');
return backup_migrate_get_profile($this->get('profile_id'));
}
/**
* Get the name of the source.
*/
function get_profile_name() {
if ($profile = $this->get_profile()) {
return check_plain($profile->get_name());
}
return '<div class="row-error">'. t("Missing") .'</div>';
}
/**
* Format a frequency in human-readable form.
*/
function get_frequency_description() {
$period = $this->get_frequency_period();
$out = format_plural(($this->period / $period['seconds']), $period['singular'], $period['plural']);
return $out;
}
/**
* Format the number to keep in human-readable form.
*/
function get_keep_description() {
return !empty($this->keep) ? $this->keep : t('All');
}
/**
* Format the enabled status in human-readable form.
*/
function get_enabled_description() {
return !empty($this->enabled) ? t('Enabled') : t('Disabled');
}
/**
* Format the enabled status in human-readable form.
*/
function get_last_run_description() {
$last_run = $this->get('last_run');
return !empty($last_run) ? format_date($last_run, 'small') : t('Never');
}
/**
* Get the number of excluded tables.
*/
function get_exclude_tables_count() {
return count($this->exclude_tables) ? count($this->exclude_tables) : t("No tables excluded");
}
/**
* Get the number of excluded tables.
*/
function get_nodata_tables_count() {
return count($this->nodata_tables) ? count($this->nodata_tables) : t("No data omitted");
}
/**
* Get the edit form.
*/
function edit_form() {
$form = parent::edit_form();
backup_migrate_include('destinations', 'profiles');
$form['enabled'] = array(
"#type" => "checkbox",
"#title" => t("Enabled"),
"#default_value" => $this->get('enabled'),
);
$form['name'] = array(
"#type" => "textfield",
"#title" => t("Schedule Name"),
"#default_value" => $this->get('name'),
);
$form += _backup_migrate_get_source_form($this->get('source_id'));
$form['profile_id'] = array(
"#type" => "select",
"#title" => t("Settings Profile"),
"#options" => _backup_migrate_get_profile_form_item_options(),
"#default_value" => $this->get('profile_id'),
);
$form['profile_id']['#description'] = ' '. l(t("Create new profile"), BACKUP_MIGRATE_MENU_PATH . "/profile/add");
if (!$form['profile_id']['#options']) {
$form['profile_id']['#options'] = array('0' => t('-- None Available --'));
}
$period_options = array();
foreach ($this->frequency_periods() as $type => $period) {
$period_options[$type] = $period['title'];
}
$default_period = $this->get_frequency_period();
$default_period_num = $this->get('period') / $default_period['seconds'];
$form['period'] = array(
"#type" => "item",
"#title" => t("Backup every"),
"#prefix" => '<div class="container-inline">',
"#suffix" => '</div>',
"#tree" => TRUE,
);
$form['period']['number'] = array(
"#type" => "textfield",
"#size" => 6,
"#default_value" => $default_period_num,
);
$form['period']['type'] = array(
"#type" => "select",
"#options" => $period_options,
"#default_value" => $default_period['type'],
);
$form['keep'] = array(
"#type" => "textfield",
"#size" => 6,
"#title" => t("Number of Backup files to keep"),
"#description" => t("The number of backup files to keep before deleting old ones. Use 0 to never delete backups. <strong>Other files in the destination directory will get deleted if you specify a limit.</strong>"),
"#default_value" => $this->get('keep'),
);
$destination_options = _backup_migrate_get_destination_form_item_options('scheduled backup');
$form['destination_id'] = array(
"#type" => "select",
"#title" => t("Destination"),
"#description" => t("Choose where the backup file will be saved. Backup files contain sensitive data, so be careful where you save them."),
"#options" => $destination_options,
"#default_value" => $this->get('destination_id'),
);
$form['destination_id']['#description'] .= ' '. l(t("Create new destination"), BACKUP_MIGRATE_MENU_PATH . "/destination/add");
return $form;
}
/**
* Submit the edit form.
*/
function edit_form_validate($form, &$form_state) {
if (!is_numeric($form_state['values']['period']['number']) || $form_state['values']['period']['number'] <= 0) {
form_set_error('period][number', t('Backup period must be a number greater than 0.'));
}
if (!is_numeric($form_state['values']['keep']) || $form_state['values']['keep'] < 0) {
form_set_error('keep', t('Number to keep must be an integer greater than or equal to 0.'));
}
parent::edit_form_validate($form, $form_state);
}
/**
* Submit the edit form.
*/
function edit_form_submit($form, &$form_state) {
$periods = $this->frequency_periods();
$period = $periods[$form_state['values']['period']['type']];
$form_state['values']['period'] = $form_state['values']['period']['number'] * $period['seconds'];
parent::edit_form_submit($form, $form_state);
}
/**
* Get the period of the frequency (ie: seconds, minutes etc.)
*/
function get_frequency_period() {
foreach (array_reverse($this->frequency_periods()) as $period) {
if ($period['seconds'] && ($this->period % $period['seconds']) === 0) {
return $period;
}
}
}
/**
* Get a list of available backup periods. Only returns time periods which have a
* (reasonably) consistent number of seconds (ie: no months).
*/
function frequency_periods() {
return array(
'seconds' => array('type' => 'seconds', 'seconds' => 1, 'title' => t('Seconds'), 'singular' => t('Once a second'), 'plural' => t('Every @count seconds')),
'minutes' => array('type' => 'minutes', 'seconds' => 60, 'title' => t('Minutes'), 'singular' => t('Once a minute'), 'plural' => t('Every @count minutes')),
'hours' => array('type' => 'hours', 'seconds' => 3600, 'title' => t('Hours'), 'singular' => t('Once an hour'), 'plural' => t('Every @count hours')),
'days' => array('type' => 'days', 'seconds' => 86400, 'title' => t('Days'), 'singular' => t('Once a day'), 'plural' => t('Every @count days')),
'weeks' => array('type' => 'weeks', 'seconds' => 604800, 'title' => t('Weeks'), 'singular' => t('Once a week'), 'plural' => t('Every @count weeks')),
);
}
/**
* Get the message to send to the user when confirming the deletion of the item.
*/
function delete_confirm_message() {
return t('Are you sure you want to delete the profile %name? Any schedules using this profile will be disabled.', array('%name' => $this->get('name')));
}
/**
* Perform the cron action. Run the backup if enough time has elapsed.
*/
function cron() {
$now = time();
// Add a small negative buffer (1% of the entire period) to the time to account for slight difference in cron run length.
$wait_time = $this->period - ($this->period * variable_get('backup_migrate_schedule_buffer', 0.01));
if ($this->is_enabled() && ($now - $this->get('last_run')) >= $wait_time) {
if ($settings = $this->get_profile()) {
$settings->destination_id = $this->destination_id;
$settings->source_id = $this->source_id;
$this->update_last_run($now);
backup_migrate_perform_backup($settings);
$this->remove_expired_backups();
}
else {
backup_migrate_backup_fail("Schedule '%schedule' could not be run because requires a profile which is missing.", array('%schedule' => $schedule->get_name()), $settings);
}
}
}
/**
* Set the last run time of a schedule to the given timestamp, or now if none specified.
*/
function update_last_run($timestamp = NULL) {
if ($timestamp === NULL) {
$timestamp = time();
}
variable_set('backup_migrate_schedule_last_run_' . $this->get('id'), $timestamp);
}
/**
* Set the last run time of a schedule to the given timestamp, or now if none specified.
*/
function get_last_run($timestamp = NULL) {
return variable_get('backup_migrate_schedule_last_run_' . $this->get('id'), 0);
}
/**
* Remove older backups keeping only the number specified by the aministrator.
*/
function remove_expired_backups() {
$files = array();
backup_migrate_include('destinations');
$num_to_keep = $this->keep;
// If num to keep is not 0 (0 is infinity).
if ($num_to_keep && ($destination = $this->get_destination())) {
$i = 0;
if ($destination->op('delete') && $destination_files = $destination->list_files()) {
// Sort the files by modified time.
foreach ($destination_files as $file) {
if ($file->is_recognized_type() && $destination->can_delete_file($file->file_id())) {
$files[str_pad($file->info('filetime'), 10, "0", STR_PAD_LEFT) ."-". $i++] = $file;
}
}
// If we are beyond our limit, remove as many as we need.
$num_files = count($files);
if ($num_files > $num_to_keep) {
$num_to_delete = $num_files - $num_to_keep;
// Sort by date.
ksort($files);
// Delete from the start of the list (earliest).
for ($i = 0; $i < $num_to_delete; $i++) {
$file = array_shift($files);
$destination->delete_file($file->file_id());
}
}
}
}
}
}