v1.1.0 2021-10-07 08:38 UTC

This package is auto-updated.

Last update: 2024-09-07 12:47:01 UTC


README

为PHP项目管理备份。此仓库包含主接口、基本管理器、作业和清除规则。

用法

此包围绕备份管理器的理念构建,所有备份作业都注册在此管理器下。管理器还使用清除规则清除旧备份,以保持备份使用的存储在控制之下。

示例

// init
$purgeRule = new ElektroPotkan\Backups\PurgeRules\Rolling;

$manager = new ElektroPotkan\Backups\Managers\Locking('path-to-backups-directory', 'MyApp-v1.47.2', $purgeRule);

$manager->addJob('config', new ElektroPotkan\Backups\Jobs\FileCopy('config/main.cfg'));
$manager->addJob('readme', new ElektroPotkan\Backups\Jobs\File('txt', 'Some README notes to keep in the backup for future restore'));
$manager->addJob('data', new ElektroPotkan\Backups\Jobs\Callback('json',
	function(string $path) use($jsonSource): void {
		if($jsonSource->hasData()){
			file_put_contents($path, $jsonSource->getData());
		}
	}
));

// run periodically
$manager->create();
$manager->purge();

// backend management
$manager->list();
$manager->get($id);
$manager->delete($id);

// backup details
$backup = $manager->list()[0];
$backup->id;
$backup->time;
$backup->size;

$file = $backup->files[0];
$file->name;
$file->path;
$file->size;

自定义备份作业

class MyJob implements ElektroPotkan\Backups\IJob {
	private $api;
	
	
	public function __construct($api){
		$this->api = $api;
	}
	
	public function create(string $path): void {
		if($this->api->hasData()){
			$this->api->saveData($path);
		}
	}
	
	public function getExtension(): string {
		return 'data';
	}
}


// register the new job
$myJob = new MyJob($myApi);

$manager->addJob('api', $myJob);

自定义清除规则

class MyRule implements ElektroPotkan\Backups\IPurgeRule {
	public function keepOrPurge(DateTimeInterface $dt, DateTimeInterface $now): bool {
		// keep backups from last 12 days and from January 1 of each year
		return $dt >= Nette\Utils\DateTime::from($now)->modify('- 12 days')->setTime(0, 0, 0, 0) || $dt->format('z') === '0';
	}
}

$purgeRule = new MyRule;

作业文件名

作业文件使用以下架构命名

timestamp_date_name_job-name.ext

例如,对于配置了MyApp-v1.47.2作为$name参数的管理器,在2021-08-30 20:07:24 UTC创建的备份,作业以test命名并返回扩展名txt,生成的文件名将是

1630354044_2021-08-30_MyApp-v1.47.2_test.txt

日志记录

要记录内置管理器的信息和错误,您需要通过调用setLogger提供ElektroPotkan\Backups\ILogger接口的实现

// $logger implements ElektroPotkan\Backups\ILogger
$manager->setLogger($logger);

如果您正在使用Tracy,可以使用提供的TracyLogger

// $logger implements Tracy\ILogger
$manager->setLogger(new ElektroPotkan\Backups\TracyLogger($logger));

作者

Elektro-potkan git@elektro-potkan.cz

信息

版本控制

此项目使用语义版本控制 2.0.0 (semver.org)

分支

此项目使用略微修改的Git-Flow工作流程和分支模型

许可证

您可以在BSD Zero Clause License或GNU通用公共许可证(GPL)版本3或更高版本下使用此程序。

请参阅文件LICENSE