forked from backdrop-contrib/backup_migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_migrate.module
1859 lines (1644 loc) · 67.3 KB
/
backup_migrate.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
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Create (manually or scheduled) and restore backups of your Backdrop MySQL
* database with an option to exclude table data (e.g. cache_*)
*/
define('BACKUP_MIGRATE_VERSION', '1.x-1.x');
define('BACKUP_MIGRATE_MENU_PATH', 'admin/config/system/backup_migrate');
define('BACKUP_MIGRATE_MENU_DEPTH', 4);
/* Backdrop hook_config_info() */
function backup_migrate_config_info() {
$prefixes['backup_migrate.settings'] = array(
'label' => t('Backup Migrate settings'),
'group' => t('Backup Migrate'),
);
return $prefixes;
}
/* Backdrop Hooks */
/**
* Implements hook_help().
*/
/** Backdrop does not utilize hook_help so this section has been removed */
/*
function backup_migrate_help($section, $arg) {
$help = array(
array(
'body' =>
t('Backup and Migrate makes the task of backing up your Backdrop database and migrating data from one Backdrop install to another easier. It provides a function to backup the entire database to file or download, and to restore from a previous backup. You can also schedule the backup operation. Compression of backup files is also supported. The database backup files created with this module can be imported into this or any other Backdrop installation with the !restorelink, or you can use a database tool such as <a href="!phpmyadminurl">phpMyAdmin</a> or the mysql command line command.',
array(
'!restorelink' => user_access('restore from backup') ? l(t('restore feature'), BACKUP_MIGRATE_MENU_PATH . '/restore') : t('restore feature'),
'!phpmyadminurl' => 'http://www.phpmyadmin.net'
)
)
),
BACKUP_MIGRATE_MENU_PATH => array(
'title' => t('Quick Backup Tab'),
'body' => t('Use this form to run simple manual backups of your database and files.'),
'access arguments' => array('perform backup'),
),
BACKUP_MIGRATE_MENU_PATH . '/export/advanced' => array(
'title' => t('Advanced Backup Tab'),
'body' => t('Use this form to run manual backups of your database and files with more advanced options. If you have any !profilelink saved you can load those settings. You can save any of the changes you make to these settings as a new settings profile.',
array("!profilelink" => user_access('administer backup and migrate') ? l(t('settings profiles'), BACKUP_MIGRATE_MENU_PATH . '/profile') : t('settings profiles'), '!restorelink' => user_access('restore from backup') ? l(t('restore feature'), BACKUP_MIGRATE_MENU_PATH . '/restore') : t('restore feature'), '!phpmyadminurl' => 'http://www.phpmyadmin.net')),
'access arguments' => array('perform backup'),
),
BACKUP_MIGRATE_MENU_PATH . '/restore' => array(
'title' => t('Restore Tab'),
'body' => t('Upload a backup and migrate backup file. The restore function will not work with database dumps from other sources such as phpMyAdmin.'),
'access arguments' => array('restore from backup'),
),
BACKUP_MIGRATE_MENU_PATH . '/settings/destination' => array(
'title' => t('Destinations'),
'body' => t('Destinations are the places you can save your backup files to or them load from.'),
'more' => t('Files can be saved to a directory on your web server, downloaded to your desktop or emailed to a specified email account. From the Destinations tab you can create, delete and edit destinations or list the files which have already been backed up to the available destinations.'),
'access arguments' => array('administer backup and migrate'),
),
BACKUP_MIGRATE_MENU_PATH . '/settings/source' => array(
'title' => t('Sources'),
'body' => t('Sources are the things that can be backed up such as database or file directories.'),
'access arguments' => array('administer backup and migrate'),
),
BACKUP_MIGRATE_MENU_PATH . '/settings/profile' => array(
'title' => t('Profiles'),
'body' => t('Profiles are saved backup settings. Profiles store your table exclusion settings as well as your backup file name, compression and timestamp settings. You can use profiles in !schedulelink and for !manuallink.',
array('!schedulelink' => user_access('administer backup and migrate') ? l(t('schedules'), BACKUP_MIGRATE_MENU_PATH . '/schedule') : t('settings profiles'), '!manuallink' => user_access('perform backups') ? l(t('manual backups'), BACKUP_MIGRATE_MENU_PATH) : t('manual backups'))),
'more' => t('You can create new profiles using the add profiles tab or by checking the "Save these settings" button on the advanced backup page.'),
'access arguments' => array('administer backup and migrate'),
),
BACKUP_MIGRATE_MENU_PATH . '/schedule' => array(
'title' => t('Scheduling'),
'body' => t('Automatically backup up your database and files on a regular schedule using <a href="!cronurl">cron</a>.',
array('!cronurl' => url('admin/config/system/cron'))),
'more' => t('Each schedule will run a maximum of once per cron run, so they will not run more frequently than your cron is configured to run. If you specify a number of backups to keep for a schedule, old backups will be deleted as new ones are created. <strong>If you specify a number of files to keep, other backup files in that schedule\'s destination will get deleted</strong>.'),
'access arguments' => array('administer backup and migrate'),
),
BACKUP_MIGRATE_MENU_PATH . '/settings/import' => array(
'title' => t('Importing Settings'),
'body' => t('Import a settings profile, backup schedule, source or destination by pasting the export code into the textarea.',
array('!cronurl' => url('admin/config/system/cron'))),
),
);
if (module_exists('help')) {
$help[BACKUP_MIGRATE_MENU_PATH]['body'] .= ' ' . t('Visit the !helppage for more help using this module.', array('!helppage' => l(t('help page'), 'admin/help/backup_migrate')));
}
if (isset($help[$section])) {
return $help[$section]['body'];
}
if ($section == 'admin/help#backup_migrate') {
$out = "";
foreach ($help as $key => $section) {
if (isset($section['access arguments'])) {
foreach ($section['access arguments'] as $access) {
if (!user_access($access)) {
continue 2;
}
}
}
if (@$section['title']) {
if (!is_numeric($key)) {
$section['title'] = l($section['title'], $key);
}
$out .= "<h3>". $section['title'] ."</h3>";
}
$out .= "<p>". $section['body'] ."</p>";
if (!empty($section['more'])) {
$out .= "<p>". $section['more'] ."</p>";
}
}
return $out;
}
}
END of HOOK_HELP REMOVAL */
/**
* Implements hook_menu().
*/
function backup_migrate_menu() {
$items = array();
$items[BACKUP_MIGRATE_MENU_PATH] = array(
'title' => 'Backup and Migrate',
'description' => 'Backup/restore your database and files or migrate data to or from another Backdrop site.',
'page callback' => 'backup_migrate_menu_callback',
'page arguments' => array('', 'backup_migrate_ui_manual_backup_quick', TRUE),
'access arguments' => array('access backup and migrate'),
'type' => MENU_NORMAL_ITEM,
);
$items[BACKUP_MIGRATE_MENU_PATH . '/export'] = array(
'title' => 'Backup',
'description' => 'Backup the database.',
'page callback' => 'backup_migrate_menu_callback',
'page arguments' => array('', 'backup_migrate_ui_manual_backup_quick', TRUE),
'access arguments' => array('access backup and migrate'),
'weight' => 0,
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items[BACKUP_MIGRATE_MENU_PATH . '/export/quick'] = array(
'title' => 'Quick Backup',
'description' => 'Backup the database.',
'page callback' => 'backup_migrate_menu_callback',
'page arguments' => array('', 'backup_migrate_ui_manual_backup_quick', TRUE),
'access arguments' => array('perform backup'),
'weight' => 0,
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items[BACKUP_MIGRATE_MENU_PATH . '/export/advanced'] = array(
'title' => 'Advanced Backup',
'description' => 'Backup the database.',
'page callback' => 'backup_migrate_menu_callback',
'page arguments' => array('', 'backup_migrate_ui_manual_backup_advanced', TRUE),
'access arguments' => array('perform backup'),
'weight' => 1,
'type' => MENU_LOCAL_TASK,
);
$items[BACKUP_MIGRATE_MENU_PATH . '/restore'] = array(
'title' => 'Restore',
'description' => 'Restore the database from a previous backup',
'page callback' => 'backup_migrate_menu_callback',
'page arguments' => array('', 'backup_migrate_ui_manual_restore', TRUE),
'access arguments' => array('restore from backup'),
'weight' => 1,
'type' => MENU_LOCAL_TASK,
);
$items[BACKUP_MIGRATE_MENU_PATH . '/backups'] = array(
'title' => 'Saved Backups',
'description' => 'Previously created backups',
'page callback' => 'backup_migrate_menu_callback',
'page arguments' => array('', 'backup_migrate_ui_saved_backups', TRUE),
'access arguments' => array('access backup files'),
'weight' => 2,
'type' => MENU_LOCAL_TASK,
);
// @TODO: This was removed because it was getting far to complicated for an abstraction. Exposing the actual schedules is more direct.
// $items[BACKUP_MIGRATE_MENU_PATH . '/schedule'] = array(
// 'title' => 'Schedule',
// 'description' => 'Schedule automatic backups',
// 'page callback' => 'backup_migrate_menu_callback',
// 'page arguments' => array('', 'backup_migrate_ui_schedule', TRUE),
// 'access arguments' => array('administer backup and migrate'),
// 'weight' => 3,
// 'type' => MENU_LOCAL_TASK,
// );
$items[BACKUP_MIGRATE_MENU_PATH . '/settings'] = array(
'title' => 'Settings',
'description' => 'Module settings.',
'page callback' => 'backup_migrate_menu_callback',
'page arguments' => array('crud', 'backup_migrate_crud_ui_list_all', TRUE),
'access arguments' => array('administer backup and migrate'),
'weight' => 4,
'type' => MENU_LOCAL_TASK,
);
$items[BACKUP_MIGRATE_MENU_PATH . '/settings/import'] = array(
'title' => 'Import',
'description' => 'Import backup and migrate settings.',
'page callback' => 'backup_migrate_menu_callback',
'page arguments' => array('crud', 'backup_migrate_crud_ui_import', TRUE),
'access arguments' => array('administer backup and migrate'),
'weight' => 10,
'type' => MENU_LOCAL_TASK,
);
backup_migrate_include('crud');
$items += backup_migrate_crud_menu();
return $items;
}
/**
* Implements hook_element_info().
*/
function backup_migrate_element_info() {
$types['backup_migrate_dependent'] = array(
'#dependencies' => array(),
'#theme' => 'backup_migrate_dependent',
);
$types['backup_migrate_file_list'] = array(
'#theme' => 'backup_migrate_file_list',
'#process' => array('form_process_backup_migrate_file_list'),
'#multiple' => FALSE,
'#files' => array(),
'#display_options' => array(),
'#default_value' => 0,
);
return $types;
}
/**
* Callback function for backup_migrate_file_list '#process' callback.
*/
function form_process_backup_migrate_file_list($element) {
if (count($element['#files']) > 0) {
$weight = 0;
$options = array();
$max = $count = $element['#display_options']['limit'];
$files = $element['#files'];
foreach ($files as $id => $file) {
$options[$id] = $file->info('filename');
}
$element['files'] = array(
'#type' => 'radios',
'#options' => $options,
'#default_value' => $element['#default_value'],
'#parents' => $element['#parents'],
);
}
return $element;
}
/**
* Implements hook_form_update_script_selection_form_alter().
*/
function backup_migrate_form_update_script_selection_form_alter(&$form, $form_state) {
$msg = t('Before you run any database updates, <strong>make sure you have a backup of your database.</strong> Use !backup_migrate to create that backup now.', array('!backup_migrate' => l(t('Backup and Migrate'), BACKUP_MIGRATE_MENU_PATH)));
backdrop_set_message($msg, 'warning');
}
/**
* Implements hook_cron().
*
* Takes care of scheduled backups and deletes abandoned temp files.
*/
function backup_migrate_cron() {
// Set the message mode to logging.
_backup_migrate_message_callback('_backup_migrate_message_log');
// Clean up any previous abandoned tmp files before we attempt to back up.
backup_migrate_include('files');
_backup_migrate_temp_files_delete();
backup_migrate_include('schedules');
backup_migrate_schedules_cron();
// Clean up any tmp files from this run.
_backup_migrate_temp_files_delete();
}
/**
* Implements hook_cronapi().
*/
function backup_migrate_cronapi($op, $job = NULL) {
$items = array();
backup_migrate_include('schedules');
foreach (backup_migrate_get_schedules() as $schedule) {
if ($schedule->get('cron') == BACKUP_MIGRATE_CRON_ELYSIA) {
$id = $schedule->get('id');
$items['backup_migrate_' . $id] = array(
'description' => t('Run the Backup and Migrate \'!name\' schedule', array('!name' => $schedule->get('name'))),
'rule' => $schedule->get('cron_schedule'),
'callback' => 'backup_migrate_schedule_run',
'arguments' => array($id),
'file' => 'includes/schedules.inc',
);
}
}
return $items;
}
/**
* Implements hook_permission().
*/
function backup_migrate_permission() {
return array(
'access backup and migrate' => array(
'title' => t('Access Backup and Migrate'),
'description' => t('Access the Backup and Migrate admin section.'),
),
'perform backup' => array(
'title' => t('Perform a backup'),
'description' => t('Back up any of the available databases.'),
),
'access backup files' => array(
'title' => t('Access backup files'),
'description' => t('Access and download the previously created backup files.'),
),
'delete backup files' => array(
'title' => t('Delete backup files'),
'description' => t('Delete the previously created backup files.'),
),
'restore from backup' => array(
'title' => t('Restore the site'),
'description' => t('Restore the site\'s database from a backup file.'),
),
'administer backup and migrate' => array(
'title' => t('Administer Backup and Migrate'),
'description' => t('Edit Backup and Migrate profiles, schedules and destinations.'),
),
);
}
/**
* Implements hook_simpletest().
*/
function backup_migrate_simpletest() {
$dir = backdrop_get_path('module', 'backup_migrate') .'/tests';
$tests = file_scan_directory($dir, '\.test$');
return array_keys($tests);
}
/**
* Implements hook_theme().
*/
function backup_migrate_theme() {
$themes = array(
'backup_migrate_ui_manual_quick_backup_form_inline' => array(
'arguments' => array('form'),
'render element' => 'form',
),
'backup_migrate_ui_quick_schedule_form_inline' => array(
'arguments' => array('form'),
'render element' => 'form',
),
'backup_migrate_dependent' => array(
'arguments' => array('element'),
'render element' => 'element',
),
'backup_migrate_file_list' => array(
'arguments' => array('element'),
'render element' => 'element',
),
'backup_migrate_group' => array(
'variables' => array('title' => '', 'body' => '', 'description' => ''),
),
);
return $themes;
}
/* Blocks */
/**
* Implements hook_block_info().
*/
function backup_migrate_block_info() {
$blocks = array();
$blocks['quick_backup'] = array(
'info' => t('Backup and Migrate Quick Backup'),
'admin' => TRUE
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function backup_migrate_block_view($delta) {
$function = '_backup_migrate_block_view_' . $delta;
if (function_exists('_backup_migrate_block_view_' . $delta)) {
return $function();
}
}
/**
* Quick Backup block.
*/
function _backup_migrate_block_view_quick_backup() {
if (user_access('access backup and migrate') && user_access('perform backup') && $_GET['q'] != BACKUP_MIGRATE_MENU_PATH) {
backdrop_add_css(backdrop_get_path('module', 'backup_migrate') .'/backup_migrate.css');
// Messages should be sent to the browser.
_backup_migrate_message_callback('_backup_migrate_message_browser');
return array(
'subject' => t('Quick Backup'),
'content' => backdrop_get_form('backup_migrate_ui_manual_quick_backup_form', FALSE),
);
}
return array();
}
/**
* Implements hook_backup_migrate_destinations().
*/
function backup_migrate_backup_migrate_schedules() {
backup_migrate_include('sources');
$schedules = array();
// Declare a default backup for each source that can be enabled.
// @TODO: This makes setting up scheduled easier, but only when combined with the now disabled quick-schedule feature.
// $sources = backup_migrate_get_sources();
// foreach ($sources as $id => $source) {
// $schedule = array(
// 'schedule_id' => $id,
// 'name' => $source->get('name'),
// 'source_id' => $id,
// 'destination_id' => 'scheduled',
// 'profile_id' => 'default',
// 'period' => config_get('backup_migrate.settings','backup_migrate_default_schedule'),
// 'keep' => BACKUP_MIGRATE_SMART_DELETE,
// 'enabled' => FALSE,
// );
// $schedules[$id] = backup_migrate_crud_create_item('schedule', $schedule);
// }
return $schedules;
}
/**
* Implementation of hook_ctools_plugin_api().
*
* Tell CTools that we support the default_mymodule_presets API.
*/
function backup_migrate_ctools_plugin_api($owner, $api) {
if ($owner == 'backup_migrate') {
return array('version' => 1);
}
}
/* Menu Callbacks */
/**
* A menu callback helper. Handles file includes and interactivity setting.
*/
function backup_migrate_menu_callback($include, $function, $interactive = TRUE) {
if ($include) {
backup_migrate_include($include);
}
// Set the message handler based on interactivity setting.
_backup_migrate_message_callback($interactive ? '_backup_migrate_message_browser' : '_backup_migrate_message_log');
// Get the arguments with the first 3 removed.
$args = array_slice(func_get_args(), 3);
return call_user_func_array($function, $args);
}
/**
* Include views .inc files as necessary.
*/
function backup_migrate_include() {
static $used = array();
foreach (func_get_args() as $file) {
if (!isset($used[$file])) {
require_once backup_migrate_include_path($file);
}
$used[$file] = TRUE;
}
}
/**
* Include views .inc files as necessary.
*/
function backup_migrate_include_path($file) {
return BACKDROP_ROOT . '/' . backdrop_get_path('module', 'backup_migrate') . "/includes/$file.inc";
}
/**
* The menu callback for easy manual backups.
*/
function backup_migrate_ui_manual_backup_quick() {
$out = array();
if (user_access('perform backup')) {
return backdrop_get_form('backup_migrate_ui_manual_quick_backup_form');
}
else {
return t('You do not have permission to back up this site.');
}
return $out;
}
/**
* The menu callback for advanced manual backups.
*/
function backup_migrate_ui_manual_backup_advanced() {
backup_migrate_include('profiles');
$out = array();
$profile_id = arg(BACKUP_MIGRATE_MENU_DEPTH + 2);
$profile = _backup_migrate_profile_saved_default_profile($profile_id);
$out[] = backdrop_get_form('backup_migrate_ui_manual_backup_load_profile_form', $profile);
$out[] = backdrop_get_form('backup_migrate_ui_manual_backup_form', $profile);
return $out;
}
/**
* The backup/export load profile form.
*/
function backup_migrate_ui_manual_backup_load_profile_form($form, &$form_state, $profile = NULL) {
$form = array();
$profile_options = _backup_migrate_get_profile_form_item_options();
if (count($profile_options) > 0) {
$profile_options = array(0 => t('-- Select a Settings Profile --')) + $profile_options;
$form['profile'] = array(
"#title" => t("Settings Profile"),
"#collapsible" => TRUE,
"#collapsed" => FALSE,
"#prefix" => '<div class="container-inline">',
"#suffix" => '</div>',
"#tree" => FALSE,
"#description" => t("You can load a profile. Any changes you made below will be lost."),
);
$form['profile']['profile_id'] = array(
"#type" => "select",
"#title" => t("Load Settings"),
'#default_value' => is_object($profile) ? $profile->get_id() : 0,
"#options" => $profile_options,
);
$form['profile']['load_profile'] = array(
'#type' => 'submit',
'#value' => t('Load Profile'),
);
}
return $form;
}
/**
* Submit the profile load form.
*/
function backup_migrate_ui_manual_backup_load_profile_form_submit($form, &$form_state) {
if ($profile = backup_migrate_get_profile($form_state['values']['profile_id'])) {
config_set("backup_migrate.settings","backup_migrate_profile_id", $profile->get_id());
$form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . '/export/advanced';
}
else {
config_set("backup_migrate.settings","backup_migrate_profile_id", NULL);
}
}
/**
* The menu callback for quick schedules.
*/
function backup_migrate_ui_schedule() {
backup_migrate_include('sources', 'schedules', 'profiles');
$out = '';
$sources = backup_migrate_get_sources();
$schedules = backup_migrate_get_schedules();
$out = backdrop_get_form('backup_migrate_ui_schedule_form', $sources, $schedules);
return $out;
}
/**
* The quick schedule form.
*/
function backup_migrate_ui_schedule_form($form, &$form_state, $sources, $schedules) {
$form['quickschedule'] = array('#tree' => TRUE);
// Add a quick schedule item for each source.
foreach ($sources as $id => $source) {
if (isset($schedules[$id])) {
$schedule = $schedules[$id];
$key = preg_replace('/[^A-Za-z0-9\-_]/', '-', $id);
$form['quickschedule'][$key] = array();
$form['quickschedule'][$key]['#theme'] = 'backup_migrate_ui_quick_schedule_form_inline';
$form['quickschedule'][$key]['id'] = array(
'#type' => 'value',
'#value' => $id,
);
$form['quickschedule'][$key]['enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Automatically backup my @source', array('@source' => $source->get('name'))),
'#default_value' => $schedule->get('enabled'),
);
/*
$form['quickschedule'][$id]['source_id'] = array(
'#type' => 'item',
'#title' => t('Backup Source'),
'#value' => $source->get('name'),
);
*/
$form['quickschedule'][$key]['settings'] = array(
'#type' => 'backup_migrate_dependent',
'#dependencies' => array(
'quickschedule[' . $key . '][enabled]' => TRUE,
),
);
$options = array(
(60*60) => t('Once an hour'),
(60*60*24) => t('Once a day'),
(60*60*24*7) => t('Once a week'),
);
$period = $schedule->get('period');
if (!isset($options[$period])) {
$options[$period] = $schedule->get('frequency_description');
}
$form['quickschedule'][$key]['settings']['period'] = array(
'#type' => 'select',
'#title' => t('Schedule Frequency'),
'#options' => $options,
'#default_value' => $period,
);
$form['quickschedule'][$key]['settings']['destination'] = _backup_migrate_get_destination_pulldown('scheduled backup', $schedule->get('destination_id'), $schedule->get('copy_destination_id'));
$form['quickschedule'][$key]['settings']['destination']['destination_id']['#parents'] = array('quickschedule', $key, 'destination_id');
$form['quickschedule'][$key]['settings']['destination']['copy']['#parents'] = array('quickschedule', $key, 'copy');
$form['quickschedule'][$key]['settings']['destination']['copy_destination']['copy_destination_id']['#parents'] = array('quickschedule', $key, 'copy_destination_id');
array(
'#type' => 'select',
'#title' => t('Backup Destination'),
'#options' => _backup_migrate_get_destination_form_item_options('scheduled backup'),
'#default_value' => $schedule->get('period'),
);
$form['quickschedule'][$key]['settings']['profile_id'] = array(
'#type' => 'select',
'#title' => t('Settings Profile'),
'#default_value' => $schedule->get('profile_id'),
'#options' => _backup_migrate_get_profile_form_item_options(),
);
$keep = $schedule->get('keep');
$option_keys = array_unique(array(BACKUP_MIGRATE_SMART_DELETE, 30, 100, 500, BACKUP_MIGRATE_KEEP_ALL, $keep));
$options = array();
foreach ($option_keys as $i) {
$options[$i] = $schedule->generate_keep_description($i);
}
$form['quickschedule'][$key]['settings']['keep'] = array(
'#type' => 'select',
'#title' => t('Backup retention'),
'#options' => $options,
'#default_value' => $keep,
);
$form['quickschedule'][$key]['settings']['advanced'] = array(
'#type' => 'markup',
'#markup' => l(t('Advanced Settings.'), BACKUP_MIGRATE_MENU_PATH . '/settings/schedule/edit/' . $id, array('query' => backdrop_get_destination())),
);
// Set the parent for the settings up a level.
foreach (element_children($form['quickschedule'][$key]['settings']) as $child) {
$form['quickschedule'][$key]['settings'][$child]['#parents'] = array('quickschedule', $key, $child);
}
}
}
if (element_children($form['quickschedule'])) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Schedules'),
);
}
$form['advanced'] = array(
'#type' => 'markup',
'#markup' => '<div class="clearblock backup-migrate-footer-message">' . t('For more scheduling options and to add additional schedules, try the <a href="!advancedurl">advanced schedule page</a>.', array('!advancedurl' => url(BACKUP_MIGRATE_MENU_PATH . '/settings/schedule'))) . '</div>',
);
return $form;
}
/**
* Submit the quick schedule form.
*/
function backup_migrate_ui_schedule_form_submit($form, &$form_state) {
backup_migrate_include('schedules');
if (user_access('administer backup and migrate')) {
// Override the backups.
foreach ($form_state['values']['quickschedule'] as $key => $values) {
$id = $values['id'];
if ($schedule = backup_migrate_get_schedule($id)) {
$schedule->from_array($values);
$schedule->save();
}
}
backdrop_set_message(t('Your schedules have been saved'));
}
}
/**
* The quick backup form.
*/
function backup_migrate_ui_manual_quick_backup_form($form, &$form_state, $inline = TRUE) {
backup_migrate_include('profiles', 'destinations', 'sources');
backdrop_add_js(backdrop_get_path('module', 'backup_migrate') .'/backup_migrate.js');
$form = array();
// Theme the form if we want it inline.
if ($inline) {
$form['#theme'] = 'backup_migrate_ui_manual_quick_backup_form_inline';
}
$form['quickbackup'] = array(
'#type' => 'fieldset',
"#title" => t("Quick Backup"),
"#collapsible" => FALSE,
"#collapsed" => FALSE,
"#tree" => FALSE,
);
$form['quickbackup']['source_id'] = _backup_migrate_get_source_pulldown(config_get('backup_migrate.settings','backup_migrate_source_id'));
$form['quickbackup']['destination'] = _backup_migrate_get_destination_pulldown('manual backup', config_get('backup_migrate.settings','backup_migrate_destination_id'), config_get('backup_migrate.settings','backup_migrate_copy_destination_id'));
$profile_options = _backup_migrate_get_profile_form_item_options();
$form['quickbackup']['profile_id'] = array(
"#type" => "select",
"#title" => t("Settings Profile"),
'#default_value' => config_get('backup_migrate.settings', 'backup_migrate_profile_id'),
"#options" => $profile_options,
);
$form['quickbackup']['description_enabled'] = array(
'#type' => 'checkbox',
"#title" => t("Add a note to the backup"),
);
$form['quickbackup']['description'] = array(
'#type' => 'backup_migrate_dependent',
'#dependencies' => array(
'description_enabled' => TRUE,
),
);
$form['quickbackup']['description']['description'] = array(
'#type' => 'textarea',
"#title" => t("Note"),
'#description' => t('This note will be saved with the backup file and shown on the listing page.'),
);
$form['advanced'] = array(
'#type' => 'markup',
'#markup' => '<div class="clearblock backup-migrate-footer-message">' . t('For more backup options, try the <a href="!advancedurl">advanced backup page</a>.', array('!advancedurl' => url(BACKUP_MIGRATE_MENU_PATH . '/export/advanced'))) . '</div>',
);
$form['quickbackup']['submit'] = array(
'#type' => 'submit',
'#value' => t('Backup now'),
'#weight' => 1,
);
$form['#validate'] = array('backup_migrate_ui_manual_quick_backup_form_validate');
$form['#submit'] = array('backup_migrate_ui_manual_quick_backup_form_submit');
return _backup_migrate_ui_action_form($form, $form_state, 'backup');
}
/**
* Validate the quick backup form.
*/
function backup_migrate_ui_manual_quick_backup_form_validate($form, &$form_state) {
backup_migrate_include('profiles', 'destinations');
if ($form_state['values']['source_id'] == $form_state['values']['destination_id']) {
form_set_error('destination_id', t('A source cannot be backed up to itself. Please pick a different destination for this backup.'));
}
// Wrap profile_id for prevent Undefined index notice.
$profile_id = isset($form_state['values']['profile_id']) ? $form_state['values']['profile_id'] : NULL;
// For a quick backup use the default settings.
$settings = _backup_migrate_profile_saved_default_profile($profile_id);
// Set the destination to the one chosen in the pulldown.
$settings->destination_id = array($form_state['values']['destination_id']);
$settings->source_id = $form_state['values']['source_id'];
// Add the second destination.
if (!empty($form_state['values']['copy_destination_id'])) {
$settings->destination_id[] = $form_state['values']['copy_destination_id'];
}
// Add the note
if (!empty($form_state['values']['description_enabled'])) {
$settings->filters['utils_description'] = $form_state['values']['description'];
}
$form_state['values']['settings'] = $settings;
}
/**
* Submit the quick backup form.
*/
function backup_migrate_ui_manual_quick_backup_form_submit($form, &$form_state) {
backup_migrate_include('profiles', 'destinations');
if (user_access('perform backup') && !empty($form_state['values']['settings'])) {
// Save the settings for next time.
config_set("backup_migrate.settings","backup_migrate_source_id", $form_state['values']['source_id']);
config_set("backup_migrate.settings","backup_migrate_destination_id", $form_state['values']['destination_id']);
config_set("backup_migrate.settings","backup_migrate_copy_destination_id", $form_state['values']['copy_destination_id']);
config_set("backup_migrate.settings","backup_migrate_profile_id", $form_state['values']['profile_id']);
// Do the backup.
backup_migrate_ui_manual_backup_perform($form_state['values']['settings']);
}
$form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH;
}
/**
* Theme the quick backup form.
*/
function theme_backup_migrate_ui_manual_quick_backup_form_inline($form) {
backdrop_add_css(backdrop_get_path('module', 'backup_migrate') .'/backup_migrate.css');
$form = $form['form'];
// Remove the titles so that the pulldowns can be displayed inline.
unset($form['quickbackup']['source_id']['#title']);
unset($form['quickbackup']['destination']['destination_id']['#title']);
unset($form['quickbackup']['destination']['destination_id']['#description']);
unset($form['quickbackup']['profile_id']['#title']);
unset($form['quickbackup']['destination']['copy_destination']['copy_destination_id']['#title']);
$options = array(
'!from' => backdrop_render($form['quickbackup']['source_id']),
'!to' => backdrop_render($form['quickbackup']['destination']['destination_id']),
'!profile' => backdrop_render($form['quickbackup']['profile_id']),
//'!submit' => backdrop_render($form['quickbackup']['submit']),
);
$form['quickbackup']['markup'] = array(
'#type' => 'markup',
"#prefix" => '<div class="container-inline backup-migrate-inline">',
"#suffix" => '</div>',
'#markup' => t('Backup my !from to !to using !profile', $options),
);
$destination = array(
'!to' => backdrop_render($form['quickbackup']['destination']['copy_destination']['copy_destination_id']),
);
$form['quickbackup']['destination']['copy']["#prefix"] = '<div class="container-inline backup-migrate-inline">';
$form['quickbackup']['destination']['copy']["#suffix"] = $destination['!to'] . '</div>';
// This is not good translation practice as it relies on the structure of
// english.
// If I add the pulldown to the label, however, the box toggles when the
// pulldown is clicked.
//
//$form['quickbackup']['destination']['copy']['#title'] = t('Save a copy to');
unset($form['quickbackup']['source_id']);
unset($form['quickbackup']['destination']['destination_id']);
unset($form['quickbackup']['destination']['copy_destination']);
unset($form['quickbackup']['profile_id']);
//unset($form['quickbackup']['submit']);
return backdrop_render_children($form);
}
/**
* Theme the quick schedule form.
*/
function theme_backup_migrate_ui_quick_schedule_form_inline($form) {
backdrop_add_js(backdrop_get_path('module', 'backup_migrate') .'/backup_migrate.js');
backdrop_add_css(backdrop_get_path('module', 'backup_migrate') .'/backup_migrate.css');
$form = $form['form'];
// Remove the titles so that the pulldowns can be displayed inline.
unset($form['settings']['source_id']['#title']);
unset($form['settings']['period']['#title']);
unset($form['settings']['destination']['destination_id']['#title']);
unset($form['settings']['destination']['destination_id']['#description']);
unset($form['settings']['profile_id']['#title']);
unset($form['settings']['destination']['copy_destination']['copy_destination_id']['#title']);
unset($form['settings']['profile_id']['#title']);
unset($form['settings']['keep']['#title']);
unset($form['settings']['keep']['#field_prefix']);
unset($form['settings']['keep']['#field_suffix']);
$replacements = array(
'!from' => backdrop_render($form['settings']['source_id']),
'!to' => backdrop_render($form['settings']['destination']['destination_id']),
'!frequency' => backdrop_render($form['settings']['period']),
'!profile' => backdrop_render($form['settings']['profile_id']),
'!keep' => backdrop_render($form['settings']['keep']),
);
$form['settings'] = array('inline' => array(
'#type' => 'markup',
"#prefix" => '<div class="container-inline backup-migrate-quickschedule">',
"#suffix" => '</div>',
'#markup' => t('Backup !frequency to !to using !profile keeping !keep', $replacements),
)) + $form['settings'];
$replacements = array(
'!to' => backdrop_render($form['settings']['destination']['copy_destination']['copy_destination_id']),
);
$form['settings']['destination']['copy']["#prefix"] = '<div class="container-inline backup-migrate-inline">';
$form['settings']['destination']['copy']["#suffix"] = $replacements['!to'] . '</div>';
unset($form['settings']['period']);
unset($form['settings']['destination']['destination_id']);
unset($form['settings']['destination']['copy_destination']);
unset($form['settings']['profile_id']);
unset($form['settings']['keep']);
return backdrop_render_children($form);
}
/**
* Theme the dependent form section.
*/
function theme_backup_migrate_dependent($vars) {
$element = $vars['element'];
backdrop_add_js(backdrop_get_path('module', 'backup_migrate') .'/backup_migrate.js');
backdrop_add_css(backdrop_get_path('module', 'backup_migrate') .'/backup_migrate.css');
$id = backdrop_html_id(implode('-', $element['#array_parents']));
$settings = array(
'backup_migrate' => array(
'dependents' => array($id => array('dependent' => $id, 'dependencies' => $element['#dependencies']))
)
);
backdrop_add_js($settings, 'setting');
return '<div id="edit-' . $id . '" class="backup-migrate-form-dependent">' . backdrop_render_children($element) . '</div>';
}
/**
* Theme file list form widget.
*/
function theme_backup_migrate_file_list($vars) {
$element = $vars['element'];
// Render the radios so they can be placed in the file list rendering.
$vars['element']['#display_options']['form_elements'] = array();
if (isset($vars['element']['files']) && count($vars['element']['files'])) {
foreach (element_children($vars['element']['files']) as $id) {
$vars['element']['files'][$id]['#title'] = '';
$vars['element']['#display_options']['form_elements'][$id] = render($vars['element']['files'][$id]);
}
}
$out = _backup_migrate_ui_destination_display_file_list($vars['element']['#files'], $vars['element']['#display_options']);
return $out;
}
/**
* Theme the dependent form section.
*/
function theme_backup_migrate_group($vars) {
$output = '';
if (!empty($vars['title'])) {
$output .= '<h2 class="backup-migrate-group-title">' . $vars['title'] . '</h2>';
}
if (!empty($vars['description'])) {
$output .= '<div class="backup-migrate-group-description backup-migrate-description description">' . $vars['description'] . '</div>';
}
$output .= '<div class="backup-migrate-group-body">' . $vars['body'] . '</div>';
$output = '<div class="backup-migrate-group">' . $output . '</div>';
return $output;
}
/**
* The backup/export form.
*/
function backup_migrate_ui_manual_backup_form($form, &$form_state, $profile) {
backdrop_add_js(backdrop_get_path('module', 'backup_migrate') .'/backup_migrate.js', array('type' => 'module', 'scope' => 'footer'));
$form = array();
$form += _backup_migrate_get_source_form('db');
$form_state['profile'] = $profile;
$form['machine_name'] = array(
"#type" => "value",
'#default_value' => $profile->get_id(),
);
$form['storage'] = array(
"#type" => "value",
'#default_value' => $profile->storage,
);
$form['destination'] = array(
"#type" => "fieldset",
"#title" => t("Backup Destination"),
"#collapsible" => TRUE,
"#collapsed" => FALSE,
"#tree" => FALSE,