-
Notifications
You must be signed in to change notification settings - Fork 0
/
hosting_site_backup_manager.module
963 lines (833 loc) · 30.2 KB
/
hosting_site_backup_manager.module
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
<?php
/**
* @file
* Hosting site backup manager module.
*
* Adds a backup tab to the site node.
*/
/**
* Implements of hook_menu().
*
* @return array
* An array of menu items.
*/
function hosting_site_backup_manager_menu() {
$items = array();
$items['admin/hosting/backup_manager'] = array(
'title' => 'Backup manager',
'description' => 'Configure backup manager',
'page callback' => 'drupal_get_form',
'page arguments' => array('hosting_site_backup_manager_settings'),
'access arguments' => array('administer hosting site backup manager'),
'type' => MENU_LOCAL_TASK
);
$items['node/%hosting_site_node/backups'] = array(
'title' => 'Backups',
'description' => 'List of backups of this website',
'page callback' => 'hosting_site_backup_manager_page',
'page arguments' => array(1),
'access callback' => 'node_access',
'access arguments' => array('view', 1),
'type' => MENU_LOCAL_TASK,
);
$items['node/%hosting_site_node/ajax/backups'] = array(
'title' => 'Backup list',
'description' => 'AJAX callback for refreshing backups list',
'page callback' => 'hosting_site_backup_manager_ajax_list',
'page arguments' => array(1),
'access callback' => 'node_access',
'access arguments' => array('view', 1),
'type' => MENU_CALLBACK,
);
$items['node/%hosting_site_node/backup/download/%'] = array(
'title' => 'Download backup',
'description' => 'Download the selected backup',
'page callback' => 'hosting_site_backup_manager_download',
'page arguments' => array(1, 4),
'access callback' => 'node_access',
'access arguments' => array('view', 1),
'type' => MENU_CALLBACK,
);
$items['node/%hosting_site_node/backup/delete/%'] = array(
'title' => 'Delete backup',
'description' => 'Delete the selected backup',
'page callback' => 'hosting_site_backup_manager_delete',
'page arguments' => array(1, 4),
'access callback' => 'node_access',
'access arguments' => array('view', 1),
'type' => MENU_CALLBACK,
);
$items['node/%hosting_site_node/backup/restore/%'] = array(
'title' => 'Restore backup',
'description' => 'Restore the selected backup',
'page callback' => 'hosting_site_backup_manager_restore',
'page arguments' => array(1, 4),
'access callback' => 'node_access',
'access arguments' => array('view', 1),
'type' => MENU_CALLBACK,
);
$items['node/%hosting_site_node/backup/export/%'] = array(
'title' => 'Export backup',
'description' => 'Export the selected backup',
'page callback' => 'hosting_site_backup_manager_export',
'page arguments' => array(1, 4),
'access callback' => 'node_access',
'access arguments' => array('view', 1),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_perm
*/
function hosting_site_backup_manager_perm() {
return array('administer hosting site backup manager');
}
/**
* Placeholder function for additional delete checks.
*
* @param node $site
* The site node object.
* @param int $bid
* The backup id.
*
* @return string
* A confirmation form or a error string.
*/
function hosting_site_backup_manager_delete($site, $bid) {
// Get the filename.
$source = db_fetch_object(db_query("SELECT filename FROM {hosting_site_backups} WHERE site=%d AND bid=%d ORDER BY timestamp DESC", $site->nid, $bid));
if ($source) {
// Return a confirmation form.
$output = drupal_get_form('hosting_site_backup_manager_confirm_delete', $source->filename, $site->nid, $bid);
}
else {
$output = t('A valid backup could not be found.');
}
return $output;
}
/**
* Function that renders a confirmation form for the selected deletetion.
*
* @param arrays $form_state
* The form state array. Changes made to this variable will have no effect.
* @param string $filename
* The backup filename to delete.
* @param int $sitenid
* The selected site node id.
* @param int $bid
* The backup id.
*
* @return string
* A confirmation form.
*/
function hosting_site_backup_manager_confirm_delete($form_state, $filename, $sitenid, $bid) {
// Add the hosting_task javascript,
// so we can use the confirm form functionality.
drupal_add_js(drupal_get_path('module', 'hosting_task') . '/hosting_task.js');
// Get the backup data.
$source = db_fetch_object(db_query("SELECT * FROM {hosting_site_backups} WHERE site=%d AND bid=%d", $sitenid, $bid));
// Build the form.
$form = array();
$form['#filename'] = $filename;
$form['#sitenid'] = $sitenid;
$form['#bid'] = $bid;
// Not the best formatted code, but suffices for now.
$form['explanation'] = array(
'#type' => 'markup',
'#value' => '<h2>' . t('Backup information') . '</h2>' . t('Backup description:<br /> %description', array('%description' => filter_xss($source->description))) . '<br />',
'#weight' => -10,
);
$form['date'] = array(
'#type' => 'markup',
'#value' => t('Backup was created on:<br /> %date', array('%date' => format_date($source->timestamp, 'short'))) . '<br /><br />',
'#weight' => -8,
);
// Build the confirmation form.
$form = confirm_form(
$form,
t('Are you sure you want to delete this backup?', array()),
'node/' . $sitenid . '/backup',
t('This action cannot be undone.'),
t('Delete'),
t('Cancel'),
'hosting_site_backup_manager_confirm_delete');
// Copied from hosting_task.module
// Add an extra class to the actions to allow us to disable
// the cancel link via javascript for the modal dialog.
$form['actions']['#prefix'] = '<div id="hosting-task-confirm-form-actions" class="container-inline">';
return $form;
}
/**
* The form submit function.
*/
function hosting_site_backup_manager_confirm_delete_submit($form, &$form_state) {
// The deletion has been confirmed, process the result.
$filename = $form['#filename'];
$sitenid = $form['#sitenid'];
$bid = $form['#bid'];
// Add the hosting task.
hosting_add_task($sitenid, 'backup-delete', array($bid => $filename));
$form_state['redirect'] = 'node/' . $sitenid . '/backup';
modalframe_close_dialog();
}
/**
* Placeholder function for additional restore checks.
*
* @param node $site
* The site node object.
* @param int $bid
* The backup id.
*
* @return string
* A confirmation form.
*/
function hosting_site_backup_manager_restore($site, $bid) {
// @todo: Build in extra checks.
$output = drupal_get_form('hosting_site_backup_manager_confirm_restore', $site->nid, $bid);
return $output;
}
/**
* Function that renders a confirmation form for the selected deletetion.
*
* @param array $form_state
* The form state array. Changes made to this variable will have no effect.
* @param int $sitenid
* The selected site node id.
* @param int $bid
* The backup id
*
* @return string
* A confirmation form.
*/
function hosting_site_backup_manager_confirm_restore($form_state, $sitenid, $bid) {
// Add the hosting_task javascript,
// so we can use the confirm form functionality.
drupal_add_js(drupal_get_path('module', 'hosting_task') . '/hosting_task.js');
// Build the form.
$form = array();
$form['#sitenid'] = $sitenid;
$form['#bid'] = $bid;
// Get the backup data.
$source = db_fetch_object(db_query("SELECT * FROM {hosting_site_backups} WHERE site=%d AND bid=%d", $sitenid, $bid));
// Not the best formatted code, but suffices for now.
$form['explanation'] = array(
'#type' => 'markup',
'#value' => '<h2>' . t('Backup information') . '</h2>' . t('Backup description:<br /> %description', array('%description' => filter_xss($source->description))) . '<br />',
'#weight' => -10,
);
$form['date'] = array(
'#type' => 'markup',
'#value' => t('Backup was created on:<br /> %date', array('%date' => format_date($source->timestamp, 'short'))) . '<br /><br />',
'#weight' => -8,
);
// Build the confirmation form.
$form = confirm_form(
$form,
t('Are you sure you want to restore this backup?', array()),
'node/' . $sitenid . '/backup',
t('This action cannot be undone.'),
t('Restore'),
t('Cancel'),
'hosting_site_backup_manager_confirm_restore');
// Copied from hosting_task.module
// add an extra class to the actions to allow us to
// disable the cancel link via javascript for the modal dialog.
$form['actions']['#prefix'] = '<div id="hosting-task-confirm-form-actions" class="container-inline">';
return $form;
}
/**
* The form submit function.
*/
function hosting_site_backup_manager_confirm_restore_submit($form, &$form_state) {
// The restoration has been confirmed, process the result.
$sitenid = $form['#sitenid'];
$bid = $form['#bid'];
hosting_add_task($sitenid, 'restore', array('bid' => $bid));
$form_state['redirect'] = 'node/' . $sitenid . '/backup';
modalframe_close_dialog();
}
/**
* The form submit function.
*/
function hosting_site_backup_manager_export($site, $bid) {
// Get the filename.
$source = db_fetch_object(db_query("SELECT filename FROM {hosting_site_backups} WHERE site=%d AND bid=%d ORDER BY timestamp DESC", $site->nid, $bid));
// Start the task.
hosting_add_task($site->nid, 'export_backup', array('backup' => $source->filename));
// Redirect to the backups page.
drupal_goto('node/' . $site->nid . '/backups');
}
/**
* Helper function to get the root directory where backups are exported.
* @todo: make this configurable again, the provision_site_backup_manager needs to be in sync.
*/
function _hosting_site_backup_manager_get_backup_export_root() {
$root = '/var/aegir/backup-exports';
return $root;
}
/**
* Function to download a backup file.
*
* @param node $site
* The site node object.
* @param int $bid
* The backup id.
*
* @return file
* A file or a 404 page.
*/
function hosting_site_backup_manager_download($site, $bid) {
// @todo: check if the bid is not in a task.
$source = db_fetch_object(db_query("SELECT filename FROM {hosting_site_backups} WHERE site=%d AND bid=%d ORDER BY timestamp DESC", $site->nid, $bid));
if ($source) {
$backup_root = _hosting_site_backup_manager_get_backup_export_root();
$symlink = $backup_root . '/' . basename($source->filename);
if ($fd = @fopen($symlink, 'rb')) {
// Construct filename.
$filename = basename($source->filename);
// Set headers.
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
while (!feof($fd)) {
print fread($fd, 1024);
}
fclose($fd);
// File downloaded so delete the file.
// Start the task.
hosting_add_task($site->nid, 'remove_export_backup', array('export' => $symlink));
exit();
}
else {
drupal_not_found();
}
}
else {
drupal_not_found();
}
}
/**
* Show a list of backups for a website.
*
* @param node $site
* The site node.
*
* @return string
* A theme table with a list of backups.
*/
function hosting_site_backup_manager_page($site) {
// Add the js.
drupal_add_js(drupal_get_path('module', 'hosting_site_backup_manager') . '/hosting_site_backup_manager.js');
$settings['hostingSiteBackupManager'] = array(
'nid' => $site->nid,
);
drupal_add_js($settings, 'setting');
$output = '<div id="hosting-site-backup-manager-backupstable">';
$output .= hosting_site_backup_manager_backups_table($site);
$output .= '</div>';
return $output;
}
/**
* Page callback for backups table via AJAX.
*/
function hosting_site_backup_manager_ajax_list($site) {
$return['markup'] = hosting_site_backup_manager_backups_table($site);
drupal_json($return);
exit();
}
/**
* Prepare a table of available backups.
*/
function hosting_site_backup_manager_backups_table($site) {
global $user;
// Determine client name.
$client = hosting_client_node_load($site->client);
// Determine site's profile name
$profile = node_load($site->profile);
$ishostmaster = ($profile->short_name == 'hostmaster');
$isdeleted = $site->site_status == HOSTING_SITE_DELETED;
$headers[] = t('Backup');
$headers[] = array('data' => t('Actions'), 'class' => 'hosting-actions');
$rows = array();
// Only allow actions when there's no backup delete or
// restore task running for this node.
$buttonstatus = !(hosting_task_outstanding($site->nid, 'backup-delete') || hosting_task_outstanding($site->nid, 'restore') || hosting_task_outstanding($site->nid, 'export_backup') || hosting_task_outstanding($site->nid, 'remove_export_backup'));
// TODO Make the table reload automatically.
$result = db_query("SELECT bid, description, filename, size, timestamp FROM {hosting_site_backups} WHERE site=%d ORDER BY timestamp DESC", $site->nid);
if ($ishostmaster) {
$output = t("Feature not available for the Hostmaster site.");
return $output;
}
if (db_affected_rows($result) == 0) {
$output = t("No backups available.");
$options = array(
'query' => array('token' => drupal_get_token($user->uid)),
'attributes' => array('class' => 'hosting-button-dialog'),
);
$output .= ' ' . l(t('Create backup'), 'node/' . $site->nid . '/site_backup', $options);
return $output;
}
while ($object = db_fetch_object($result)) {
$row = array();
$row['description'] = filter_xss($object->description) . "<br />" . format_size($object->size) . " - " . format_date($object->timestamp, 'short');
$actions = array();
// @todo: Add check if the backup can be restored to current platform?
/* Add download button */
$downloadstatus = (_hosting_site_backup_manager_isfileavailable($site, $object->bid) && !hosting_task_outstanding($site->nid, 'remove_export_backup'));
$actions['download'] = _hosting_task_button(t('Get'), 'node/' . $site->nid . '/backup/download/' . $object->bid, t('Download the backup'), '', $downloadstatus, FALSE, FALSE);
/* Add export backup button */
$exportstatus = ($buttonstatus && !$downloadstatus);
$actions['export_backup'] = _hosting_task_button(t('Export'), 'node/' . $site->nid . '/backup/export/' . $object->bid, t('Make the backup exportable'), '', $exportstatus, FALSE, FALSE);
/* Add delete button */
$actions['delete'] = _hosting_task_button(t('Delete'), 'node/' . $site->nid . '/backup/delete/' . $object->bid, t('Delete the backup'), '', $buttonstatus, TRUE, FALSE);
/* Add restore button only for non-Hostmaster, non-deleted sites */
$restorestatus = ($buttonstatus && !$ishostmaster && !$isdeleted);
$actions['restore'] = _hosting_task_button(t('Restore'), 'node/' . $site->nid . '/backup/restore/' . $object->bid, t('Restore the backup'), '', $restorestatus, TRUE, FALSE);
$row['actions'] = array(
'data' => implode('', $actions),
'class' => 'hosting-actions',
);
$rows[] = array(
'data' => $row,
'class' => $info['class'],
);
}
$output .= theme('table', $headers, $rows, array('class' => 'hosting-table'));
return $output;
}
/**
* Implements hook_hosting_tasks().
*/
function hosting_site_backup_manager_hosting_tasks() {
$tasks = array();
$tasks['site']['export_backup'] = array(
'title' => t('Export Backup'),
'description' => t('Make a backup available for download.'),
'dialog' => TRUE,
'hidden' => TRUE,
);
$tasks['site']['remove_export_backup'] = array(
'title' => t('Remove Export Backup'),
'description' => t('Remove an exported backup, making it unavailable for download.'),
'dialog' => FALSE,
'hidden' => TRUE,
);
return $tasks;
}
/**
* Helper function to check if a backup file is available.
*/
function _hosting_site_backup_manager_isfileavailable($site, $bid) {
$result = FALSE;
// @todo: check if the bid is not in a task.
$source = db_fetch_object(db_query("SELECT filename FROM {hosting_site_backups} WHERE site=%d AND bid=%d ORDER BY timestamp DESC", $site->nid, $bid));
if ($source) {
// Get Document Root.
$root = _hosting_site_backup_manager_get_backup_export_root();
$file = $root . '/' . basename($source->filename);
if (file_exists($file)) {
return TRUE;
}
}
return $result;
}
/**
* Remove backup links if they persist beyond a timeout.
*
* They should normally be cleared directly after download.
*
* Implements hook_cron().
*/
function hosting_site_backup_manager_cron() {
// Get Document Root.
$backup_root = _hosting_site_backup_manager_get_backup_export_root();
$backup_export_expire_timeout = 60 * 10;
if (!is_dir($backup_root)) {
return;
}
try {
// Loop the client names that have a dir under the backup root.
$iterator = new DirectoryIterator($backup_root);
foreach ($iterator as $path) {
if ($path->isDot() || !$path->isDir()) {
continue;
}
// Loop all files in the client's backup dir.
$iterator2 = new DirectoryIterator($path->getPathname());
foreach ($iterator2 as $path2) {
if ($path2->isDot() || !$path2->isFile()) {
continue;
}
// Get the inode Change time, not to be confused with the modified time.
// When the inode changed, not the content. e.g. when the extra hardlink was added to the inode.
if ($path2->getCTime() < (time() - $backup_export_expire_timeout)) {
echo "wanna unlink: " . $path2->getPathname();
watchdog('hosting_site_backup_manager',
"Unlinking leftover exported backup: @pathname ",
array('@pathname' => $path2->getPathname()));
unlink($path2->getPathname());
}
// Were now using hardlinks, but incase we ever want to switch:
// For symlinks, $path2->getCTime() returns that of the link target not the link itself.
// $link_stats = lstat($path2->getPathname());
// if ($link_stats['mtime'] < (time() - $backup_export_expire_timeout)) {
}
}
} catch (UnexpectedValueException $e) {
// TODO: convert to watchdog_exception for D7
watchdog('hosting_site_backup_manager',
nl2br(check_plain($e->getMessage())),
NULL,
WATCHDOG_WARNING);
}
}
/**
* Configuration form for backup schedules
*/
function hosting_site_backup_manager_settings() {
$form['garbage_collection'] = array(
'#type' => 'fieldset',
'#title' => t('Garbage colleciton'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['garbage_collection']['hosting_backup_gc_default_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enable backup garbage collection'),
'#description' => t('Set the options below before enabling the garbage collection of old backups.'),
'#default_value' => variable_get('hosting_backup_gc_default_enabled', FALSE),
);
$form['garbage_collection']['hosting_backup_gc_intervals'] = array(
'#tree' => TRUE,
'#theme' => 'hosting_backup_gc_intervals_table',
'#element_validate' => array('hosting_backup_gc_intervals_element_validate'),
);
$form['garbage_collection']['hosting_backup_gc_intervals']['example'] = array(
'#type' => 'fieldset',
'#title' => t('Example'),
'#weight' => 5,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => _hosting_backup_gc_example(),
);
$intervals = array(
'' => t('n/a'),
strtotime('1 hour', 0) => t('Hours'),
strtotime('1 day', 0) => t('Days'),
strtotime('1 week', 0) => t('Weeks'),
strtotime('1 year', 0) => t('Years'),
);
$default_intervals = variable_get('hosting_backup_gc_intervals', array());
ksort($default_intervals);
// Add the empty row:
$default_intervals[] = array(
'older_than' => array(
'number' => 1,
'interval' => '',
),
'keep_per' => array(
'number' => 1,
'interval' => '',
),
);
$i = 0;
foreach ($default_intervals as $interval) {
$form['garbage_collection']['hosting_backup_gc_intervals'][$i]['older_than']['number'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(range(1, 365)),
'#default_value' => isset($interval['older_than']['number']) ? $interval['older_than']['number'] : 1,
'#attributes' => array(
'class' => 'hosting-backup-gc-inline',
),
);
$form['garbage_collection']['hosting_backup_gc_intervals'][$i]['older_than']['interval'] = array(
'#type' => 'select',
'#options' => $intervals,
'#default_value' => isset($interval['older_than']['interval']) ? $interval['older_than']['interval'] : '',
'#attributes' => array(
'class' => 'hosting-backup-gc-inline',
),
);
$form['garbage_collection']['hosting_backup_gc_intervals'][$i]['keep_per']['number'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(range(1, 365)),
'#default_value' => isset($interval['keep_per']['number']) ? $interval['keep_per']['number'] : 1,
'#attributes' => array(
'class' => 'hosting-backup-gc-inline',
),
);
$form['garbage_collection']['hosting_backup_gc_intervals'][$i]['keep_per']['interval'] = array(
'#type' => 'select',
'#options' => $intervals,
'#default_value' => isset($interval['keep_per']['interval']) ? $interval['keep_per']['interval'] : '',
'#attributes' => array(
'class' => 'hosting-backup-gc-inline',
),
);
$i++;
}
return system_settings_form($form);
}
function hosting_backup_gc_intervals_element_validate($element, &$form_state) {
$values = $form_state['values'][$element['#parents'][0]];
// Build an array of the non-empty ones.
$new_values = array();
foreach ($values as $val) {
if (!empty($val['older_than']['interval']) && !empty($val['keep_per']['interval'])) {
// Compute the key
$key = $val['older_than']['number'] * $val['older_than']['interval'];
$new_values[$key] = $val;
}
}
// Sort the new values.
asort($new_values);
// Set the new values.
form_set_value($element, $new_values, $form_state);
}
/**
* Implementation of hook_hosting_queues
*
* Return a list of queues that this module needs to manage.
*/
function hosting_site_backup_manager_hosting_queues() {
$queue['backup_gc'] = array(
'name' => t('Backup GC'),
'description' => t('Process the garbage collection of backups.'),
'type' => 'batch', # run queue sequentially. always with the same parameters.
'frequency' => strtotime("1 hour", 0),
'min_threads' => 6,
'max_threads' => 12,
'threshold' => 100,
'total_items' => hosting_site_count(),
'singular' => t('site'),
'plural' => t('sites'),
);
return $queue;
}
/**
* The main queue callback for the backup garbage collection.
*/
function hosting_backup_gc_queue($count) {
// Early exit if we are disabled.
if (!variable_get('hosting_backup_gc_default_enabled', FALSE)) {
return;
}
// Get the settings:
$intervals = variable_get('hosting_backup_gc_intervals', array());
ksort($intervals);
// Early exit if we've no work to do.
if (empty($intervals)) {
return;
}
// Otherwise loop over all the hosted sites.
$sites = hosting_backup_gc_get_sites($count);
foreach ($sites as $site) {
// Get a list of backups.
$backups = hosting_backup_gc_backup_list($site->nid);
$backups_to_remove = hosting_backup_gc_compute_backups_to_remove($intervals, $backups);
// Add the backup delete task for the backups to remove
if (!empty($backups_to_remove)) {
hosting_add_task($site->nid, 'backup-delete', $backups_to_remove);
}
hosting_backup_gc_site_touch($site->nid);
}
}
/**
* Get sites that need to be checked for backup GC.
*/
function hosting_backup_gc_get_sites($limit = 5) {
$sites = array();
$result = db_query("SELECT n.nid FROM {node} n LEFT JOIN {hosting_site} s ON n.nid = s.nid LEFT JOIN {hosting_backup_gc_sites} gc ON n.nid = gc.site_id WHERE n.type='site' and s.status = %d ORDER BY gc.last_gc ASC, n.nid ASC limit %d", HOSTING_SITE_ENABLED, $limit);
while ($nid = db_fetch_object($result)) {
$sites[$nid->nid] = node_load($nid->nid);
}
return $sites;
}
/**
* Retrieve a list of backup generated for a site.
*
* @param site
* The node if of the site backups are being retrieved for
* @return
* An associative array of backups existing for the site, indexed by bid and sorted reverse chronologically.
*/
function hosting_backup_gc_backup_list($site) {
$result = db_query("SELECT bid, filename, timestamp FROM {hosting_site_backups} WHERE site=%d ORDER BY timestamp DESC", $site);
while ($object = db_fetch_object($result)) {
$backups[$object->bid] = $object;
}
return $backups;
}
/**
* Compute which backups to remove.
*/
function hosting_backup_gc_compute_backups_to_remove($intervals, $backups) {
$to_remove = array();
foreach ($intervals as $interval) {
// Find the backups that are older than specified in the interval
$backups_to_consider = array();
// The $pockets array will store keep a record of which backups we have,
// two backups in a pocket means that the older one will have to go...
$pockets = array();
foreach ($backups as $backup) {
if ($backup->timestamp < (time() - $interval['older_than']['number'] * $interval['older_than']['interval'])) {
$backups_to_consider[] = $backup;
}
}
foreach ($backups_to_consider as $backup) {
// Now compute which backups we should be removing.
$pocket = floor((time() - $backup->timestamp) / ($interval['keep_per']['number'] * $interval['keep_per']['interval']));
// If there is not a backup in this pocket, mark it, otherwise add it to the list to delete.
if (empty($pockets[$pocket])) {
$pockets[$pocket] = TRUE;
}
else {
$to_remove[$backup->bid] = $backup->filename;
}
}
}
return $to_remove;
}
/**
* Mark a site as having its backups garbage collected.
*/
function hosting_backup_gc_site_touch($site_id, $timestamp = NULL) {
if (is_null($timestamp)) {
$timestamp = time();
}
$record = array('site_id' => $site_id, 'last_gc' => $timestamp);
if (db_result(db_query('SELECT count(*) FROM {hosting_backup_gc_sites} WHERE site_id = %d', $record['site_id']))) {
// Update
drupal_write_record('hosting_backup_gc_sites', $record, 'site_id');
}
else {
// Insert
drupal_write_record('hosting_backup_gc_sites', $record);
}
}
/**
* Implementation of hook_theme().
*/
function hosting_site_backup_manager_theme() {
$handlers = array();
$handlers['hosting_backup_gc_intervals_table'] = array(
'arguments' => array(
'form' => array(),
),
);
return $handlers;
}
/**
* Theme implementation of our form element of intervals.
*/
function theme_hosting_backup_gc_intervals_table($form) {
$output = '';
drupal_add_css(drupal_get_path('module', 'hosting_site_backup_manager') . '/hosting_site_backup_manager.admin.css');
$rows = array();
foreach (element_children($form) as $i) {
if ($form[$i]['#type'] == 'fieldset') {
continue;
}
$row = array();
$row[] = array('data' => t('For backups older than'), 'class' => 'hosting-backup-gc-narrow-first');
$row[] = drupal_render($form[$i]['older_than']);
$row[] = array('data' => t('Keep 1 backup per'), 'class' => 'hosting-backup-gc-narrow-second');
$row[] = drupal_render($form[$i]['keep_per']);
$rows[] = $row;
}
$output .= theme('table', NULL, $rows);
$output .= drupal_render($form);
return $output;
}
/**
* Generate example tekst for garbage collection intervals.
*
* @return string HTML
*/
function _hosting_backup_gc_example() {
$now = time();
// Introduction text
$output = '<p>' . t("This page provides an example of how the current backup garbage collection settings affect stored backups.") . '<br />';
$output .= t("The backups listed below are dummies (they don't represent real backups), but they show how many backups would be kept for a typical site over a given period.") . '</p>';
// Get current interval settings
$intervals = variable_get('hosting_backup_gc_intervals', array());
ksort($intervals);
if (empty($intervals)) {
$output .= '<p>' . t('<strong>No garbage collection intervals found.</strong> Please configure some options first.') . '</p>';
return $output;
}
// Get history (how long ago to display backups for)
$interval_keys = array_keys($intervals);
rsort($interval_keys);
$id = $interval_keys[0];
$history = ($intervals[$id]['older_than']['number'] * $intervals[$id]['older_than']['interval']) + ($intervals[$id]['keep_per']['number'] * $intervals[$id]['keep_per']['interval'] * 5);
// Get backup queue configuration
$default_backup_interval = variable_get('hosting_backup_queue_default_interval', strtotime('1 day', 0));
$period = array();
switch ($default_backup_interval) {
case '3600':
$period['value'] = -1;
$period['unit'] = 'hours';
break;
case '21600':
$period['value'] = -6;
$period['unit'] = 'hours';
break;
case '43200':
$period['value'] = -12;
$period['unit'] = 'hours';
break;
case '86400':
$period['value'] = -1;
$period['unit'] = 'days';
break;
case '604800':
$period['value'] = -1;
$period['unit'] = 'weeks';
break;
case '2419200':
$period['value'] = -4;
$period['unit'] = 'weeks';
break;
case '31536000':
$period['value'] = -1;
$period['unit'] = 'years';
break;
}
// Create a list of dummy backups (based on the default interval)
$number_backups = $history / $default_backup_interval;
$backups = array();
$i = 0;
while ($i < $number_backups) {
$backups[$i]['time'] = strtotime(($period['value'] * $i) . ' ' . $period['unit']);
$backups[$i]['date'] = date('Y-m-d H:i:s', $backups[$i]['time']);
$i++;
}
// Delete dummy backups based on configured intervals
foreach ($intervals as $interval) {
$pockets = array();
foreach ($backups as $id => $backup) {
if ($backup['time'] < ($now - $interval['older_than']['number'] * $interval['older_than']['interval'])) {
$pocket = floor($backup['time'] / ($interval['keep_per']['number'] * $interval['keep_per']['interval']));
if (empty($pockets[$pocket])) {
$pockets[$pocket] = TRUE;
}
else {
unset($backups[$id]);
}
}
}
}
// Get list of backup dates
$items = array();
foreach ($backups as $backup) {
$items[] = $backup['date'];
}
$items[] = '...';
// Display remaining backups
$output .= theme('item_list', $items, t('Backups'));
return $output;
}