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
|
<?php
// DO NOT CALL THIS CLASS DIRECTLY. CALL VIA: pb_backupbuddy_destination in bootstrap.php.
class pb_backupbuddy_destination_local { // Change class name end to match destination name. public static $destination_info = array( 'name' => 'Local Directory', 'description' => 'Send files to another directory on this server / hosting account. This is useful for storing copies locally in another location. This is also a possible destination for automated migrations.', ); // Default settings. Should be public static for auto-merging. public static $default_settings = array( 'type' => 'local', // MUST MATCH your destination slug. Required destination field. 'title' => '', // Required destination field. 'path' => '', // Local file path for destination. 'url' => '', // Corresponding web URL for this location. 'created_at' => 0, 'temporary' => false, 'archive_limit' => '0', 'disable_file_management' => '0', // When 1, _manage.php will not load which renders remote file management DISABLED. ); /* send() * * Send one or more files. * * @param array $files Array of one or more files to send. * @return boolean True on success, else false. */ public static function send( $settings = array(), $files = array(), $send_id = '' ) { $limit = $settings['archive_limit']; $path = $settings['path']; if ( !file_exists( $settings['path'] ) ) { pb_backupbuddy::$filesystem->mkdir( $settings['path'] ); } $total_transfer_time = 0; $total_transfer_size = 0; foreach( $files as $file ) { pb_backupbuddy::status( 'details', 'Starting send to `' . $path . '`.' ); $filesize = filesize( $file ); $total_transfer_size += $filesize; $send_time = -(microtime( true )); if ( true !== @copy( $file, $path . '/' . basename( $file ) ) ) { pb_backupbuddy::status( 'error', 'Unable to copy file `' . $file . '` of size `' . pb_backupbuddy::$format->file_size( $filesize ) . '` to local path `' . $path . '`. Please verify the directory exists and permissions permit writing.' ); backupbuddy_core::mail_error( $error_message ); return false; } else { pb_backupbuddy::status( 'details', 'Send success.' ); } $send_time += microtime( true ); $total_transfer_time += $send_time; // Start remote backup limit if ( $limit > 0 ) { pb_backupbuddy::status( 'details', 'Archive limit of `' . $limit . '` in settings.' ); pb_backupbuddy::status( 'details', 'path: ' . $path . '*.zip' ); $remote_files = glob( $path . '/*.zip' ); if ( !is_array( $remote_files ) ) { $remote_files = array(); } usort( $remote_files, create_function('$a,$b', 'return filemtime($a) - filemtime($b);' ) ); pb_backupbuddy::status( 'details', 'Found `' . count( $remote_files ) . '` backups.' ); // Create array of backups and organize by date $bkupprefix = backupbuddy_core::backup_prefix(); foreach( $remote_files as $file_key => $remote_file ) { if ( false === stripos( $remote_file, 'backup-' . $bkupprefix . '-' ) ) { pb_backupbuddy::status( 'details', 'backup-' . $bkupprefix . '-' . 'not in file: ' . $remote_file ); unset( $backups[$file_key] ); } } arsort( $remote_files ); pb_backupbuddy::status( 'details', 'Found `' . count( $remote_files ) . '` backups.' ); if ( ( count( $remote_files ) ) > $limit ) { pb_backupbuddy::status( 'details', 'More archives (' . count( $remote_files ) . ') than limit (' . $limit . ') allows. Trimming...' ); $i = 0; $delete_fail_count = 0; foreach( $remote_files as $remote_file ) { $i++; if ( $i > $limit ) { pb_backupbuddy::status ( 'details', 'Trimming excess file `' . $remote_file . '`...' ); if ( !unlink( $remote_file ) ) { pb_backupbuddy::status( 'details', 'Unable to delete excess local file `' . $remote_file . '`.' ); $delete_fail_count++; } } } pb_backupbuddy::status( 'details', 'Finished trimming excess backups.' ); if ( $delete_fail_count !== 0 ) { $error_message = 'Local remote limit could not delete ' . $delete_fail_count . ' backups.'; pb_backupbuddy::status( 'error', $error_message ); backupbuddy_core::mail_error( $error_message ); } } } else { pb_backupbuddy::status( 'details', 'No local destination file limit to enforce.' ); } // End remote backup limit } // end foreach. // Load fileoptions to the send. pb_backupbuddy::status( 'details', 'About to load fileoptions data.' ); require_once( pb_backupbuddy::plugin_path() . '/classes/fileoptions.php' ); pb_backupbuddy::status( 'details', 'Fileoptions instance #11.' ); $fileoptions_obj = new pb_backupbuddy_fileoptions( backupbuddy_core::getLogDirectory() . 'fileoptions/send-' . $send_id . '.txt', $read_only = false, $ignore_lock = false, $create_file = false ); if ( true !== ( $result = $fileoptions_obj->is_ok() ) ) { pb_backupbuddy::status( 'error', __('Fatal Error #9034.2344848. Unable to access fileoptions data.', 'it-l10n-backupbuddy' ) . ' Error: ' . $result ); return false; } pb_backupbuddy::status( 'details', 'Fileoptions data loaded.' ); $fileoptions = &$fileoptions_obj->options; $fileoptions['write_speed'] = ( $total_transfer_time / $total_transfer_size ); return true; } // End send(). /* test() * * Tests ability to write to this remote destination. * TODO: Should this delete the temporary test directory to clean up after itself? * * @param array $settings Destination settings. * @return bool|string True on success, string error message on failure. */ public static function test( $settings, $files = array() ) { $path = rtrim( $settings['path'], '/\\' ); $url = rtrim( $settings['url'], '/\\' ); if ( !file_exists( $path ) ) { pb_backupbuddy::$filesystem->mkdir( $path ); } if ( is_writable( $path ) !== true ) { return __('Failure', 'it-l10n-backupbuddy' ) . '; The path does not allow writing. Please verify write file permissions.'; } if ( $url != '' ) { $test_filename = 'migrate_test_' . pb_backupbuddy::random_string( 10 ) . '.php'; $test_file_path = $path . '/' . $test_filename; $test_file_url = $url . '/' . $test_filename; // Make file. file_put_contents( $test_file_path, "<?php die( '1' ); ?>" ); pb_backupbuddy::status( 'details', 'Local test: Veryifing `' . $test_file_url . '` points to `' . $test_file_path . '`.' ); // Test URL points to file. $response = wp_remote_get( $test_file_url, array( 'method' => 'GET', 'timeout' => 45, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => null, 'cookies' => array() ) ); unlink( $test_file_path ); if ( is_wp_error( $response ) ) { return __( 'Failure. Unable to connect to the provided URL.', 'it-l10n-backupbuddy' ); } if ( trim( $response['body'] ) != '1' ) { return __('Failure. The path appears valid but the URL does not correspond to it. Leave the URL blank if not using this destination for migrations.', 'it-l10n-backupbuddy' ); } } // Made it this far so success. return true; } // End test(). } // End class.
|