Skip to content

Commit

Permalink
0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gvozdb committed Jul 14, 2020
1 parent f939caf commit 4c457d2
Show file tree
Hide file tree
Showing 16 changed files with 649 additions and 191 deletions.
18 changes: 17 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
# Changelog

## [0.2.0] - 2020-07-14

* Refactoring
* Added output of error messages
* Added information messages output
* Added recording of logs to file
* Added sending of logs to mail
* Added sending of logs to Telegram Chat
* Changed README.md


## [0.1.5] - 2019-05-21

* Added param **users** for **Backup::run()**


## [0.1.4] - 2018-11-21

* Added **class_exists** for storage in Dumper\Backup
* Added **class_exists** for storage in `Dumper\Backup`
* Changed README.md


## [0.1.3] - 2018-11-07

* Fixed composer.json, changed version of `arhitector/yandex` to ~2.0


## [0.1.2] - 2018-11-07

* Fixed composer.json, added property `prefer-stable`


## [0.1.1] - 2018-11-07

* Changed README.md


## [0.1.0] - 2018-11-06

* First release
48 changes: 46 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,67 @@ try {
### config.yaml
Конфигурационный файл приложения.
```yaml
#
main:
prefix: "%Y%m%d-" #
expires: &main.expires 4 #
clean_logs: true #

#
path:
tmp: '/tmp/dumper/%Y%m%d/' #
users: '/var/www/' #
root: '/root/' #
log: '/var/log/' #
etc: '/etc/' #

#
storages:
# Upload to YandexDisk
YandexDisk:
token: 'AQAAAAABEJ2-AAVH0ERr79Yz4E5dpd-7nhV1W18' #
path: 'disk:/Dumper/%Y%m%d/' #
token: 'AQAAAAABEJ2-AAVH0EIr79Yz4E5dpd-7nhV1W18' #
path: 'disk:/Dumper/ServerIP/%Y%m%d/' #
expires: *main.expires #

#
logs:
enabled: true #
notify:
# Print to console
Console:
path: 'php://stdout' #
#level: 'info' #
#format: "[%datetime%] [%level_name%] > %message%\n" #
#dateFormat: 'd.m.Y H:i:s' #

# Write to file
File:
path: './log/%Y%m%d.log' #
#level: 'info' #
#format: "[%datetime%] [%level_name%] > %message%\n" #
#dateFormat: 'd.m.Y H:i:s' #

# Send to email
Email:
host: '' #
port: 465 #
encryption: 'ssl' #
username: '' #
password: '' #
subject: '[%d.%m.%Y] Dumper Report' #
from: '' #
to: '' #
#level: 'info' #
#format: "[%datetime%] [%level_name%] > %message%\n" #
dateFormat: 'H:i:s' #

# Send to telegram chat
Telegram:
token: '' #
chat: '' #
#level: 'info' #
#format: "[%datetime%] [%level_name%] > %message%\n" #
dateFormat: 'H:i:s' #
```
Поместить в директорию с cron.php
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"arhitector/yandex": "~2.0",
"symfony/filesystem": "~3.4|~4.0",
"symfony/yaml": "~3.4|~4.0",
"symfony/console": "~3.4|~4.0"
"symfony/console": "~3.4|~4.0",
"monolog/monolog": "^2.1",
"swiftmailer/swiftmailer": "^6.2"
},
"autoload": {
"psr-4": {
Expand Down
43 changes: 42 additions & 1 deletion examples/cron/config_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,48 @@ path:

#
storages:
# Upload to YandexDisk
YandexDisk:
token: 'AQAAAAABEJ2-AAVH0EIr79Yz4E5dpd-7nhV1W18' #
path: 'disk:/Dumper/%Y%m%d/' #
path: 'disk:/Dumper/ServerIP/%Y%m%d/' #
expires: *main.expires #

#
logs:
enabled: true #
notify:
# Print to console
Console:
path: 'php://stdout' #
#level: 'info' #
#format: "[%datetime%] [%level_name%] > %message%\n" #
#dateFormat: 'd.m.Y H:i:s' #

# Write to file
File:
path: './log/%Y%m%d.log' #
#level: 'info' #
#format: "[%datetime%] [%level_name%] > %message%\n" #
#dateFormat: 'd.m.Y H:i:s' #

# Send to email
Email:
host: '' #
port: 465 #
encryption: 'ssl' #
username: '' #
password: '' #
subject: '[%d.%m.%Y] Dumper Report' #
from: '' #
to: '' #
#level: 'info' #
#format: "[%datetime%] [%level_name%] > %message%\n" #
dateFormat: 'H:i:s' #

# Send to telegram chat
Telegram:
token: '' #
chat: '' #
#level: 'info' #
#format: "[%datetime%] [%level_name%] > %message%\n" #
dateFormat: 'H:i:s' #
128 changes: 93 additions & 35 deletions src/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@

class Backup
{
/**
* @var Logger\Handler $log
*/
public $log;
/**
* @var array $storages
*/
public $storages = [];
/**
* @var array $config
*/
protected $config = [];

/**
* @param Config\Load $config
*
* @throws \Exception
*/
public function __construct(Config\Load $config)
{
Expand All @@ -24,16 +34,25 @@ public function __construct(Config\Load $config)
if (!file_exists($this->config['path']['tmp'])) {
@mkdir($this->config['path']['tmp'], 0755, true);
}
// print_r($this->config);

//
$this->log = new Logger\Handler($this->config['logs']);
}

/**
* @param array $users
*
* @throws \ReflectionException
* @return void|bool
*
* @throws \Exception
*/
public function run(array $users = [])
{
if (!is_dir($this->config['path']['users'])) {
$this->log->emergency('Directory with users has been not found.');
return false;
}

$tasks = [];
$prefix = $this->config['main']['prefix'];

Expand All @@ -46,73 +65,112 @@ public function run(array $users = [])

$path = $this->config['path']['users'] . $k;

$config = new Config\Load($path . '/dumper.yaml', [
'src' => $path,
'dest' => $this->config['path']['tmp'] . $prefix . 'www-' . $k,
]);
$config = $config->toArray();
try {
$config = new Config\Load($path . '/dumper.yaml', [
'key' => $k,
'src' => $path,
'dest' => $this->config['path']['tmp'] . $prefix . 'www-' . $k,
]);
$config = $config->toArray();
} catch (\Exception $e) {
$this->log->error("Could not read user config `{$k}`. Message: " . $e->getMessage());
continue;
}

$tmp = new Path\User($config);
if ($tmp->isEnabled()) {
$tasks[] = $tmp;
try {
$task = new Path\User($this, $config);
if ($task->enabled()) {
$tasks[] = $task;
}
unset($task, $config, $path);
} catch (\Exception $e) {
$this->log->error($e->getMessage());
continue;
}
unset($tmp, $config, $path);
}
}
unset($dir);

//
if (!empty($this->config['path']['log'])) {
$tasks[] = new Path\Log([
'enabled' => true,
'clean_logs' => $this->config['main']['clean_logs'],
'src' => $this->config['path']['log'],
'dest' => $this->config['path']['tmp'] . $prefix . 'log',
]);
foreach (['root', 'etc'] as $k) {
if (!empty($this->config['path'][$k])) {
try {
$tasks[] = new Path\System($this, [
'key' => $k,
'src' => $this->config['path'][$k],
'dest' => $this->config['path']['tmp'] . $prefix . $k,
'enabled' => true,
]);
} catch (\Exception $e) {
$this->log->error($e->getMessage());
}
}
}

//
foreach (['root', 'etc'] as $k) {
if (!empty($this->config['path'][$k])) {
$tasks[] = new Path\System([
if (!empty($this->config['path']['log'])) {
try {
$tasks[] = new Path\Log($this, [
'key' => 'log',
'src' => $this->config['path']['log'],
'dest' => $this->config['path']['tmp'] . $prefix . 'log',
'clean_logs' => $this->config['main']['clean_logs'],
'enabled' => true,
'src' => $this->config['path'][$k],
'dest' => $this->config['path']['tmp'] . $prefix . $k,
]);
} catch (\Exception $e) {
$this->log->error($e->getMessage());
}
}

//
$storages = [];
foreach ($this->config['storages'] as $class => $config) {
if (!class_exists("\Gvozdb\Dumper\Storage\\{$class}")) {
$this->log->error("Storage handler `{$class}` not found.");
continue;
}
$storage = new \ReflectionClass("\Gvozdb\Dumper\Storage\\{$class}");
$storages[$class] = $storage->newInstance($config);
unset($storage);
try {
$storage = new \ReflectionClass("\Gvozdb\Dumper\Storage\\{$class}");
$storageInstance = $storage->newInstance($config);
if ($storageInstance->enabled()) {
$this->storages[$class] = $storageInstance;
}
} catch (\Exception $e) {
$this->log->error("It was not possible to initialize the storage instance `{$class}`. Message: " . $e->getMessage());
}
}
unset($class, $config, $storage, $storageInstance);

//
/** @var Path\AbstractPath $task */
foreach ($tasks as $task) {
$task->run($storages);
$task->run();
}

//
$this->clean($storages);
$this->clean();

//
$this->log->bufferReset();

return true;
}

/**
* @param array $storages
*
*/
protected function clean($storages = [])
protected function clean()
{
//
if (!empty($storages)) {
/** @var Storage\AbstractStorage $storage */
foreach ($storages as $storage) {
$storage->clean();
if (!empty($this->storages)) {
/** @var Storage\AbstractStorage $storageInstance */
foreach ($this->storages as $storageClass => $storageInstance) {
try {
if ($storageInstance->clean() === true) {
$this->log->info("Successfully cleaning old backups in the storage `{$storageClass}`.");
}
} catch (\Exception $e) {
$this->log->error("Could not remove old backups in the storage `{$storageClass}`. Message: " . $e->getMessage());
}
}
}

Expand Down
Loading

0 comments on commit 4c457d2

Please sign in to comment.