-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fcb0f34
commit a37d558
Showing
3 changed files
with
29 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
composer.phar | ||
/vendor/ | ||
data | ||
.idea | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,57 @@ | ||
<?php | ||
|
||
// Example: custom finder function that finds files in ftp by using a custom function instead of scanning the file system | ||
// Example: custom prune function that deletes files from ftp | ||
// Example: custom prune function that deletes files from ftp (use xlight ftp server for quick testing) | ||
|
||
use PhpRetention\FileInfo; | ||
use PhpRetention\Retention; | ||
|
||
require_once __DIR__ . "/../vendor/autoload.php"; | ||
|
||
$ftpConnection = ftp_connect("ftp.example.com"); | ||
$ftpConnection = ftp_connect("localhost"); | ||
if (!$ftpConnection) { | ||
die('Could not connect to the FTP server'); | ||
} | ||
$loginResult = ftp_login($ftpConnection, "ftp-user", "ftp-pass"); | ||
$loginResult = ftp_login($ftpConnection, "test", "123456"); | ||
if (!$loginResult) { | ||
die('FTP login failed'); | ||
} | ||
|
||
$ret = new Retention([ | ||
'keep-last' => 2 | ||
'keep-daily' => 3, | ||
'keep-weekly' => 2 | ||
]); | ||
$ret->setFindHandler(function (string $targetDir) use ($ftpConnection) { | ||
$files = []; | ||
$fileList = ftp_mlsd($ftpConnection, $targetDir) ?: []; | ||
foreach ($fileList as $file) { | ||
$filename = $file['name']; | ||
$time = (int) $file['modify']; | ||
|
||
if (preg_match('/^backup_\w+\.zip$/', $filename)) { | ||
$date = new DateTimeImmutable('now', new DateTimeZone('UTC')); | ||
$date->setTimestamp($time); | ||
|
||
$filepath = "$targetDir/$filename"; | ||
|
||
$files[] = new FileInfo( | ||
date: $date, | ||
path: $filepath, | ||
isDirectory: false | ||
); | ||
if (preg_match('/^backup\-\w+$/', $filename)) { | ||
if (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})/', $file['modify'], $matches)) { | ||
$year = intval($matches[1]); | ||
$month = intval($matches[2]); | ||
$day = intval($matches[3]); | ||
|
||
$date = new DateTimeImmutable('now', new DateTimeZone('UTC')); | ||
$date = $date->setDate($year, $month, $day)->setTime(0, 0, 0); | ||
|
||
$filepath = "$targetDir/$filename"; | ||
$files[] = new FileInfo( | ||
date: $date, | ||
path: $filepath, | ||
isDirectory: false | ||
); | ||
} | ||
} | ||
} | ||
|
||
return $files; | ||
}); | ||
$ret->setPruneHandler(function (FileInfo $fileInfo) use ($ftpConnection) { | ||
ftp_delete($ftpConnection, $fileInfo->path); | ||
return ftp_delete($ftpConnection, $fileInfo->path); | ||
}); | ||
$ret->apply("/path/to/dir"); | ||
$keptFiles = $ret->apply("/backup"); | ||
|
||
print_r($keptFiles); | ||
|
||
ftp_close($ftpConnection); |