|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * backup_db.module |
| 6 | + */ |
| 7 | + |
| 8 | +use Drupal\Core\StreamWrapper\PrivateStream; |
| 9 | +use Drupal\backup_db\Adapter\BackupDatabaseLocalAdapter; |
| 10 | +use Drupal\Core\Database\Database; |
| 11 | + |
| 12 | +/** |
| 13 | + * Inserts an export entry. |
| 14 | + * |
| 15 | + * @param array $file |
| 16 | + * The file. |
| 17 | + * |
| 18 | + * @return \Drupal\Core\Database\StatementInterface|int|string|null |
| 19 | + * |
| 20 | + * @throws \Exception |
| 21 | + */ |
| 22 | +function backup_db_history_insert($file) { |
| 23 | + $connection = Database::getConnection(); |
| 24 | + return $connection->insert('backup_db') |
| 25 | + ->fields(['fid', 'name', 'uri', 'created']) |
| 26 | + ->values([ |
| 27 | + 'fid' => $file['fid'], |
| 28 | + 'name' => $file['name'], |
| 29 | + 'uri' => $file['uri'], |
| 30 | + 'created' => \Drupal::time()->getRequestTime(), |
| 31 | + ]) |
| 32 | + ->execute(); |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Return export history entry. |
| 37 | + * |
| 38 | + * @param int $id |
| 39 | + * The entry id. |
| 40 | + * |
| 41 | + * @return mixed |
| 42 | + */ |
| 43 | +function backup_db_history_load($id) { |
| 44 | + $connection = Database::getConnection(); |
| 45 | + return $connection->query('SELECT * FROM {backup_db} WHERE eid = :id', [':id' => $id]) |
| 46 | + ->fetchAll(); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Delete history entry. |
| 51 | + * |
| 52 | + * @param int $id |
| 53 | + * The file id. |
| 54 | + * |
| 55 | + * @return bool |
| 56 | + * Return the status. |
| 57 | + * |
| 58 | + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException |
| 59 | + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException |
| 60 | + * @throws \Drupal\Core\Entity\EntityStorageException |
| 61 | + */ |
| 62 | +function backup_db_history_delete($id) { |
| 63 | + $status = FALSE; |
| 64 | + |
| 65 | + // Remove backup_db entry. |
| 66 | + \Drupal::database()->delete('backup_db') |
| 67 | + ->condition('fid', $id) |
| 68 | + ->execute(); |
| 69 | + |
| 70 | + // Remove file and managed entry. |
| 71 | + $file = \Drupal::entityTypeManager()->getStorage('file')->load($id); |
| 72 | + if ($file) { |
| 73 | + $status = $file->delete(); |
| 74 | + } |
| 75 | + |
| 76 | + return $status; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Return list of all tables. |
| 81 | + */ |
| 82 | +function backup_db_show_tables() { |
| 83 | + $connection = Database::getConnection(); |
| 84 | + return $connection->query('SHOW TABLES')->fetchAll(); |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * File_create_url in combination with Url::fromUri does not. |
| 89 | + * |
| 90 | + * Return a correct path for private files. |
| 91 | + * |
| 92 | + * @param string $uri |
| 93 | + * The uri. |
| 94 | + * |
| 95 | + * @return string |
| 96 | + * Return the link. |
| 97 | + */ |
| 98 | +function backup_db_link($uri) { |
| 99 | + $scheme = \Drupal::service('stream_wrapper_manager')->getScheme($uri); |
| 100 | + $path = $uri; |
| 101 | + if ($scheme == 'private') { |
| 102 | + $private_path = PrivateStream::basePath(); |
| 103 | + $path = str_replace('private:/', $private_path, $uri); |
| 104 | + } |
| 105 | + return $path; |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Format results as select list options. |
| 110 | + */ |
| 111 | +function _backup_db_format_options($results) { |
| 112 | + $options = []; |
| 113 | + foreach ($results as $result) { |
| 114 | + $value = current((array) $result); |
| 115 | + $options[$value] = $value; |
| 116 | + } |
| 117 | + return $options; |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Implements hook_cron(). |
| 122 | + */ |
| 123 | +function backup_db_cron() { |
| 124 | + $config = \Drupal::config('backup_db.settings'); |
| 125 | + if ($config->get('cron_backup_enabled')) { |
| 126 | + $interval = (int) $config->get('cron_interval') * 60 * 60; |
| 127 | + $expires = Drupal::state()->get('backup_db.cron_last_run') ?: \Drupal::time()->getRequestTime(); |
| 128 | + $next = Drupal::state()->get('backup_db.cron_next_backup') ?: $expires + $interval; |
| 129 | + if (\Drupal::time()->getRequestTime() > $next) { |
| 130 | + $client = \Drupal::service('backup_db.client'); |
| 131 | + // Use the local adapter for cron export. |
| 132 | + $handler = new BackupDatabaseLocalAdapter($client); |
| 133 | + $handler->export(); |
| 134 | + \Drupal::state()->set('backup_db.cron_next_backup', \Drupal::time()->getRequestTime() + $interval); |
| 135 | + } |
| 136 | + \Drupal::state()->set('backup_db.cron_last_run', \Drupal::time()->getRequestTime()); |
| 137 | + } |
| 138 | +} |
0 commit comments