-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into issue225-wilayah
- Loading branch information
Showing
20 changed files
with
923 additions
and
4 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 |
---|---|---|
|
@@ -16,3 +16,4 @@ yarn-error.log | |
/.idea | ||
/.vscode | ||
composer.lock | ||
/backup |
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Http\Controllers\Helpers\CommandController; | ||
use Exception; | ||
use Illuminate\Console\Command; | ||
use Spatie\DbDumper\Databases\MySql; | ||
|
||
class BackupDatabaseStorage extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'tracksid:backup-database-storage'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Melakukan Backup Database dan folder storage (dapat dilakukan melaluo cronjob)'; | ||
|
||
private $command; | ||
|
||
private $folder_database; | ||
|
||
private $database_name = 'db_pantau.sql'; | ||
|
||
/** | ||
* Create a new command instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
$this->command = new CommandController(); | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
{ | ||
$this->folder_database = folderBackupDatabase(); | ||
$this->backupDatabase(); | ||
$this->backupStorage(); | ||
} | ||
|
||
private function backupDatabase() | ||
{ | ||
try { | ||
$backup = MySql::create() | ||
->setDbName(env('DB_DATABASE')) | ||
->setUserName(env('DB_USERNAME')) | ||
->setPassword(env('DB_PASSWORD')); | ||
|
||
$backup->dumpToFile($this->folder_database.'/'.$this->database_name); | ||
} catch (Exception $ex) { | ||
$this->command->notifMessage('Peringatan : gagal backup ke database Pantau, silakan cek koneksi !!!'); | ||
|
||
return exec('rm '.$this->folder_database.'/'.$this->database_name); | ||
} | ||
} | ||
|
||
private function backupStorage() | ||
{ | ||
$folderdesa_from = 'storage'; | ||
$folderdesa_to = folder_backup().DIRECTORY_SEPARATOR.'storage'; | ||
|
||
if (file_exists($folderdesa_from)) { | ||
exec('cp -R '.$folderdesa_from.' '.$folderdesa_to); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Http\Controllers\Helpers\RemoteController; | ||
use App\Models\PengaturanAplikasi; | ||
use Illuminate\Console\Command; | ||
|
||
class BackupGoogleDrive extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'tracksid:backup-google-drive'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Melakukan backup database dan folder storage ke google drive'; | ||
|
||
private $remote; | ||
|
||
/** | ||
* Create a new command instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
$this->remote = new RemoteController(); | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
{ | ||
// tipe pencadangan, misalkan: drive, sftp, dll | ||
$storage_type = 'drive'; | ||
|
||
// nama remote yang dibuat melalui rclone config | ||
$remote_name = 'backup-drive'; | ||
|
||
// data pelanggan | ||
$pelanggans = ''; //Pelanggan::get(); dicek kembali sesuai issue tanggal terakhir backup | ||
|
||
if (PengaturanAplikasi::get_pengaturan()['cloud_storage'] == $storage_type) { | ||
// hapus data yang paling lama dengan batas maksimal yang ditentukan | ||
$this->remote->removeBackupCloudStorage($remote_name, $pelanggans, null); | ||
|
||
// proses backup | ||
$this->remote->backupToCloudStorage($storage_type, $remote_name, null); | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Helpers; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class CommandController extends Controller | ||
{ | ||
/** perintah untuk notifikasi var_damp dan log laravel*/ | ||
public function notifMessage($notif) | ||
{ | ||
var_dump($notif); | ||
Log::channel('single')->info($notif); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Helpers; | ||
|
||
use App\Http\Controllers\Controller; | ||
|
||
class RemoteController extends Controller | ||
{ | ||
private $command; | ||
|
||
private $host; | ||
|
||
/** | ||
* Create a new component instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->command = new CommandController(); | ||
$this->host = request()->getHttpHost(); | ||
} | ||
|
||
/** proses remote rclone syncs to cloud storage */ | ||
public function backupToCloudStorage($storage_type, $remote_name, $root) | ||
{ | ||
if (file_exists(folder_backup()) && rclone_syncs_storage() == true) { | ||
// nama app_url & tanggal backup | ||
$directory_backup = $root.$this->host.'/'.date('Y-m-d'); | ||
|
||
// membuat folder di storage_type | ||
exec('rclone mkdir '.$remote_name.':/'.$directory_backup); | ||
|
||
// proses backup ke storage_type | ||
exec('rclone -v sync '.folder_backup().' '.$remote_name.':'.$directory_backup); | ||
|
||
// notif berhasil | ||
$this->command->notifMessage('Berhasil backup menggunakan tipe '.$storage_type.' tanggal '.date('Y-m-d')); | ||
} else { | ||
// notif gagal | ||
$this->command->notifMessage('Gagal backup menggunakan tipe '.$storage_type); | ||
} | ||
} | ||
|
||
public function removeBackupCloudStorage($remote_name, $pelanggans, $root) | ||
{ | ||
if ($this->countDirectoryCloudStorage($remote_name, $root) == max_backup_dir()) { | ||
$this->removeBackup(max_backup_dir(), $remote_name, $pelanggans, $root); | ||
} | ||
} | ||
|
||
public function countDirectoryCloudStorage($remote_name, $root) | ||
{ | ||
$count_dir = exec('rclone lsd '.$remote_name.':'.$root.$this->host.'/ | wc -l'); | ||
$this->command->notifMessage('Jumlah Folder '.$count_dir); | ||
|
||
return $count_dir; | ||
} | ||
|
||
public function removeBackup($directory, $remote_name, $pelanggans, $root) | ||
{ | ||
// folder yang paling lama | ||
$old_dir = exec('sudo rclone lsf '.$remote_name.':'.$root.$this->host.'/ | sort -r | tail -n +'.$directory); | ||
$old_dir = rtrim($old_dir, '/'); | ||
|
||
// nama app_url & tanggal backup | ||
$directory_backup = $root.$this->host.'/'.$old_dir; | ||
|
||
if (rclone_syncs_storage() == true && cek_tgl_akhir_backup($pelanggans) == 0) { | ||
// hapus folder backup terlama | ||
exec('sudo rclone purge '.$remote_name.':/'.$directory_backup); | ||
/** | ||
* rclone rmdir = hapus directory | ||
* rclone rmdirs = hapus directory dengan beberapa directory di dalamnya | ||
* rclone purge = hapus semua directory dan file yang ada di directory yang akan dihapus | ||
* */ | ||
|
||
// notif berhasil | ||
$this->command->notifMessage('Berhasil telah menghapus folder '.$old_dir); | ||
} else { | ||
// notif gagal | ||
$this->command->notifMessage('Gagal hapus folder '.$old_dir.' tidak ada.'); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.