fipamo/app/Services/MaintenanceService.php
ro f8005aa60d
turned on backups
ported over the backup functionality from the old build to the new while
making  few tweaks

instead of packaging up all files in the site to create massive zip
files, now a list of files is created for the user and blog directories
that the system will use to download and put them in the appropriate
directories, resulting in a such slimmer backup file.

archiving images my be added at a later date, but will be kept seperate
from the current back up process
2024-04-05 12:29:38 -06:00

97 lines
3.6 KiB
PHP

<?php
namespace App\Services;
use Carbon\Carbon;
class MaintenanceService
{
protected $settings;
public function __construct(SettingsService $settingsService)
{
$this->settings = $settingsService;
}
public function createBackUp()
{
//make sure back directory is there
$stamp = Carbon::now()->format("YmdGis");
if (!is_dir('../content/backups')) {
mkdir('../content/backups', 0755, true);
}
//creat backup zip
$zip = new \ZipArchive();
$zip->open(
'../content/backups/backup-' . $stamp . '.zip',
\ZipArchive::CREATE | \ZipArchive::OVERWRITE
);
//gather data and path info for md pages
$pagePath = '../content/pages';
$yearPaths = glob($pagePath . '/*', GLOB_ONLYDIR);
foreach ($yearPaths as $years) {
$year = explode('/', $years);
//grap the index and save it
if (trim($year[3]) == 'start') {
$options = [
'add_path' => 'content/pages/' . $year[3] . '/',
'remove_all_path' => true,
];
$zip->addGlob($years . '/*.md', GLOB_BRACE, $options);
}
$monthsPath = glob($pagePath . '/' . $year[3] . '/*', GLOB_ONLYDIR);
foreach ($monthsPath as $months) {
$month = explode('/', $months);
//once info is collected, add md pages to zip
$options = [
'add_path' => 'content/pages/' . $year[3] . '/' . $month[4] . '/',
'remove_all_path' => true,
];
$zip->addGlob($months . '/*.md', GLOB_BRACE, $options);
}
}
//gather paths for blog images
$blogImages = [];
$dir = new \RecursiveDirectoryIterator('../public/assets/images/blog');
$flat = new \RecursiveIteratorIterator($dir);
$files = new \RegexIterator($flat, '/\.png|jpg|gif$/i');
foreach ($files as $file) {
$blogImages[] = ['path' => $file->getPath(), 'file' => $file->getFilename()];
};
//gather paths for user images
$userImages = [];
$dir = new \RecursiveDirectoryIterator('../public/assets/images/user');
$flat = new \RecursiveIteratorIterator($dir);
$files = new \RegexIterator($flat, '/\.png|jpg|gif$/i');
foreach ($files as $file) {
$userImages[] = ['path' => $file->getPath(), 'file' => $file->getFilename()];
};
//add directory for settings and save them
$zip->addEmptyDir('config');
$zip->addEmptyDir('images');
$zip->addFile(env('SETTINGS_PATH'), 'config/settings.json');
$zip->addFile(env('FOLKS_PATH'), 'config/folks.json');
$zip->addFile(env('TAGS_PATH'), 'config/tags.json');
// create temp files for image lists
file_put_contents('../content/backups/blog_temp.json', json_encode($blogImages));
file_put_contents('../content/backups/user_temp.json', json_encode($userImages));
//add to zip
$zip->addFile('../content/backups/blog_temp.json', 'images/blog.json');
$zip->addFile('../content/backups/user_temp.json', 'images/user.json');
//save zip file
$zip->close();
//clean up temp files
unlink('../content/backups/blog_temp.json');
unlink('../content/backups/user_temp.json');
//update settings file with latest back up date
$this->settings->updateGlobalData('last_backup', $stamp);
return ['message' => "Backup created. THIS IS A SAFE SPACE!"];
}
}