89 lines
2.7 KiB
PHP
89 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use Log;
|
|
use File;
|
|
|
|
class DBBackup extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'db:backup';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Backup DB';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->backupLocal();
|
|
}
|
|
|
|
private function backupLocal()
|
|
{
|
|
Artisan::call('down');
|
|
Log::warning(['dbbackup-start' => now()]);
|
|
$path = storage_path() . "/app/backup/" . now()->year . "/";
|
|
File::isDirectory($path) or File::makeDirectory($path, 0777, true, true);
|
|
|
|
$filename = "backup_" . now()->format('Y-m-d_H:i:m') . ".gz";
|
|
$command = "mysqldump --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . " --complete-insert " . " | gzip > " . $path . $filename;
|
|
$returnVar = NULL;
|
|
$output = NULL;
|
|
|
|
exec($command, $output, $returnVar);
|
|
sleep(10);
|
|
Log::warning(['dbbackup-stop' => now()]);
|
|
Artisan::call('up');
|
|
}
|
|
|
|
private function backupRemote()
|
|
{
|
|
Artisan::call('down');
|
|
Log::warning(['dbbackup-start' => now()->format('Y-m-d H:i:m')]);
|
|
|
|
$filename = "ags_billing_" . now()->format('Y-m-d_H:i') . ".gz";
|
|
$filename_withpath = storage_path() . "/app/public/" . $filename;
|
|
$command = "mysqldump --user=" . \config('backup.db_username') ." --password=" . \config('backup.db_password') . " --host=" . \config('backup.db_host') . " " . \config('backup.db_database') . " --complete-insert " . " | gzip > " . $filename_withpath;
|
|
|
|
$returnVar = NULL;
|
|
$output = NULL;
|
|
|
|
exec($command, $output, $returnVar);
|
|
sleep(3);
|
|
|
|
Log::warning(['dbbackup-stop' => now()->format('Y-m-d H:i:m')]);
|
|
Artisan::call('up');
|
|
|
|
$remote_serv = 'user@ip-address';
|
|
$remote_path = ':/var/sql_data/ags_billing/' . now()->year . "/" . now()->month . "/";
|
|
$remote_pass = 'remote_pass';
|
|
|
|
Log::warning(['start: db backup cp to remote server' => now()->format('Y-m-d H:i:m')]);
|
|
|
|
$command = "sshpass -p " . $remote_pass . " scp " . $filename_withpath . " " . $remote_serv . $remote_path;
|
|
// apt-get install sshpass;
|
|
|
|
exec($command, $output, $returnVar);
|
|
|
|
Log::warning(['end: db backup cp to remote server' => now()->format('Y-m-d H:i:m')]);
|
|
|
|
// remote file
|
|
if($returnVar == 0)
|
|
File::delete($filename_withpath);
|
|
}
|
|
} |