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

128 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
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. 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;
2021-10-20 05:05:46 +02:00
use App\Models\Design;
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;
use stdClass;
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()
2022-11-06 22:20:14 +01:00
->each(function ($company){
2022-11-11 04:52:50 +01:00
$company_logo = $company->present()->logo();
if($company_logo == 'https://invoicing.co/images/new_logo.png')
return;
$logo = @file_get_contents($company_logo);
2022-11-06 22:20:14 +01:00
if($logo){
2022-11-11 04:52:50 +01:00
$path = str_replace("https://objects.invoicing.co/", "", $company->present()->logo());
2022-11-06 22:20:14 +01:00
$path = str_replace("https://v5-at-backup.us-southeast-1.linodeobjects.com/", "", $path);
Storage::disk($this->option('disk'))->put($path, $logo);
}
});
//documents
2022-11-11 04:52:50 +01:00
Document::cursor()
->each(function ($document){
$doc_bin = $document->getFile();
if($doc_bin)
Storage::disk($this->option('disk'))->put($document->url, $doc_bin);
});
2022-11-06 22:20:14 +01:00
//backups
2022-11-11 04:52:50 +01:00
Backup::cursor()
->each(function ($backup){
$backup_bin = Storage::disk('s3')->get($backup->filename);
if($backup_bin)
Storage::disk($this->option('disk'))->put($backup->filename, $backup_bin);
});
2022-11-06 22:20:14 +01:00
2021-10-20 05:05:46 +02:00
}
}