public function generate()
{
if (PHP_SAPI != 'cli') {
throw new \Exception("This script only can be used in CLI");
}
$config = $this->config->get('database');
system(sprintf('/usr/bin/mysqldump -u %s -h %s -p%s -r /tmp/phosphorum.sql %s', $config->username, $config->host, $config->password, $config->dbname));
system('bzip2 -f /tmp/phosphorum.sql');
$config = $this->config->get('dropbox');
if (!$config instanceof Config) {
throw new \Exception("Unable to retrieve Dropbox credentials. Please check Forum Configuration");
}
if (!$config->get('appSecret') || !$config->get('accessToken')) {
throw new \Exception("Please provide correct 'appSecret' and 'accessToken' config values");
}
$sourcePath = '/tmp/phosphorum.sql.bz2';
if (!file_exists($sourcePath)) {
throw new \Exception("Backup could not be created");
}
$client = new Client($config->get('accessToken'), $config->get('appSecret'));
$adapter = new DropboxAdapter($client, $config->get('prefix', null));
$filesystem = new Filesystem($adapter);
$dropboxPath = '/phosphorum.sql.bz2';
if ($filesystem->has($dropboxPath)) {
$filesystem->delete($dropboxPath);
}
$fp = fopen($sourcePath, "rb");
$filesystem->putStream($dropboxPath, $fp);
fclose($fp);
@unlink($sourcePath);
}