1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 06:32:40 +01:00
invoiceninja/app/Console/Commands/BackupUpdate.php

118 lines
2.9 KiB
PHP
Raw Normal View History

2021-10-20 05:05:46 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2021-10-20 05:05:46 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Console\Commands;
use App\Libraries\MultiDB;
use App\Models\Backup;
2022-11-06 22:20:14 +01:00
use App\Models\Company;
2022-11-11 04:52:50 +01:00
use App\Models\Document;
2021-10-20 05:05:46 +02:00
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
class BackupUpdate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
2022-11-06 22:20:14 +01:00
protected $signature = 'ninja:backup-files {--disk=}';
2021-10-20 05:05:46 +02:00
/**
* The console command description.
*
* @var string
*/
2022-11-06 22:20:14 +01:00
protected $description = 'Shift files between object storage locations';
2021-10-20 05:05:46 +02:00
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//always return state to first DB
$current_db = config('database.default');
if (! config('ninja.db.multi_db_enabled')) {
$this->handleOnDb();
} else {
//multiDB environment, need to
foreach (MultiDB::$dbs as $db) {
MultiDB::setDB($db);
$this->handleOnDb();
}
MultiDB::setDB($current_db);
2021-10-20 05:05:46 +02:00
}
}
private function handleOnDb()
{
2022-02-19 06:11:30 +01:00
set_time_limit(0);
2022-03-05 04:11:16 +01:00
2022-11-06 22:20:14 +01:00
//logos
2022-11-11 04:52:50 +01:00
Company::cursor()
2023-02-16 02:36:09 +01:00
->each(function ($company) {
$company_logo = $company->present()->logo();
2022-11-11 04:52:50 +01:00
2023-02-16 02:36:09 +01:00
if ($company_logo == 'https://invoicing.co/images/new_logo.png') {
return;
}
2022-11-11 04:52:50 +01:00
2023-02-16 02:36:09 +01:00
$logo = @file_get_contents($company_logo);
2022-11-06 22:20:14 +01:00
2023-02-16 02:36:09 +01:00
if ($logo) {
$path = str_replace("https://objects.invoicing.co/", "", $company->present()->logo());
$path = str_replace("https://v5-at-backup.us-southeast-1.linodeobjects.com/", "", $path);
2022-11-06 22:20:14 +01:00
2023-02-16 02:36:09 +01:00
Storage::disk($this->option('disk'))->put($path, $logo);
}
2022-11-06 22:20:14 +01:00
});
//documents
2022-11-11 04:52:50 +01:00
Document::cursor()
2023-02-16 02:36:09 +01:00
->each(function ($document) {
2022-11-11 04:52:50 +01:00
$doc_bin = $document->getFile();
2023-02-16 02:36:09 +01:00
if ($doc_bin) {
2022-11-11 04:52:50 +01:00
Storage::disk($this->option('disk'))->put($document->url, $doc_bin);
2023-02-16 02:36:09 +01:00
}
2022-11-11 04:52:50 +01:00
});
2022-11-06 22:20:14 +01:00
2023-02-16 02:36:09 +01:00
//backups
2022-11-11 04:52:50 +01:00
Backup::cursor()
2023-02-16 02:36:09 +01:00
->each(function ($backup) {
$backup_bin = Storage::disk('s3')->get($backup->filename);
2022-11-11 04:52:50 +01:00
2023-02-16 02:36:09 +01:00
if ($backup_bin) {
2022-11-11 04:52:50 +01:00
Storage::disk($this->option('disk'))->put($backup->filename, $backup_bin);
2023-02-16 02:36:09 +01:00
}
2022-11-11 04:52:50 +01:00
});
2021-10-20 05:05:46 +02:00
}
}