backup_migrate.drush.inc
9.37 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
<?php
/**
* @file
* Drush commands for backup and migrate.
*/
/**
* Implementation of hook_drush_command().
*/
function backup_migrate_drush_command() {
$items['bam-backup'] = array(
'callback' => 'backup_migrate_drush_backup',
'description' => dt('Backup the site\'s database with Backup and Migrate.'),
'aliases' => array('bb'),
'examples' => array(
'drush bam-backup' => 'Backup the default database to the manual backup directory using the default settings.',
'drush bam-backup db scheduled mysettings' => 'Backup the database to the scheduled directory using a settings profile called "mysettings"',
'drush bam-backup files' => 'Backup the files directory to the manual directory using the default settings. The Backup and Migrate Files module is required for files backups.',
),
'arguments' => array(
'source' => "Optional. The id of the source (usually a database) to backup. Use 'drush bam-sources' to get a list of sources. Defaults to 'db'",
'destination' => "Optional. The id of destination to send the backup file to. Use 'drush bam-destinations' to get a list of destinations. Defaults to 'manual'",
'profile' => "Optional. The id of a settings profile to use. Use 'drush bam-profiles' to get a list of available profiles. Defaults to 'default'",
),
);
$items['bam-restore'] = array(
'callback' => 'backup_migrate_drush_restore',
'description' => dt('Restore the site\'s database with Backup and Migrate.'),
'arguments' => array(
'source' => "Required. The id of the source (usually a database) to restore the backup to. Use 'drush bam-sources' to get a list of sources. Defaults to 'db'",
'destination' => "Required. The id of destination to send the backup file to. Use 'drush bam-destinations' to get a list of destinations. Defaults to 'manual'",
'backup id' => "Required. The id of a backup file restore. Use 'drush bam-backups' to get a list of available backup files.",
),
'options' => array(
'yes' => 'Skip confirmation',
),
);
$items['bam-destinations'] = array(
'callback' => 'backup_migrate_drush_destinations',
'description' => dt('Get a list of available destinations.'),
);
$items['bam-sources'] = array(
'callback' => 'backup_migrate_drush_sources',
'description' => dt('Get a list of available sources.'),
);
$items['bam-profiles'] = array(
'callback' => 'backup_migrate_drush_profiles',
'description' => dt('Get a list of available settings profiles.'),
);
$items['bam-backups'] = array(
'callback' => 'backup_migrate_drush_destination_files',
'description' => dt('Get a list of previously created backup files.'),
'arguments' => array(
'destination' => "Required. The id of destination to list backups from. Use 'drush bam-destinations' to get a list of destinations.",
),
);
return $items;
}
/**
* Implementation of hook_drush_help().
*/
function backup_migrate_drush_help($section) {
switch ($section) {
case 'drush:bam-backup':
return dt("Backup the site's database using default settings.");
case 'drush:bam-restore':
return dt('Restore the site\'s database with Backup and Migrate.');
case 'drush:bam-destinations':
return dt('Get a list of available destinations.');
case 'drush:bam-profiles':
return dt('Get a list of available settings profiles.');
case 'drush:bam-backups':
return dt('Get a list of previously created backup files.');
}
}
/**
* Backup the default database.
*/
function backup_migrate_drush_backup($source_id = 'db', $destination_id = 'manual', $profile_id = 'default') {
backup_migrate_include('profiles', 'destinations');
// Set the message mode to logging.
_backup_migrate_message_callback('_backup_migrate_message_drush');
if (!backup_migrate_get_destination($source_id)) {
_backup_migrate_message("Could not find the source '@source'. Try using 'drush bam-sources' to get a list of available sources or use 'db' to backup the Drupal database.", array('@source' => $source_id), 'error');
return;
}
if (!backup_migrate_get_destination($destination_id)) {
_backup_migrate_message("Could not find the destination '@destination'. Try using 'drush bam-destinations' to get a list of available destinations.", array('@destination' => $destination_id), 'error');
return;
}
$settings = backup_migrate_get_profile($profile_id);
if(!$settings) {
_backup_migrate_message("Could not find the profile '@profile'. Try using 'drush bam-profiles' to get a list of available profiles.", array('@profile' => $profile_id), 'error');
return;
}
_backup_migrate_message('Starting backup...');
$settings->destination_id = $destination_id;
$settings->source_id = $source_id;
backup_migrate_perform_backup($settings);
}
/**
* Restore to the default database.
*/
function backup_migrate_drush_restore($source_id = '', $destination_id = '', $file_id = '') {
drush_print(dt('Restoring will delete some or all of your data and cannot be undone. ALWAYS TEST YOUR BACKUPS ON A NON-PRODUCTION SERVER!'));
if (!drush_confirm(dt('Are you sure you want to restore the database?'))) {
return drush_user_abort();
}
backup_migrate_include('profiles', 'destinations');
// Set the message mode to drush output.
_backup_migrate_message_callback('_backup_migrate_message_drush');
if (!backup_migrate_get_destination($source_id)) {
_backup_migrate_message("Could not find the source '@source'. Try using 'drush bam-sources' to get a list of available sources or use 'db' to backup the Drupal database.", array('@source' => $source_id), 'error');
return;
}
if (!$destination = backup_migrate_get_destination($destination_id)) {
_backup_migrate_message("Could not find the destination '@destination'. Try using 'drush bam-destinations' to get a list of available destinations.", array('@destination' => $destination_id), 'error');
return;
}
if (!$file_id || !$file = backup_migrate_destination_get_file($destination_id, $file_id)) {
_backup_migrate_message("Could not find the file '@file'. Try using 'drush bam-backups @destination' to get a list of available backup files in this destination destinations.", array('@destination' => $destination_id, '@file' => $file_id), 'error');
return;
}
_backup_migrate_message('Starting restore...');
$settings = array('source_id' => $source_id);
backup_migrate_perform_restore($destination_id, $file_id, $settings);
}
/**
* Get a list of available destinations.
*/
function backup_migrate_drush_destinations() {
return _backup_migrate_drush_destinations('all');
}
/**
* Get a list of available sources.
*/
function backup_migrate_drush_sources() {
return _backup_migrate_drush_destinations('source');
}
/**
* Get a list of available destinations with the given op.
*/
function _backup_migrate_drush_destinations($op = NULL) {
backup_migrate_include('destinations');
$rows = array(array(dt('ID'), dt('Name'), dt('Operations')));
foreach (backup_migrate_get_destinations($op) as $destination) {
$rows[] = array(
$destination->get_id(),
$destination->get_name(),
implode (', ', $destination->ops()),
);
}
drush_print_table($rows, TRUE, array(32, 32));
}
/**
* Get a list of available profiles.
*/
function backup_migrate_drush_profiles() {
backup_migrate_include('profiles');
$rows = array(array(dt('ID'), dt('Name')));
foreach (backup_migrate_get_profiles() as $profile) {
$rows[] = array(
$profile->get_id(),
$profile->get_name(),
);
}
drush_print_table($rows, TRUE, array(32, 32));
}
/**
* Get a list of files in a given destination
*/
function backup_migrate_drush_destination_files($destination_id = NULL) {
backup_migrate_include('destinations');
// Set the message mode to drush output.
_backup_migrate_message_callback('_backup_migrate_message_drush');
if (!$destination_id) {
_backup_migrate_message("You must specify an existing destination. Try using 'drush bam-destinations' to get a list of available destinations.", array('@destination' => $destination_id), 'error');
return;
}
if (!$destination = backup_migrate_get_destination($destination_id)) {
_backup_migrate_message("Could not find the destination '@destination'. Try using 'drush bam-destinations' to get a list of available destinations.", array('@destination' => $destination_id), 'error');
return;
}
$out = array(array(
dt('Filename'),
dt('Date'),
dt('Age'),
dt('Size'),
));
$files = $destination->list_files();
$i = 0;
foreach ((array)$files as $file) {
// Show only files that can be restored from.
if ($file->is_recognized_type()) {
$info = $file->info();
$out[] = array(
check_plain($info['filename']),
format_date($info['filetime'], 'small'),
format_interval(time() - $info['filetime'], 1),
format_size($info['filesize']),
);
}
}
if (count($out) > 1) {
drush_print_table($out, TRUE);
}
else {
drush_print(dt('There are no backup files to display.'));
}
}
/**
* Send a message to the drush log.
*/
function _backup_migrate_message_drush($message, $replace, $type) {
// Use drush_log to display to the user.
drush_log(strip_tags(dt($message, $replace)), str_replace('status', 'notice', $type));
// Watchdog log the message as well for admins.
_backup_migrate_message_log($message, $replace, $type);
}