/** * @test */ public function adds_directory_to_collection_with_exclusion() { $backup = new Backup($this->dropboxInstance(), $this->working); $backup->addJob(new Job(new DirectoryJob($this->css_directory(), $this->assets, [$this->css_components_directory()]), 'directories')); $backup->prepare(); $backup->processDirectories(); $this->assertCount(1, $backup->getCollection()['directories'], 'There is more then 1 item in the "directories" collection'); }
/** * Execute job. * * @return void */ public function execute() { $remoteDirectory = $this->backup->getRemoteDirectory(); $archiveFile = $this->backup->getArchiveName(); if (!$this->backup->manager->has("remote://{$remoteDirectory}")) { $this->backup->manager->createDir("remote://{$remoteDirectory}"); } $this->backup->manager->move('local://' . $archiveFile, 'remote://' . $this->backup->getRemoteDirectory() . '/' . $archiveFile); }
/** * Add files from the collection to the archive. * * @return void */ private function processCollection() { foreach ($this->backup->getCollection() as $namespace => $items) { foreach ($items as $item) { if (is_dir($item['path'])) { $this->archive->addEmptyDir($this->namespacedName($namespace, $item)); } else { $this->archive->addFile($item['path'], $this->namespacedName($namespace, $item)); } } } }
/** * Remove old backup files. * * @return void */ public function clearOutdated() { $files = $this->backup->manager->listContents('remote://' . $this->backup->getRemoteDirectory(), true); $count = count($files); if (empty($files) || $count <= $this->backup->getNumberOfBackups()) { return; } $remove = $count - $this->backup->getNumberOfBackups(); asort($files); foreach ($files as $key => $file) { if ($key + 1 > $remove) { return; } $this->backup->manager->delete('remote://' . $this->backup->getRemoteDirectory() . '/' . $file['basename']); } }
/** * @test */ public function adds_jobs() { $backup = new Backup($this->dropboxInstance(), $this->working); $backup->addJob(new Job(new MySQLDatabase(['host' => 'foo', 'name' => 'bar', 'user' => 'abc', 'password' => 'password']), 'database')); $backup->addJob(new Job(new PostgreSQLDatabase(['host' => 'foo', 'name' => 'bar', 'user' => 'abc', 'password' => 'password']), 'database')); $backup->addJob(new Job(new File($this->css_file(), $this->css_directory()))); $backup->addJob(new Job(new Directory($this->css_components_directory(), $this->css_directory()))); $jobs = $backup->getJobs(); $this->assertEquals(4, count($jobs), 'Number of jobs does not equal 4'); $this->assertEquals('database', $jobs[0]->namespace, 'MySQLDatabase job namespace is not set to "database"'); $this->assertInstanceOf(MySQLDatabase::class, $jobs[0]->job, 'Job is not instance of MySQLDatabase'); $this->assertInstanceOf(PostgreSQLDatabase::class, $jobs[1]->job, 'Job is not instance of PostgreSQLDatabase'); $this->assertEmpty($jobs[2]->namespace, 'File job namespace is not empty'); $this->assertInstanceOf(File::class, $jobs[2]->job, 'Job is not instance of File'); $this->assertEmpty($jobs[3]->namespace, 'Directory job namespace is not empty'); $this->assertInstanceOf(Directory::class, $jobs[3]->job, 'Job is not instance of Directory'); }
/** * Process single backup job. * * @param DatabaseContract $database * @param string $namespace */ private function instance(DatabaseContract $database, $namespace = '') { $file = $this->workingDir . '/' . $database->fileName(); if (is_file($file)) { unlink($file); } $filesystems = new Filesystems\FilesystemProvider($this->fileSystemConfig()); $filesystems->add(new Filesystems\LocalFilesystem()); $databases = new Databases\DatabaseProvider($this->databaseConfig($database)); $databases->add(new Databases\MysqlDatabase()); $databases->add(new Databases\PostgresqlDatabase()); $compressors = new Compressors\CompressorProvider(); $compressors->add(new Compressors\NullCompressor()); $manager = new Manager($filesystems, $databases, $compressors); $manager->makeBackup()->run('config', [new Destination('local', $database->fileName())], 'null'); $this->backup->addToCollection(['name' => $database->fileName(), 'path' => $file], $namespace); $this->backup->addToRemoval($file); }
/** * @test */ public function adds_file_to_collection() { $backup = new Backup($this->dropboxInstance(), $this->working); $backup->addJob(new Job(new FileJob($this->css_file(), $this->assets), 'files')); $backup->addJob(new Job(new FileJob($this->terms_file(), $this->assets), 'files')); $backup->prepare(); $backup->processFiles(); $this->assertCount(1, $backup->getCollection(), 'Files are not in the collection'); $this->assertCount(2, $backup->getCollection()['files'], 'There are more or less than 2 items in the "files" item of the collection'); }
/** * @test */ public function cleans_collection() { $backup = new Backup($this->dropboxInstance(), $this->working); $backup->addJob(new Job(new File($this->terms_file(), $this->assets))); $backup->prepare(); $backup->processFiles(); $archive = new Archive($backup, new ZipArchive()); $archive->execute(); $this->assertCount(1, $backup->getCollection(), 'Collection does not contain 1 item'); $this->assertFileExists($backup->archivePath()); $cleanup = new Cleanup($backup); $cleanup->execute(); $this->assertEmpty($backup->getCollection(), 'Collection is not empty'); $this->assertFileNotExists($backup->archivePath()); }
/** * @test */ public function creates_archive() { $backup = new Backup($this->dropboxInstance(), $this->working); $backup->addJob(new Job(new File($this->terms_file(), $this->assets))); $backup->prepare(); $backup->processFiles(); $archive = new Archive($backup, new ZipArchive()); $archive->execute(); $this->add_file_to_remove($backup->archivePath()); $this->assertFileExists($backup->archivePath()); }
$dotenv = new DotEnv([__DIR__ . '/.env']); $dotenv->load(); $dotenv->required(['DROPBOX_SECRET', 'DROPBOX_OAUTH', 'FTP_HOST', 'FTP_USER', 'FTP_PASS', 'DOMAIN_NAME', 'DB_HOST', 'DB_PORT', 'DB_NAME', 'DB_USER', 'DB_PASS']); // working directory $workingDirectory = __DIR__ . '/tmp'; try { $remote = new Dropbox(getenv('DROPBOX_OAUTH'), getenv('DROPBOX_SECRET')); //$remote = new Ftp( // getenv('FTP_HOST'), // getenv('FTP_USER'), // getenv('FTP_PASS'), // [ // 'root' => 'public_html' // ] //); $backup = new Backup($remote, $workingDirectory); // directory to which backup should be saved on the remote server $backup->setRemoteDirectory(getenv('DOMAIN_NAME')); // keep only 7 backups then overwrite the oldest one $backup->setNumberOfBackups(7); // add MySQL database to the backup $backup->addJob(new Job(new MySQLDatabase(['host' => getenv('DB_HOST'), 'name' => getenv('DB_NAME'), 'user' => getenv('DB_USER'), 'password' => getenv('DB_PASS')]), 'database')); // add single file ot the backup $backup->addJob(new Job(new File(__DIR__ . '/files/text.txt', __DIR__))); // add the entire directory to the backup $backup->addJob(new Job(new Directory(__DIR__ . '/files', __DIR__, ['files/css']))); // run backup $backup->run(); } catch (Exception $e) { $file = $workingDirectory . DIRECTORY_SEPARATOR . 'error_log'; file_put_contents($file, $e->getMessage() . PHP_EOL, FILE_APPEND | LOCK_EX);