destinations.inc
37.7 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
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
<?php
/**
* @file
* All of the destination handling code needed for Backup and Migrate.
*/
backup_migrate_include('crud');
/**
* Get the available destination types.
*/
function backup_migrate_get_destination_types() {
$out = array();
foreach (module_implements('backup_migrate_destination_types') as $module) {
$fn = $module . '_backup_migrate_destination_types';
$out += $fn();
}
return $out;
}
/**
* Implementation of hook_backup_migrate_destination_types().
*
* Get the built in Backup and Migrate destination types.
*/
function backup_migrate_backup_migrate_destination_types() {
$out = array();
if (!module_exists('nodesquirrel')) {
$out += array(
'nodesquirrel' => array(
'description' => t('!link is the cloud backup service by the maintainers of Backup and Migrate.', array('!link' => l(t('NodeSquirrel'), 'http://nodesquirrel.com'))),
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.nodesquirrel.inc',
'class' => 'backup_migrate_destination_nodesquirrel',
'type_name' => t('NodeSquirrel.com'),
'can_create' => TRUE,
),
);
}
if (variable_get('backup_migrate_allow_backup_to_file', TRUE)) {
$out += array(
'file' => array(
'description' => t('Save the backup files to any local directory the web server can write to.'),
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.file.inc',
'class' => 'backup_migrate_destination_files',
'type_name' => t('Server Directory'),
'can_create' => TRUE,
),
'file_manual' => array(
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.file.inc',
'type_name' => t('Server Directory'),
'class' => 'backup_migrate_destination_files_manual',
),
'file_scheduled' => array(
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.file.inc',
'type_name' => t('Server Directory'),
'class' => 'backup_migrate_destination_files_scheduled',
),
);
}
$out += array(
'browser_download' => array(
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.browser.inc',
'class' => 'backup_migrate_destination_browser_download',
),
'browser_upload' => array(
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.browser.inc',
'class' => 'backup_migrate_destination_browser_upload',
),
'db' => array(
'type_name' => t('Database'),
'description' => t('Import the backup directly into another database. Database destinations can also be used as a source to backup from.'),
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.db.inc',
'class' => 'backup_migrate_destination_db',
'can_create' => FALSE,
),
'mysql' => array(
'type_name' => t('MySQL Database'),
'description' => t('Import the backup directly into another MySQL database. Database destinations can also be used as a source to backup from.'),
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.db.mysql.inc',
'class' => 'backup_migrate_destination_db_mysql',
'can_create' => TRUE,
),
'ftp' => array(
'description' => t('Save the backup files to any a directory on an FTP server.'),
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.ftp.inc',
'class' => 'backup_migrate_destination_ftp',
'type_name' => t('FTP Directory'),
'can_create' => TRUE,
),
's3' => array(
'description' => t('Save the backup files to a bucket on your <a href="@link">Amazon S3 account</a>.', array('@link' => url('http://aws.amazon.com/s3/'))),
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.s3.inc',
'class' => 'backup_migrate_destination_s3',
'type_name' => t('Amazon S3 Bucket'),
'can_create' => TRUE,
),
'email' => array(
'type_name' => t('Email'),
'description' => t('Send the backup as an email attachment to the specified email address.'),
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.email.inc',
'class' => 'backup_migrate_destination_email',
'can_create' => TRUE,
),
);
return $out;
}
/**
* Implementation of hook_backup_migrate_destinations().
*
* Get the built in backup destinations and those in the db.
*/
function backup_migrate_backup_migrate_destinations() {
$out = array();
// Add the default, out of the box destinations for new users.
if (variable_get('backup_migrate_allow_backup_to_file', TRUE)) {
if (variable_get('file_private_path', FALSE)) {
$out['manual'] = backup_migrate_create_destination('file_manual', array('destination_id' => 'manual'));
$out['scheduled'] = backup_migrate_create_destination('file_scheduled', array('destination_id' => 'scheduled'));
}
else {
_backup_migrate_message('You must specify a private file system path in the !settings to backup to the server.', array('!settings' => l(t('file system settings'), 'admin/config/media/file-system')), 'warning');
}
}
// Add the browser destination for downloading to the desktop.
if (variable_get('backup_migrate_allow_backup_to_download', TRUE)) {
$out['download'] = backup_migrate_create_destination('browser_download');
}
$out['upload'] = backup_migrate_create_destination('browser_upload');
if ($secret_key = variable_get('nodesquirrel_secret_key', '')) {
$out['nodesquirrel'] = backup_migrate_create_destination('nodesquirrel', array('destination_id' => 'nodesquirrel'));
$out['nodesquirrel']->settings['secret_key'] = $secret_key;
}
// Expose the configured databases as sources.
backup_migrate_include('filters');
$out += backup_migrate_filters_invoke_all('destinations');
/*
global $db_url;
$urls = is_array($db_url) ? $db_url : array('default' => $db_url);
foreach ((array)$urls as $key => $url) {
// Only mysql is currently supported. If more DBMs's are implemented, theis will need to be abstacted.
if (strpos($url, 'mysql') === 0) {
if ($destination = backup_migrate_create_destination('mysql', array('url' => $url))) {
// Treat the default database differently because it is probably the only one available.
if ($key == 'default') {
$destination->set_id('db');
$destination->set_name(t('Default Database'));
// Dissalow backing up to the default database because that's confusing and potentially dangerous.
unset($destination->supported_ops['scheduled backup']);
unset($destination->supported_ops['manual backup']);
}
else {
$destination->set_id('db:'. $key);
$destination->set_name($key .": ". $destination->get_display_location());
}
$out[$destination->get_id()] = $destination;
}
}
}
*/
return $out;
}
/**
* Get all the available backup destination.
*
* @param $op
* The operation which will be performed on the destination. Hooks can use this
* to return only those destinations appropriate for the given op.
* Options include:
* 'manual backup' - destinations available for manual backup
* 'scheduled backup' - destinations available for schedules backup
* 'list files' - destinations whose backup files can be listed
* 'restore' - destinations whose files can be restored from
* 'all' - all available destinations should be returned
*/
function backup_migrate_get_destinations($op = 'all') {
static $destinations = NULL;
// Get the list of destinations and cache them locally.
if ($destinations === NULL) {
$destinations = backup_migrate_crud_get_items('destination');
}
// Return all if that's what was asked for.
if ($op == 'all') {
return $destinations;
}
// Return only those destinations which support the given op.
$out = array();
foreach ($destinations as $key => $destination) {
if ($destination->op($op)) {
$out[$key] = $destination;
}
}
return $out;
}
/**
* Get the destination of the given id.
*/
function backup_migrate_get_destination($id) {
$destinations = backup_migrate_get_destinations('all');
return empty($destinations[$id]) ? NULL : $destinations[$id];
}
/**
* Create a destination object of the given type with the given params.
*/
function backup_migrate_create_destination($destination_type, $params = array()) {
$params['type'] = $destination_type;
// Create a new dummy destination to call the create method on because the base item create is not static.
$destination = new backup_migrate_destination();
return $destination->create($params);
}
/**
* Load a file from a destination and return the file info.
*/
function backup_migrate_destination_get_file($destination_id, $file_id) {
if ($destination = backup_migrate_get_destination($destination_id)) {
return $destination->load_file($file_id);
}
return NULL;
}
/**
* Check if a file exists in the given destination.
*/
function backup_migrate_destination_file_exists($destination_id, $file_id) {
if ($destination = backup_migrate_get_destination($destination_id)) {
return $destination->file_exists($file_id);
}
return NULL;
}
/**
* Send a file to the destination specified by the settings array.
*/
function backup_migrate_destination_save_file($file, &$settings) {
if ($destination = $settings->get_destination()) {
$file = $destination->save_file($file, $settings);
return $file;
}
return NULL;
}
/**
* Delete a file in the given destination.
*/
function backup_migrate_destination_delete_file($destination_id, $file_id) {
if ($destination = backup_migrate_get_destination($destination_id)) {
return $destination->delete_file($file_id);
}
}
/**
* Get the action links for a file on a given destination.
*/
function _backup_migrate_destination_get_file_links($destination_id, $file_id) {
$out = array();
if ($destination = backup_migrate_get_destination($destination_id)) {
$out = $destination->get_file_links($file_id);
}
return $out;
}
/* UI Menu Callbacks */
/**
* List the backup files in the given destination.
*/
function backup_migrate_ui_destination_display_files($destination_id = NULL) {
drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css');
$rows = $sort = array();
if ($destination = backup_migrate_get_destination($destination_id)) {
// Refresh the file listing cache if requested.
if (isset($_GET['refresh'])) {
$destination->file_cache_clear();
drupal_goto($_GET['q']);
}
drupal_set_title(t('@title Files', array('@title' => $destination->get_name())));
$headers = array(
array('data' => t('Filename'), 'field' => 'filename'),
array('data' => t('Date'), 'field' => 'filetime'),
array('data' => t('Age'), 'field' => 'filetime', 'sort' => 'desc'),
array('data' => t('Size'), 'field' => 'filesize'),
);
$sort_order = tablesort_get_order($headers);
$sort_key = $sort_order['sql'] ? $sort_order['sql'] : 'filetime';
$sort_dir = tablesort_get_sort($headers) == 'desc' ? SORT_DESC : SORT_ASC;
$files = $destination->list_files();
$i = 0;
$ops = 0;
foreach ((array)$files as $file) {
$info = $file->info();
$operations = $destination->get_file_links($file->file_id());
$description = '';
// Add the description as a new row.
if (!empty($info['description'])) {
$description = ' <div title="'.check_plain($info['description']).'" class="backup-migrate-description">'. $info['description'] .'</div>';
}
// Show only files that can be restored from.
if ($file->is_recognized_type()) {
$sort[] = $info[$sort_key];
$rows[] = array_merge(array(
check_plain($info['filename']) . $description,
format_date($info['filetime'], 'small'),
format_interval(time() - $info['filetime'], 1),
format_size($info['filesize']),
), $operations);
}
$ops = max($ops, count($operations));
}
// Add the operations if any
if ($ops) {
$headers[] = array('data' => t('Operations'), 'colspan' => $ops);
}
array_multisort($sort, $sort_dir, $rows);
if ($rows) {
$out = theme('table', array('header' => $headers, 'rows' => $rows));
}
else {
$out = t('There are no backup files to display.');
}
if ($destination->cache_files && $destination->fetch_time) {
drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css');
$out .= '<div class="backup-migrate-cache-time">'. t('This listing was fetched !time ago. !refresh', array('!time' => format_interval(time() - $destination->fetch_time, 1), '!refresh' => l(t('fetch now'), $_GET['q'], array('query' => array('refresh' => 'true'))))) .'</div>';
}
return $out;
}
drupal_goto(BACKUP_MIGRATE_MENU_PATH . "/destination");
}
/**
* Download a file to the browser.
*/
function backup_migrate_ui_destination_download_file($destination_id = NULL, $file_id = NULL) {
if ($file = backup_migrate_destination_get_file($destination_id, $file_id)) {
backup_migrate_include('files');
$file->transfer();
}
drupal_goto(BACKUP_MIGRATE_MENU_PATH);
}
/**
* Restore a backup file from a destination.
*/
function backup_migrate_ui_destination_restore_file($destination_id = NULL, $file_id = NULL) {
if (backup_migrate_destination_file_exists($destination_id, $file_id)) {
return drupal_get_form('backup_migrate_ui_destination_restore_file_confirm', $destination_id, $file_id);
}
drupal_goto(user_access('access backup files') ? BACKUP_MIGRATE_MENU_PATH . "/destination/list/files/". $destination_id : BACKUP_MIGRATE_MENU_PATH);
}
/**
* Ask confirmation for file restore.
*/
function backup_migrate_ui_destination_restore_file_confirm($form, &$form_state, $destination_id, $file_id) {
$sources = _backup_migrate_get_destination_form_item_options('source');
if (count($sources) > 1) {
$form['source_id'] = array(
"#type" => "select",
"#title" => t("Database"),
"#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['destination_id'] = array('#type' => 'value', '#value' => $destination_id);
$form['file_id'] = array('#type' => 'value', '#value' => $file_id);
$form = confirm_form($form, t('Are you sure you want to restore the database?'), BACKUP_MIGRATE_MENU_PATH . "/destination/list/files/". $destination_id, t('Are you sure you want to restore the database from the backup file %file_id? This will delete some or all of your data and cannot be undone. <strong>Always test your backups on a non-production server!</strong>', array('%file_id' => $file_id)), t('Restore'), t('Cancel'));
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'));
$form['actions']['#weight'] = 100;
// 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;
}
return $form;
}
/**
* Do the file restore.
*/
function backup_migrate_ui_destination_restore_file_confirm_submit($form, &$form_state) {
$destination_id = $form_state['values']['destination_id'];
$file_id = $form_state['values']['file_id'];
if ($destination_id && $file_id) {
backup_migrate_perform_restore($destination_id, $file_id, $form_state['values']);
}
$redir = user_access('access backup files') ? BACKUP_MIGRATE_MENU_PATH . "/destination/list/files/". $destination_id : BACKUP_MIGRATE_MENU_PATH;
$form_state['redirect'] = $redir;
}
/**
* Menu callback to delete a file from a destination.
*/
function backup_migrate_ui_destination_delete_file($destination_id = NULL, $file_id = NULL) {
if (backup_migrate_destination_file_exists($destination_id, $file_id)) {
return drupal_get_form('backup_migrate_ui_destination_delete_file_confirm', $destination_id, $file_id);
}
drupal_goto(BACKUP_MIGRATE_MENU_PATH);
}
/**
* Ask confirmation for file deletion.
*/
function backup_migrate_ui_destination_delete_file_confirm($form, &$form_state, $destination_id, $file_id) {
$form['destination_id'] = array('#type' => 'value', '#value' => $destination_id);
$form['file_id'] = array('#type' => 'value', '#value' => $file_id);
return confirm_form($form, t('Are you sure you want to delete the backup file?'), BACKUP_MIGRATE_MENU_PATH . '/destination/list/files/'. $destination_id, t('Are you sure you want to delete the backup file %file_id? <strong>This action cannot be undone.</strong>', array('%file_id' => $file_id)), t('Delete'), t('Cancel'));
}
/**
* Delete confirmed, perform the delete.
*/
function backup_migrate_ui_destination_delete_file_confirm_submit($form, &$form_state) {
if (user_access('delete backup files')) {
$destination_id = $form_state['values']['destination_id'];
$file_id = $form_state['values']['file_id'];
backup_migrate_destination_delete_file($destination_id, $file_id);
_backup_migrate_message('Database backup file deleted: %file_id', array('%file_id' => $file_id));
}
$form_state['redirect'] = user_access('access backup files') ? BACKUP_MIGRATE_MENU_PATH . "/destination/list/files/". $destination_id : BACKUP_MIGRATE_MENU_PATH;
}
/* Utilities */
/**
* Get the source options as a form element.
*/
function _backup_migrate_get_source_form($source_id = 'db') {
backup_migrate_include('destinations');
$form = array();
$sources = _backup_migrate_get_source_pulldown($source_id);
if (count($sources['#options']) > 1) {
$form['source'] = array(
"#type" => "fieldset",
"#title" => t("Backup Source"),
"#collapsible" => TRUE,
"#collapsed" => FALSE,
"#tree" => FALSE,
);
$sources['#description'] = t("Choose the database to backup. Any database destinations you have created and any databases specified in your settings.php can be backed up.");
$form['source']['source_id'] = $sources;
}
else {
$form = array();
$form['source']['source_id'] = array(
"#type" => "value",
"#value" => $source_id,
);
}
return $form;
}
/**
* Get pulldown to select existing source options.
*/
function _backup_migrate_get_source_pulldown($source_id = NULL) {
backup_migrate_include('destinations');
$sources = _backup_migrate_get_destination_form_item_options('source');
$form = array(
"#type" => "select",
"#title" => t("Backup Source"),
"#options" => _backup_migrate_get_destination_form_item_options('source'),
"#default_value" => $source_id,
);
return $form;
}
/**
* Get the destination options as an options array for a form item.
*/
function _backup_migrate_get_destination_form_item_options($op) {
$out = array();
foreach (backup_migrate_get_destinations($op) as $key => $destination) {
$out[$key] = $destination->get_name();
}
return $out;
}
/**
* A base class for creating destinations.
*/
class backup_migrate_destination extends backup_migrate_item {
var $db_table = "backup_migrate_destinations";
var $type_name = "destination";
var $default_values = array('settings' => array());
var $singular = 'destination';
var $plural = 'destinations';
var $cache_files = FALSE;
var $fetch_time = NULL;
var $cache_expire = 86400; // 24 hours
var $destination_type = "";
var $supported_ops = array();
/**
* This function is not supposed to be called. It is just here to help the po extractor out.
*/
function strings() {
// Help the pot extractor find these strings.
t('Destination');
t('Destinations');
t('destinations');
t('destination');
}
function ops() {
return $this->supported_ops;
}
/**
* Does this destination support the given operation.
*/
function op($op) {
$ops = (array)$this->ops();
return in_array($op, $ops);
}
/**
* Remove the given op from the support list.
*/
function remove_op($op) {
$key = array_search($op, $this->supported_ops);
if ($key !== FALSE) {
unset($this->supported_ops[$key]);
}
}
function get_name() {
return @$this->name;
}
function set_name($name) {
return $this->name = $name;
}
function set_type($type) {
$this->type = $type;
}
function set_location($location) {
$this->location = $location;
}
function get_location() {
return @$this->location;
}
function get_display_location() {
return $this->get_location();
}
function file_types() {
return array();
}
function settings($key = NULL) {
$out = $this->settings;
if ($key) {
$out = isset($out[$key]) ? $out[$key] : NULL;
}
return $out;
}
/**
* Get the type name of this destination for display to the user.
*/
function get_destination_type_name() {
if ($type = $this->destination_type) {
$types = backup_migrate_get_destination_types();
return isset($types[$type]['type_name']) ? $types[$type]['type_name'] : $type;
}
}
/**
* Save the given file to the destination.
*/
function save_file($file, $settings) {
// This must be overriden.
$this->file_cache_clear();
// Save the file metadata if the destination supports it.
$this->save_file_info($file, $settings);
return $this->_save_file($file, $settings);
}
/**
* Save the given file to the destination.
*/
function _save_file($file, $settings) {
// This must be overriden.
return $file;
}
/**
* Save the file metadata
*/
function save_file_info($file, $settings) {
$info = $this->create_info_file($file);
// Save the info file and the actual file.
return $this->_save_file($info, $settings);
}
/**
* Load the file with the given destination specific id and return as a backup_file object.
*/
function load_file($file_id) {
// This must be overriden.
return NULL;
}
/**
* Check if a file exists in the given destination.
*/
function file_exists($file_id) {
// Check if the file exists in the list of available files. Actual destination types may have more efficient ways of doing this.
$files = $this->list_files();
return isset($files[$file_id]);
}
/**
* List all the available files in the given destination with their destination specific id.
*/
function list_files() {
$out = NULL;
if ($this->cache_files) {
$out = $this->file_cache_get();
}
if ($out === NULL) {
$out = $this->_list_files();
$out = $this->load_files_info($out);
if ($this->cache_files) {
$this->file_cache_set($out);
}
}
return $out;
}
/**
* List all the available files in the given destination with their destination specific id.
*/
function _list_files() {
return array();
}
/**
* Load up the file's metadata from the accompanying .info file if applicable.
*/
function load_files_info($files) {
foreach ($files as $key => $file) {
if (isset($files[$key . '.info'])) {
// See if there is an info file with the same name as the backup.
$info = drupal_parse_info_file($files[$key . '.info']->filepath());
// Allow the stored metadata to override the detected metadata.
$file->file_info = $info + $file->file_info;
// Remove the metadata file from the list
unset($files[$key . '.info']);
}
}
return $files;
}
/**
* Create an ini file and write the meta data.
*/
function create_info_file($file) {
$info = $this->_file_info_file($file);
$data = _backup_migrate_array_to_ini($file->file_info);
$info->put_contents($data);
return $info;
}
/**
* Create the info file object.
*/
function _file_info_file($file) {
$info = new backup_file(array('filename' => $this->_file_info_filename($file->file_id())));
return $info;
}
/**
* Determine the file name of the info file for a file.
*/
function _file_info_filename($file_id) {
return $file_id . '.info';
}
/**
* Cache the file list.
*/
function file_cache_set($files) {
cache_set('backup_migrate_file_list:'. $this->get_id(), $files, 'cache', time() + $this->cache_expire);
}
/**
* Retrieve the file list.
*/
function file_cache_get() {
backup_migrate_include('files');
$cache = cache_get('backup_migrate_file_list:'. $this->get_id());
if (!empty($cache->data) && $cache->created > (time() - $this->cache_expire)) {
$this->fetch_time = $cache->created;
return $cache->data;
}
$this->fetch_time = 0;
return NULL;
}
/**
* Retrieve the file list.
*/
function file_cache_clear() {
if ($this->cache_files) {
$this->file_cache_set(NULL);
}
}
/**
* Delete the file with the given destination specific id.
*/
function delete_file($file_id) {
$this->file_cache_clear();
$this->_delete_file($file_id);
$this->_delete_file($this->_file_info_filename($file_id));
}
/**
* Delete the file with the given destination specific id.
*/
function _delete_file($file_id) {
// This must be overriden.
}
/**
* Get the edit form for the item.
*/
function edit_form() {
if (get_class($this) !== 'backup_migrate_destination') {
$form = parent::edit_form();
$form['name'] = array(
"#type" => "textfield",
"#title" => t("Destination name"),
"#default_value" => $this->get_name(),
"#required" => TRUE,
);
$form['type'] = array(
"#type" => "value",
"#default_value" => $this->destination_type,
);
}
else {
$types = backup_migrate_get_destination_types();
$items = array();
// If no (valid) node type has been provided, display a node type overview.
foreach ($types as $key => $type) {
if (@$type['can_create']) {
$type_url_str = str_replace('_', '-', $key);
$out = '<dt>'. l($type['type_name'], BACKUP_MIGRATE_MENU_PATH . "/destination/list/add/$type_url_str", array('attributes' => array('title' => t('Add a new @s destination.', array('@s' => $type['type_name']))))) .'</dt>';
$out .= '<dd>'. filter_xss_admin($type['description']) .'</dd>';
$items[] = $out;
}
}
if (count($items)) {
$output = t('Choose the type of destination you would like to create:') .'<dl>'. implode('', $items) .'</dl>';
}
else {
$output = t('No destination types available.');
}
$form['select_type'] = array(
'#type' => 'markup',
'#markup' => $output,
);
}
return $form;
}
/**
* Get the message to send to the user when confirming the deletion of the item.
*/
function delete_confirm_message() {
return t('Are you sure you want to delete the destination %name? Backup files already saved to this destination will not be deleted.', array('%name' => $this->get_name()));
}
/**
* Get the columns needed to list the type.
*/
function get_list_column_info() {
$out = parent::get_list_column_info();
$out = array(
'name' => array('title' => t('Name')),
'destination_type_name' => array('title' => t('Type')),
'display_location' => array('title' => t('Location')),
) + $out;
return $out;
}
/**
* Get a row of data to be used in a list of items of this type.
*/
function get_list_row() {
$out = parent::get_list_row();
// Supress destinations with no actions as there's no value in showing them (and they may confuse new users).
if (empty($out['actions'])) {
return NULL;
}
return $out;
}
/**
* Get the action links for a destination.
*/
function get_action_links() {
$out = parent::get_action_links();
$item_id = $this->get_id();
// Don't display the download/delete/restore ops if they are not available for this destination.
if ($this->op('list files') && user_access("access backup files")) {
$out = array('list files' => l(t("list files"), BACKUP_MIGRATE_MENU_PATH . "/$this->type_name/list/files/". $item_id)) + $out;
}
if (!$this->op('configure') || !user_access('administer backup and migrate')) {
unset($out['edit']);
}
return $out;
}
/**
* Get the action links for a file on a given destination.
*/
function get_file_links($file_id) {
$out = array('download' => '', 'restore' => '', 'delete' => '');
// Don't display the download/delete/restore ops if they are not available for this destination.
$can_read = $this->can_read_file($file_id);
$can_delete = $this->can_delete_file($file_id);
$destination_id = $this->get_id();
if ($can_read && user_access("access backup files")) {
$out['download'] = l(t("download"), BACKUP_MIGRATE_MENU_PATH . "/destination/downloadfile/". $destination_id .'/'. $file_id);
}
if ($can_read && user_access("restore from backup")) {
$out['restore'] = l(t("restore"), BACKUP_MIGRATE_MENU_PATH . "/destination/restorefile/". $destination_id .'/'. $file_id);
}
if ($can_delete && user_access("delete backup files")) {
$out['delete'] = l(t("delete"), BACKUP_MIGRATE_MENU_PATH . "/destination/deletefile/". $destination_id .'/'. $file_id);
}
return $out;
}
/**
* Determine if we can read the given file.
*/
function can_read_file($file_id) {
return $this->op('restore');
}
/**
* Determine if we can read the given file.
*/
function can_delete_file($file_id) {
return $this->op('delete');
}
/**
* Get the form for the settings for this destination type.
*/
function settings_default() {
return array();
}
/**
* Get the form for the settings for this destination.
*/
function settings_form($form) {
return $form;
}
/**
* Validate the form for the settings for this destination.
*/
function settings_form_validate($form_values) {
}
/**
* Submit the settings form. Any values returned will be saved.
*/
function settings_form_submit($form_values) {
return $form_values;
}
/**
* Create a new destination of the correct type.
*/
function create($params = array()) {
$out = NULL;
$types = backup_migrate_get_destination_types();
// Get the type passed in in the params, or if none, check the url for a valid type name.
// This is to allow new destination type to be specified in the path.
$destination_type = !empty($params['type']) ? $params['type'] : arg(BACKUP_MIGRATE_MENU_DEPTH + 3);
if ($destination_type && ($type = @$types[$destination_type])) {
// Include the necessary file if specified by the type.
if (!empty($type['file'])) {
require_once './'. $type['file'];
}
$out = new $type['class']($params + array('destination_type' => $destination_type));
}
if (empty($out)) {
$out = new backup_migrate_destination();
}
return $out;
}
/**
* Add the menu items specific to the destination type.
*/
function get_menu_items() {
$items = parent::get_menu_items();
$items[BACKUP_MIGRATE_MENU_PATH . '/destination/list/files'] = array(
'title' => 'Destination Files',
'page callback' => 'backup_migrate_menu_callback',
'page arguments' => array('destinations', 'backup_migrate_ui_destination_display_files', TRUE),
'access arguments' => array('access backup files'),
'type' => MENU_LOCAL_TASK,
);
$items[BACKUP_MIGRATE_MENU_PATH . '/destination/deletefile'] = array(
'title' => 'Delete File',
'description' => 'Delete a backup file',
'page callback' => 'backup_migrate_menu_callback',
'page arguments' => array('destinations', 'backup_migrate_ui_destination_delete_file', TRUE),
'access arguments' => array('delete backup files'),
'type' => MENU_CALLBACK,
);
$items[BACKUP_MIGRATE_MENU_PATH . '/destination/restorefile'] = array(
'title' => 'Restore from backup',
'description' => 'Restore database from a backup file on the server',
'page callback' => 'backup_migrate_menu_callback',
'page arguments' => array('destinations', 'backup_migrate_ui_destination_restore_file', TRUE),
'access arguments' => array('restore from backup'),
'type' => MENU_CALLBACK,
);
$items[BACKUP_MIGRATE_MENU_PATH . '/destination/downloadfile'] = array(
'title' => 'Download File',
'description' => 'Download a backup file',
'page callback' => 'backup_migrate_menu_callback',
'page arguments' => array('destinations', 'backup_migrate_ui_destination_download_file', TRUE),
'access arguments' => array('access backup files'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Get the form for the settings for this filter.
*/
function backup_settings_default() {
return array();
}
/**
* Get the form for the settings for this filter.
*/
function backup_settings_form($settings) {
return array();
}
/**
* Get the form for the settings for this filter.
*/
function backup_settings_form_validate($form, &$form_state) {
}
/**
* Submit the settings form. Any values returned will be saved.
*/
function backup_settings_form_submit($form, &$form_state) {
}
/**
* Get the form for the settings for this filter.
*/
function restore_settings_default() {
return array();
}
/**
* Get the form for the settings for this filter.
*/
function restore_settings_form($settings) {
return array();
}
/**
* Get the form for the settings for this filter.
*/
function restore_settings_form_validate($form_values) {
}
/**
* Submit the settings form. Any values returned will be saved.
*/
function restore_settings_form_submit($form_values) {
return $form_values;
}
}
/**
* A base class for creating destinations.
*/
class backup_migrate_destination_remote extends backup_migrate_destination {
/**
* The location is a URI so parse it and store the parts.
*/
function get_location() {
return $this->url(FALSE);
}
/**
* The location to display is the url without the password.
*/
function get_display_location() {
return $this->url(TRUE);
}
/**
* Return the location with the password.
*/
function set_location($location) {
$this->location = $location;
$this->set_url($location);
}
/**
* Get a url from the parts.
*/
function url($hide_password = TRUE) {
return $this->glue_url($this->dest_url, $hide_password);
}
/**
* Glue a URLs component parts back into a URL.
*/
function glue_url($parts, $hide_password = TRUE) {
// Obscure the password if we need to.
$parts['pass'] = $hide_password ? "" : $parts['pass'];
// Assemble the URL.
$out = "";
$out .= $parts['scheme'] .'://';
$out .= $parts['user'] ? urlencode($parts['user']) : '';
$out .= ($parts['user'] && $parts['pass']) ? ":". urlencode($parts['pass']) : '';
$out .= ($parts['user'] || $parts['pass']) ? "@" : "";
$out .= $parts['host'];
$out .= !empty($parts['port']) ? ':'. $parts['port'] : '';
$out .= "/". $parts['path'];
return $out;
}
/**
* Break a URL into it's component parts.
*/
function set_url($url) {
$parts = (array)parse_url($url);
$parts['user'] = urldecode(@$parts['user']);
$parts['pass'] = urldecode(@$parts['pass']);
$parts['path'] = urldecode(@$parts['path']);
$parts['path'] = ltrim(@$parts['path'], "/");
$this->dest_url = $parts;
}
/**
* Destination configuration callback.
*/
function edit_form() {
$form = parent::edit_form();
$form['scheme'] = array(
"#type" => "value",
"#title" => t("Scheme"),
"#default_value" => @$this->dest_url['scheme'] ? $this->dest_url['scheme'] : 'mysql',
"#required" => TRUE,
// "#options" => array($GLOBALS['db_type'] => $GLOBALS['db_type']),
"#weight" => 0,
);
$form['host'] = array(
"#type" => "textfield",
"#title" => t("Host"),
"#default_value" => @$this->dest_url['host'] ? $this->dest_url['host'] : 'localhost',
"#required" => TRUE,
"#weight" => 10,
);
$form['path'] = array(
"#type" => "textfield",
"#title" => t("Path"),
"#default_value" => @$this->dest_url['path'],
"#required" => TRUE,
"#weight" => 20,
);
$form['user'] = array(
"#type" => "textfield",
"#title" => t("Username"),
"#default_value" => @$this->dest_url['user'],
"#required" => TRUE,
"#weight" => 30,
);
$form['pass'] = array(
"#type" => "password",
"#title" => t("Password"),
"#default_value" => @$this->dest_url['pass'],
'#description' => '',
"#weight" => 40,
);
if (@$this->dest_url['pass']) {
$form['old_password'] = array(
"#type" => "value",
"#value" => @$this->dest_url['pass'],
);
$form['pass']["#description"] .= t(' You do not need to enter a password unless you wish to change the currently saved password.');
}
return $form;
}
/**
* Submit the configuration form. Glue the url together and add the old password back if a new one was not specified.
*/
function edit_form_submit($form, &$form_state) {
$form_state['values']['pass'] = $form_state['values']['pass'] ? $form_state['values']['pass'] : $form_state['values']['old_password'];
$form_state['values']['location'] = $this->glue_url($form_state['values'], FALSE);
parent::edit_form_submit($form, $form_state);
}
}