backup_migrate.module
39.9 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
<?php
/**
* @file
* Create (manually or scheduled) and restore backups of your Drupal MySQL
* database with an option to exclude table data (e.g. cache_*)
*/
define('BACKUP_MIGRATE_VERSION', '7.x-2.x');
define('BACKUP_MIGRATE_MENU_PATH', 'admin/config/system/backup_migrate');
define('BACKUP_MIGRATE_MENU_DEPTH', 4);
/* Drupal Hooks */
/**
* Implementation of hook_help().
*/
function backup_migrate_help($section, $arg) {
$help = array(
array(
'body' =>
t('Backup and Migrate makes the task of backing up your Drupal database and migrating data from one Drupal 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 Drupal 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. Visit the !helppage for more help using this module',
array('!helppage' => l(t('help page'), 'admin/help/backup_migrate'))),
'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 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 . '/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 . '/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 on a regular schedule using <a href="!cronurl">cron</a>.',
array('!cronurl' => 'http://drupal.org/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 created. <strong>If specifiy 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'),
),
);
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;
}
}
/**
* Implementation of hook_menu().
*/
function backup_migrate_menu() {
$items = array();
$items[BACKUP_MIGRATE_MENU_PATH] = array(
'title' => 'Backup and Migrate',
'description' => 'Backup/restore your database or migrate data to or from another Drupal 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('access backup and migrate'),
'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 . '/nodesquirrel'] = array(
'title' => 'NodeSquirrel',
'page callback' => 'backup_migrate_menu_callback',
'page arguments' => array('destinations.nodesquirrel', 'nodesquirrel_settings_page'),
'access arguments' => array('administer backup and migrate'),
'weight' => 10,
'type' => MENU_LOCAL_TASK,
);
backup_migrate_include('crud');
$items += backup_migrate_crud_menu();
return $items;
}
/**
* Implementation of 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');
backup_migrate_include('schedules');
backup_migrate_schedules_run();
backup_migrate_include('files');
_backup_migrate_temp_files_delete();
}
/**
* Implementation of 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.'),
),
);
}
/**
* Implementation of hook_simpletest().
*/
function backup_migrate_simpletest() {
$dir = drupal_get_path('module', 'backup_migrate') .'/tests';
$tests = file_scan_directory($dir, '\.test$');
return array_keys($tests);
}
/**
* Implementation of hook_theme().
*/
function backup_migrate_theme() {
$themes = array(
'backup_migrate_ui_manual_quick_backup_form' => array(
'arguments' => array('form'),
'render element' => 'form',
),
);
return $themes;
}
/**
* Implementation of hook_backup_migrate_destinations().
*
* Get the default NodeSquirrel destination.
*/
function backup_migrate_backup_migrate_schedules() {
$schedules = array();
if (variable_get('nodesquirrel_schedule', 60*60*24) && variable_get('nodesquirrel_secret_key', FALSE) != FALSE) {
$schedule = array(
'schedule_id' => 'nodesquirrel',
'name' => 'NodeSquirrel',
'source_id' => 'db',
'destination_id' => 'nodesquirrel',
'profile_id' => 'default',
'period' => variable_get('nodesquirrel_schedule', 60*60*24),
'enabled' => variable_get('nodesquirrel_secret_key', FALSE) != FALSE,
);
$schedules['nodesquirrel'] = backup_migrate_crud_create_item('schedule', $schedule);
}
return $schedules;
}
/* 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 DRUPAL_ROOT . '/' . drupal_get_path('module', 'backup_migrate') . "/includes/$file.inc";
}
$used[$file] = TRUE;
}
}
/**
* The menu callback for easy manual backups.
*/
function backup_migrate_ui_manual_backup_quick() {
$out = array();
if (user_access('perform backup')) {
return drupal_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[] = drupal_get_form('backup_migrate_ui_manual_backup_load_profile_form', $profile);
$out[] = drupal_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'])) {
variable_set("backup_migrate_profile_id", $profile->get_id());
$form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . '/export/advanced';
}
else {
variable_set("backup_migrate_profile_id", NULL);
}
}
/**
* The quick backup form.
*/
function backup_migrate_ui_manual_quick_backup_form($form, &$form_state) {
backup_migrate_include('profiles', 'destinations');
drupal_add_js(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.js');
$form = array();
$form['quickbackup'] = array(
'#type' => 'fieldset',
"#title" => t("Quick Backup"),
"#collapsible" => FALSE,
"#collapsed" => FALSE,
"#tree" => FALSE,
);
$form['quickbackup']['source_id'] = _backup_migrate_get_source_pulldown(variable_get('backup_migrate_source_id', NULL));
// Pull the destination ID from the get param if it exists.
$destinations = _backup_migrate_get_destination_form_item_options('manual backup');
$destination_id = variable_get('backup_migrate_destination_id', 'download');
if (isset($_GET['destination_id']) && isset($destinations[$_GET['destination_id']])) {
$destination_id = $_GET['destination_id'];
}
$form['quickbackup']['destination_id'] = array(
"#type" => "select",
"#title" => t("Destination"),
"#options" => $destinations,
"#default_value" => $destination_id,
);
$profile_options = _backup_migrate_get_profile_form_item_options();
$form['quickbackup']['profile_id'] = array(
"#type" => "select",
"#title" => t("Settings Profile"),
'#default_value' => variable_get('backup_migrate_profile_id', NULL),
"#options" => $profile_options,
);
$form['quickbackup']['submit'] = array(
'#type' => 'submit',
'#value' => t('Backup now'),
'#weight' => 1,
);
$form['advanced'] = array(
'#type' => 'markup',
'#markup' => t('For more backup options, try the <a href="!advancedurl">advanced backup page</a>.', array('!advancedurl' => url(BACKUP_MIGRATE_MENU_PATH . '/export/advanced'))),
);
return $form;
}
/**
* Validate the quick backup form.
*/
function backup_migrate_ui_manual_quick_backup_form_validate($form, &$form_state) {
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.'));
}
}
/**
* 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')) {
// For a quick backup use the default settings.
$settings = _backup_migrate_profile_saved_default_profile($form_state['values']['profile_id']);
// Set the destination to the one chosen in the pulldown.
$settings->destination_id = $form_state['values']['destination_id'];
$settings->source_id = $form_state['values']['source_id'];
// Save the settings for next time.
variable_set("backup_migrate_source_id", $form_state['values']['source_id']);
variable_set("backup_migrate_destination_id", $form_state['values']['destination_id']);
variable_set("backup_migrate_profile_id", $form_state['values']['profile_id']);
// Do the backup.
backup_migrate_ui_manual_backup_perform($settings);
}
$form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH;
}
/**
* Theme the quick backup form.
*/
function theme_backup_migrate_ui_manual_quick_backup_form($form) {
$form = $form['form'];
// Remove the titles so that the pulldowns can be displayed inline.
unset($form['quickbackup']['source_id']['#title']);
unset($form['quickbackup']['destination_id']['#title']);
unset($form['quickbackup']['profile_id']['#title']);
$replacements = array(
'!from' => drupal_render($form['quickbackup']['source_id']),
'!to' => drupal_render($form['quickbackup']['destination_id']),
'!profile' => drupal_render($form['quickbackup']['profile_id']),
'!submit' => drupal_render($form['quickbackup']['submit']),
);
$form['quickbackup']['markup'] = array(
'#type' => 'markup',
"#prefix" => '<div class="container-inline">',
"#suffix" => '</div>',
'#markup' => t('Backup from !from to !to using !profile !submit', $replacements),
);
unset($form['quickbackup']['source_id']);
unset($form['quickbackup']['destination_id']);
unset($form['quickbackup']['profile_id']);
unset($form['quickbackup']['submit']);
return drupal_render_children($form);
}
/**
* The backup/export form.
*/
function backup_migrate_ui_manual_backup_form($form, &$form_state, $profile) {
drupal_add_js(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.js', array('type' => 'module', 'scope' => 'footer'));
$form = array();
$form += _backup_migrate_get_source_form('db');
$form += _backup_migrate_ui_backup_settings_form($profile);
$form['profile_id'] = 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,
"#description" => t("Choose where the backup file will be saved. Backup files contain sensitive data, so be careful where you save them. Select 'Download' to download the file to your desktop."),
'#weight' => 70,
);
$form['destination']['destination_id'] = array(
"#type" => "select",
"#title" => t("Destination"),
"#options" => _backup_migrate_get_destination_form_item_options('manual backup'),
"#default_value" => variable_get("backup_migrate_destination_id", "download"),
);
if (user_access('administer backup and migrate')) {
$form['destination']['destination_id']['#description'] = l(t("Create new destination"), BACKUP_MIGRATE_MENU_PATH . "/destination/add");
}
if (user_access('administer backup and migrate')) {
$form['save_settings'] = array(
"#type" => "checkbox",
"#title" => t('Save these settings.'),
"#default_value" => FALSE,
'#weight' => 80,
);
$form['save_options'] = array(
'#prefix' => '<div class="backup-migrate-save-options">',
'#suffix' => '</div>',
'#weight' => 90,
);
$name = array(
'#default_value' => $profile->get('name'),
'#type' => 'textfield',
'#title' => t('Save the settings as'),
);
if ($profile->get_id()) {
$form['save_options']['create_new'] = array(
'#default_value' => $profile->get('name'),
'#type' => 'radios',
'#default_value' => 0,
'#options' => array(
0 => t("Replace the '%profile' profile", array('%profile' => $profile->get('name'))),
1 => t('Create new profile'),
),
);
$name["#title"] = t('Profile name');
$name["#description"] = t("This will be the name of your new profile if you select 'Create new profile' otherwise it will become the name of the '%profile' profile.", array('%profile' => $profile->get('name')));
}
else {
$name["#title"] = t('Save the settings as');
$name["#description"] = t('Pick a name for the settings. Your settings will be saved as a profile and will appear in the <a href="!url">Profiles Tab</a>.', array('!url' => url(BACKUP_MIGRATE_MENU_PATH . '/profile')));
$name["#default_value"] = t('Untitled Profile');
}
$form['save_options']['name'] = $name;
$form['save_options'][] = array(
'#type' => 'submit',
'#value' => t('Save Without Backing Up'),
);
}
$form['#validate'][] = 'backup_migrate_ui_manual_quick_backup_form_validate';
$form['#submit'][] = 'backup_migrate_ui_manual_backup_form_submit';
$form[] = array(
'#type' => 'submit',
'#value' => t('Backup now'),
'#weight' => 100,
);
return $form;
}
/**
* Submit the form. Save the values as defaults if desired and output the backup file.
*/
function backup_migrate_ui_manual_backup_form_submit($form, &$form_state) {
// Save the settings profile if the save box is checked.
// $form_state['values']['nodata_tables'] = array_filter((array)$form_state['values']['nodata_tables']);
// $form_state['values']['exclude_tables'] = array_filter((array)$form_state['values']['exclude_tables']);
$profile = backup_migrate_crud_create_item('profile', $form_state['values']);
// Save the settings profile if the save box is checked.
if ($form_state['values']['save_settings'] && user_access('administer backup and migrate')) {
if (@$form_state['values']['create_new']) {
// Reset the id and storage so a new item will be saved.
$profile->set_id(NULL);
$profile->storage = BACKUP_MIGRATE_STORAGE_NONE;
}
$profile->save();
variable_set("backup_migrate_profile_id", $profile->get_id());
variable_set("backup_migrate_destination_id", $form_state['values']['destination_id']);
}
// Perform the actual backup if that is what was selected.
if ($form_state['values']['op'] == t('Backup now') && user_access('perform backup')) {
backup_migrate_ui_manual_backup_perform($profile);
}
$form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . "/export/advanced";
}
/**
* Perform an actual manual backup and tell the user of the progress.
*/
function backup_migrate_ui_manual_backup_perform($settings) {
// Peform the actual backup.
backup_migrate_perform_backup($settings);
}
/**
* The restore/import upload page.
*/
function backup_migrate_ui_manual_restore() {
return drupal_get_form('backup_migrate_ui_manual_restore_form');
}
/**
* The restore/import upload form.
*/
function backup_migrate_ui_manual_restore_form() {
backup_migrate_include('filters', 'destinations');
$form = array();
$sources = _backup_migrate_get_destination_form_item_options('source');
if (count($sources) > 1) {
$form['source_id'] = array(
"#type" => "select",
"#title" => t("Restore to"),
"#options" => _backup_migrate_get_destination_form_item_options('source'),
"#description" => t("Choose the database to restore to. Any database destinations you have created and any databases specified in your settings.php can be restored to."),
"#default_value" => 'db',
);
}
else {
$form['source_id'] = array(
"#type" => "value",
"#value" => 'db',
);
}
$form['backup_migrate_restore_upload'] = array(
'#title' => t('Upload a Backup File'),
'#type' => 'file',
'#description' => t("Upload a backup file created by this version of this module. For other database backups please use another tool for import. Max file size: %size", array("%size" => format_size(file_upload_max_size()))),
);
drupal_set_message(t('Restoring will delete some or all of your data and cannot be undone. <strong>Always test your backups on a non-production server!</strong>'), 'warning', FALSE);
$form = array_merge_recursive($form, backup_migrate_filters_settings_form(backup_migrate_filters_settings_default('restore'), 'restore'));
// Add the advanced fieldset if there are any fields in it.
if (@$form['advanced']) {
$form['advanced']['#type'] = 'fieldset';
$form['advanced']['#title'] = t('Advanced Options');
$form['advanced']['#collapsed'] = true;
$form['advanced']['#collapsible'] = true;
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Restore now'),
);
if (user_access('access backup files')) {
$form[] = array(
'#type' => 'markup',
'#markup' => t('<p>Or you can restore one of the files in your <a href="!url">saved backup destinations.</a></p>', array("!url" => url(BACKUP_MIGRATE_MENU_PATH . "/destination"))),
);
}
$form['#attributes'] = array('enctype' => 'multipart/form-data');
return $form;
}
/**
* The restore submit. Do the restore.
*/
function backup_migrate_ui_manual_restore_form_submit($form, &$form_state) {
$validators = array('file_validate_extensions' => array('gz zip sql mysql bz bz2 aes'));
if ($file = file_save_upload('backup_migrate_restore_upload', $validators)) {
backup_migrate_include('destinations');
backup_migrate_perform_restore('upload', $file->uri, $form_state['values']);
}
$form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . '/restore';
}
/**
* Convert an item to an 'exportable'.
*/
function backup_migrate_ui_export_form($form, &$form_state, $item) {
if ($item && function_exists('ctools_var_export')) {
$code = ctools_var_export($item);
$form = ctools_export_form($form_state, $code);
return $form;
}
return array();
}
/**
* Perform a backup with the given settings.
*/
function backup_migrate_perform_backup(&$settings) {
backup_migrate_include('destinations', 'files', 'filters');
timer_start('backup_migrate_backup');
// If not in 'safe mode', increase the maximum execution time:
if (!ini_get('safe_mode') && strpos(ini_get('disable_functions'), 'set_time_limit') === FALSE && ini_get('max_execution_time') != 0 && ini_get('max_execution_time') < variable_get('backup_migrate_backup_max_time', 1200)) {
set_time_limit(variable_get('backup_migrate_backup_max_time', 1200));
}
$timestamp = '';
if ($settings->append_timestamp && $settings->timestamp_format) {
$timestamp = format_date(time(), 'custom', $settings->timestamp_format);
}
$filename = _backup_migrate_construct_filename($settings->filename, $timestamp);
$file = new backup_file(array('filename' => $filename));
if (!$file) {
backup_migrate_backup_fail("Could not run backup because a temporary file could not be created.", array(), $settings);
return FALSE;
}
// Register shutdown callback to deal with timeouts.
register_shutdown_function('backup_migrate_shutdown', $settings);
$file = backup_migrate_filters_backup($file, $settings);
if (!$file) {
if (_backup_migrate_check_timeout()) {
backup_migrate_backup_fail('Could not complete the backup because the script timed out. Try increasing your PHP <a href="!url">max_execution_time setting</a>.', array('!url' => 'http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time'), $settings);
}
else {
backup_migrate_backup_fail("Could not complete the backup.", array(), $settings);
}
return FALSE;
}
$file = backup_migrate_destination_save_file($file, $settings);
if (!$file) {
backup_migrate_backup_fail("Could not run backup because the file could not be saved to the destination.", array(), $settings);
return FALSE;
}
// Backup succeeded,
$time = timer_stop('backup_migrate_backup');
$message = '%source backed up successfully to %file in destination %dest in !time ms. !action';
$params = array(
'%file' => $filename,
'%dest' => $settings->get_destination_name(),
'%source' => $settings->get_source_name(),
'!time' => $time['time'],
'!action' => !empty($settings->performed_action) ? $settings->performed_action : '',
);
if (($destination = $settings->get_destination()) && ($links = $destination->get_file_links($file->file_id()))) {
$params['!links'] = implode(", ", $links);
}
backup_migrate_backup_succeed($message, $params, $settings);
return $file;
}
/**
* Restore from a file in the given destination.
*/
function backup_migrate_perform_restore($destination_id, $file, $settings = array()) {
backup_migrate_include('files', 'filters');
timer_start('backup_migrate_restore');
// If not in 'safe mode', increase the maximum execution time:
if (!ini_get('safe_mode') && strpos(ini_get('disable_functions'), 'set_time_limit') === FALSE && ini_get('max_execution_time') != 0 && ini_get('max_execution_time') < variable_get('backup_migrate_restore_max_time', 1200)) {
set_time_limit(variable_get('backup_migrate_restore_max_time', 1200));
}
// Make the settings into a default profile.
if (!is_object($settings)) {
$settings = backup_migrate_crud_create_item('profile', $settings);
$settings->source_id = empty($settings->source_id) ? 'db' : $settings->source_id;
}
// Register shutdown callback.
register_shutdown_function('backup_migrate_shutdown', $settings);
if (!is_object($file)) {
// Load the file from the destination.
$file = backup_migrate_destination_get_file($destination_id, $file);
if (!$file) {
_backup_migrate_message("Could not restore because the file could not be loaded from the destination.", array(), 'error');
backup_migrate_cleanup();
return FALSE;
}
}
$file_id = $file->file_id();
// Filter the file and perform the restore.
$file = backup_migrate_filters_restore($file, $settings);
if (!$file) {
if (_backup_migrate_check_timeout()) {
backup_migrate_restore_fail('Could not perform the restore because the script timed out. Try increasing your PHP <a href="!url">max_execution_time setting</a>.', array('!url' => 'http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time'), 'error');
}
else {
backup_migrate_restore_fail("Could not perform the restore.", array(), 'error');
}
backup_migrate_cleanup();
return FALSE;
}
$time = timer_stop('backup_migrate_restore');
if ($file) {
$destination = backup_migrate_get_destination($destination_id);
$message = '%source restored from %dest file %file in !time ms. !action';
$params = array(
'%file' => $file->filename(),
'%source' => $settings->get_source_name(),
'%dest' => $destination->get_name(),
'!time' => $time['time'],
'!action' => !empty($settings->performed_action) ? $settings->performed_action : '',
);
if ($destination && $destination->op('list files')) {
$params['!links'] = t('<a href="!restoreurl">Restore again</a>', array('!restoreurl' => url(BACKUP_MIGRATE_MENU_PATH . '/destination/restorefile/'. $destination_id ."/". $file_id)));
}
backup_migrate_restore_succeed($message, $params, $settings);
}
// Delete any temp files we've created.
backup_migrate_cleanup();
// No errors. Return the file.
return $file;
}
/**
* Clean up when a backup operation fails.
*/
function backup_migrate_backup_fail($message, $params, $settings) {
backup_migrate_include('files', 'filters');
_backup_migrate_message($message, $params, 'error');
backup_migrate_cleanup();
backup_migrate_filters_invoke_all('backup_fail', $settings, $message, $params);
return FALSE;
}
/**
* Clean up when a backup operation suceeds.
*/
function backup_migrate_backup_succeed($message, $params, $settings) {
backup_migrate_include('filters', 'files');
_backup_migrate_message($message, $params, 'success');
backup_migrate_cleanup();
backup_migrate_filters_invoke_all('backup_succeed', $settings, $message, $params);
return FALSE;
}
/**
* Clean up when a restore operation fails.
*/
function backup_migrate_restore_fail($message, $params, $settings) {
backup_migrate_include('files', 'filters');
_backup_migrate_message($message, $params, 'error');
backup_migrate_cleanup();
backup_migrate_filters_invoke_all('restore_fail', $settings, $message, $params);
return FALSE;
}
/**
* Clean up when a restore operation suceeds.
*/
function backup_migrate_restore_succeed($message, $params, $settings) {
backup_migrate_include('filters', 'files');
_backup_migrate_message($message, $params, 'success');
backup_migrate_cleanup();
backup_migrate_filters_invoke_all('restore_succeed', $settings, $message, $params);
return FALSE;
}
/**
* Cleanup after a success or failure.
*/
function backup_migrate_cleanup() {
// Check that the cleanup function exists. If it doesn't then we probably didn't create any files to be cleaned up.
if (function_exists('_backup_migrate_temp_files_delete')) {
_backup_migrate_temp_files_delete();
}
}
/**
* Shutdown callback. Called when the script terminates even if the script timed out.
*/
function backup_migrate_shutdown($settings) {
// If we ran out of time, set an error so the user knows what happened
if (_backup_migrate_check_timeout()) {
backup_migrate_cleanup();
backup_migrate_backup_fail('The operation timed out. Try increasing your PHP <a href="!url">max_execution_time setting</a>.', array('!url' => 'http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time'), $settings);
// The session will have already been written and closed, so we need to write any changes directly.
_drupal_session_write(session_id(), session_encode());
// Add a redirect or we'll just get whitescreened.
drupal_goto(BACKUP_MIGRATE_MENU_PATH);
}
}
/* Actions/Workflow integration */
/**
* Action to backup the drupal site. Requires actions.module.
function action_backup_migrate_backup($op, $edit = array()) {
switch ($op) {
case 'do':
_backup_migrate_backup_with_defaults();
watchdog('action', 'Backed up database');
break;
case 'metadata':
return array(
'description' => t('Backup the database with the default settings'),
'type' => t('Backup and Migrate'),
'batchable' => TRUE,
'configurable' => FALSE,
);
// Return an HTML config form for the action.
case 'form':
return '';
// Validate the HTML form.
case 'validate':
return TRUE;
// Process the HTML form to store configuration.
case 'submit':
return '';
}
}
*/
/*
* Implementation of hook_action_info().
function backup_migrate_action_info() {
return array(
'backup_migrate_action_backup' => array(
'label' => t('Backup the database'),
'description' => t('Backup the database with the default settings.'),
),
);
}
*/
/*
* Action callback.
*/
function backup_migrate_action_backup() {
_backup_migrate_backup_with_defaults();
}
/* Utilities */
/**
* Backup the database with the default settings.
*/
function _backup_migrate_backup_with_defaults($destination_id = "manual") {
backup_migrate_include('files', 'profiles');
$settings = _backup_migrate_profile_saved_default_profile();
$settings->destination_id = $destination_id;
$settings->source_id = 'db';
backup_migrate_perform_backup($settings);
}
/**
* Helper function to set a drupal message and watchdog message depending on whether the module is being run interactively.
*/
function _backup_migrate_message($message, $replace = array(), $type = 'status') {
// Only set a message if there is a callback handler to handle the message.
if (($callback = _backup_migrate_message_callback()) && function_exists($callback)) {
$callback($message, $replace, $type);
}
// Store the message in case it's needed (for the status notification filter for example).
_backup_migrate_messages($message, $replace, $type);
}
/**
* Helper function to set a drupal message and watchdog message depending on whether the module is being run interactively.
*/
function _backup_migrate_messages($message = NULL, $replace = array(), $type = 'status') {
static $messages = array();
if ($message) {
$messages[] = array('message' => $message, 'replace' => $replace, 'type' => 'status');
}
return $messages;
}
/**
* Send a message to the browser. The normal type of message handling for interactive use.
*/
function _backup_migrate_message_browser($message, $replace, $type) {
// Log the message as well for admins.
_backup_migrate_message_log($message, $replace, $type);
// If there are links, we can display them in the browser.
if (!empty($replace['!links'])) {
$message .= " (!links)";
}
// Use drupal_set_message to display to the user.
drupal_set_message(t($message, $replace), str_replace('success', 'status', $type), FALSE);
}
/**
* Log message if we are in a non-interactive mode such as a cron run.
*/
function _backup_migrate_message_log($message, $replace, $type) {
// We only want to log the errors or successful completions.
if (in_array($type, array('error', 'success'))) {
watchdog('backup_migrate', $message, $replace, $type == 'error' ? WATCHDOG_ERROR : WATCHDOG_NOTICE);
}
}
/**
* Set or retrieve a message handler.
*/
function _backup_migrate_message_callback($callback = NULL) {
static $current_callback = '_backup_migrate_message_log';
if ($callback !== NULL) {
$current_callback = $callback;
}
return $current_callback;
}
function _backup_migrate_check_timeout() {
static $timeout;
// Max execution of 0 means unlimited.
if (ini_get('max_execution_time') == 0) {
return false;
}
// Figure out when we should stop execution.
if (!$timeout) {
$timeout = (!empty($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time()) + ini_get('max_execution_time') - variable_get('backup_migrate_timeout_buffer', 5);
}
return (time() > $timeout);
}
/**
* Convert an associated array to an ini format string.
*/
function _backup_migrate_array_to_ini($data, $prefix = '') {
$content = "";
foreach ($data as $key => $val) {
if ($prefix) {
$key = $prefix . '[' . $key .']';
}
if (is_array($val)) {
$content .= _backup_migrate_array_to_ini($val, $key);
}
else {
$content .= $key . " = \"". $val ."\"\n";
}
}
return $content;
}
/**
* Execute a command line command. Returns false if the function failed.
*/
function backup_migrate_exec($command, $args = array()) {
if (!function_exists('exec') || ini_get('safe_mode')) {
return FALSE;
}
// Escape the arguments
foreach ($args as $key => $arg) {
$args[$key] = escapeshellarg($arg);
}
$command = strtr($command, $args);
$output = $result = NULL;
// Run the command.
exec($command . ' 2>&1', $output, $result);
return $result == 0;
}
/**
* Implements hook_mail().
*/
function backup_migrate_mail($key, &$message, $params) {
switch($key) {
case 'backup_succeed':
$message['subject'] = t('!site backup succeeded', array('!site' => variable_get('site_name', 'Drupal')));
if ($params['messages']) {
$message['body'][] = t("The site backup has completed successfully with the following messages:");
$message['body'][] = t("!messages", array('!messages' => $params['messages']));
}
else {
$message['body'][] = t("The site backup has completed successfully.");
}
break;
case 'backup_fail':
$message['subject'] = t('!site backup failed', array('!site' => variable_get('site_name', 'Drupal')));
if ($params['messages']) {
$message['body'][] = t("The site backup has failed with the following messages:");
$message['body'][] = t("!messages", array('!messages' => $params['messages']));
}
else {
$message['body'][] = t("The site backup has failed for an unknown reason.");
}
break;
}
}