public function generate() { if (PHP_SAPI != 'cli') { throw new \Exception("This script only can be used in CLI"); } $config = $this->config->database; system('/usr/bin/mysqldump -u ' . $config->username . ' -p' . $config->password . ' -r /tmp/phosphorum.sql ' . $config->dbname); system('bzip2 /tmp/phosphorum.sql'); $sourcePath = '/tmp/phosphorum.sql.bz2'; if (!file_exists($sourcePath)) { throw new \Exception("Backup could not be created"); } list($accessToken, $host) = AuthInfo::loadFromJsonFile(APP_PATH . '/app/config/backup.auth'); $client = new Client($accessToken, "phosphorum", null, $host); $dropboxPath = '/phosphorum.sql.bz2'; $pathError = Path::findErrorNonRoot($dropboxPath); if ($pathError !== null) { throw new \Exception("Invalid <dropbox-path>: {$pathError}"); } try { $client->delete($dropboxPath); } catch (\Exception $e) { // ... } $size = null; if (\stream_is_local($sourcePath)) { $size = \filesize($sourcePath); } $fp = fopen($sourcePath, "rb"); $client->uploadFile($dropboxPath, WriteMode::add(), $fp, $size); fclose($fp); @unlink($sourcePath); }
/** * (non-PHPDoc) * * @see \phpbu\App\Backup\Sync::sync() * @param \phpbu\App\Backup\Target $target * @param \phpbu\App\Result $result * @throws \phpbu\App\Backup\Sync\Exception */ public function sync(Target $target, Result $result) { $sourcePath = $target->getPathname(); $dropboxPath = $this->path . $target->getFilename(); $client = new DropboxApi\Client($this->token, "phpbu/1.1.0"); $pathError = DropboxApi\Path::findErrorNonRoot($dropboxPath); if (substr(__FILE__, 0, 7) == 'phar://') { DropboxApi\RootCertificates::useExternalPaths(); } if ($pathError !== null) { throw new Exception(sprintf('Invalid \'dropbox-path\': %s', $pathError)); } $size = null; if (stream_is_local($sourcePath)) { $size = filesize($sourcePath); } try { $fp = fopen($sourcePath, 'rb'); $res = $client->uploadFile($dropboxPath, DropboxApi\WriteMode::add(), $fp, $size); fclose($fp); } catch (\Exception $e) { throw new Exception($e->getMessage(), null, $e); } $result->debug('upload: done (' . $res['size'] . ')'); }
/** * {@inheritdoc} */ public function upload($archive) { $fileName = explode('/', $archive); $pathError = Dropbox\Path::findErrorNonRoot($this->remotePath); if ($pathError !== null) { throw new UploadException(sprintf('Invalid path "%s".', $archive)); } $client = new Dropbox\Client($this->access_token, 'CloudBackupBundle'); $size = filesize($archive); $fp = fopen($archive, 'rb'); $client->uploadFile($this->remotePath . '/' . end($fileName), Dropbox\WriteMode::add(), $fp, $size); fclose($fp); }
#!/usr/bin/env php <?php require_once __DIR__ . '/helper.php'; use Dropbox as dbx; list($client, $dropboxPath, $localPath) = parseArgs("download-file", $argv, array(array("dropbox-path", "The path of the file (on Dropbox) to download."), array("local-path", "The local path to save the downloaded file contents to."))); $pathError = dbx\Path::findErrorNonRoot($dropboxPath); if ($pathError !== null) { fwrite(STDERR, "Invalid <dropbox-path>: {$pathError}\n"); die; } $metadata = $client->getFile($dropboxPath, fopen($localPath, "wb")); if ($metadata === null) { fwrite(STDERR, "File not found on Dropbox.\n"); die; } print_r($metadata); echo "File contents written to \"{$localPath}\"\n";
$errors = ''; foreach ($all_entries as $key => $value) { foreach ($all_entries[$key] as $entry) { list($dropbox_path, $metadata) = $entry; if (preg_match($preg_match_filter, $dropbox_path)) { if ($metadata === null) { // (-) $count_removes++; } else { // (+) if ($metadata['is_dir'] == 1) { $count_dirs++; } else { // add_file $local_path = $output_path . $metadata['path']; $path_error = dbx\Path::findErrorNonRoot($dropbox_path); if ($path_error !== null) { $error = "invalid <dropbox_path>: {$dropbox_path} ({$path_error}) \n"; $errors .= $error; } $original_bytes = $dropbox_path_file_to_meta[$dropbox_path]['bytes']; if (file_exists($local_path) && $original_bytes == filesize($local_path)) { $count_old_adds++; } else { $count_new_adds++; // if the dir does not exist create it $dirname = dirname($local_path); if (!is_dir($dirname)) { mkdir($dirname, 0755, true); } if ($report_to_screen == 1) {
public function create_object($is_file, $folder, $objname, $str, $mimetype) { // http://dropbox.github.io/dropbox-sdk-php/api-docs/v1.1.x/class-Dropbox.Client.html#_uploadFile if ($is_file) { $pathError = dbx\Path::findErrorNonRoot($folder . "/" . $objname); if ($pathError !== null) { fwrite(STDERR, "Invalid <dropbox-path>: {$pathError}\n"); die; } $size = null; if (\stream_is_local($sourcePath)) { $size = \filesize($sourcePath); } $fp = fopen($objname, "rb"); $metadata = $this->store->uploadFile($this->key_url($folder, $objname), dbx\WriteMode::add(), $fp, $size); fclose($fp); //print_r($metadata); } else { $metadata = $this->store->uploadFileFromString($this->key_url($folder, $objname), dbx\WriteMode::add(), '' . $str); //print_r($metadata); } }