Skip to content

Commit

Permalink
Merge branch 'dev' into issue225-wilayah
Browse files Browse the repository at this point in the history
  • Loading branch information
vickyrolanda committed Nov 15, 2023
2 parents 76ad816 + 2963d10 commit a3d7f33
Show file tree
Hide file tree
Showing 20 changed files with 923 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ yarn-error.log
/.idea
/.vscode
composer.lock
/backup
80 changes: 80 additions & 0 deletions app/Console/Commands/BackupDatabaseStorage.php
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);
}
}
}
62 changes: 62 additions & 0 deletions app/Console/Commands/BackupGoogleDrive.php
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);
}
}
}
4 changes: 3 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('tracksid:optimasi-desa')->weekly();
$schedule->command('tracksid:optimasi-desa')->weekly(); // setiap minggu at 00:00
$schedule->command('tracksid:backup-database-storage')->weekly()->sundays()->timezone('Asia/Jakarta')->at('01:00'); // setiap minggu at 01:00
$schedule->command('tracksid:backup-google-drive')->weekly()->sundays()->timezone('Asia/Jakarta')->at('03:00'); // setiap minggu at 03:00
}

/**
Expand Down
57 changes: 57 additions & 0 deletions app/Helpers/helper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Models\PengaturanAplikasi;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

Expand Down Expand Up @@ -222,4 +223,60 @@ function lastrelease_opendk()

return $version;
}

if (! function_exists('folder_backup')) {
function folder_backup()
{
$folder_backup = 'backup';

if (! file_exists($folder_backup)) {
exec('mkdir '.$folder_backup);
}

return $folder_backup;
}
}

if (! function_exists('folderBackupDatabase')) {
function folderBackupDatabase()
{
$folder_database = folder_backup().DIRECTORY_SEPARATOR.'database';

if (! file_exists($folder_database)) {
exec('mkdir '.$folder_database);
}

return $folder_database;
}
}

/** aktifkan backup menggunakan rclone syncs to cloud storage */
if (! function_exists('rclone_syncs_storage')) {
function rclone_syncs_storage()
{
return file_exists('/usr/bin/rclone') ? true : false;
}
}

/** jumlah directory maksimal backup ke gdrive */
if (! function_exists('max_backup_dir')) {
function max_backup_dir()
{
return PengaturanAplikasi::get_pengaturan()['maksimal_backup'];
}
}

/** pengecekan tanggal akhir backup database dan folder desa */
if (! function_exists('cek_tgl_akhir_backup')) {
function cek_tgl_akhir_backup($pelanggans)
{
// if($pelanggans->first()){
// $tglbackup = $pelanggans->where('status_langganan_opensid', 1)->first()->tgl_akhir_backup;
// $hariini = date('Y-m-d');
// $selisih = (strtotime($hariini) - strtotime($tglbackup)) / 60 / 60 / 24;

// return $selisih;
// }
}
}
}
16 changes: 16 additions & 0 deletions app/Http/Controllers/Helpers/CommandController.php
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);
}
}
85 changes: 85 additions & 0 deletions app/Http/Controllers/Helpers/RemoteController.php
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.');
}
}
}
11 changes: 11 additions & 0 deletions app/Models/PengaturanAplikasi.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,15 @@ class PengaturanAplikasi extends Model

/** {@inheritdoc} */
public $incrementing = true;

public static function get_pengaturan()
{
$aplikasi = self::get();

$data['cloud_storage'] = $aplikasi->where('key', 'cloud_storage')->first()->value ?? '';
$data['waktu_backup'] = $aplikasi->where('key', 'waktu_backup')->first()->value ?? '';
$data['maksimal_backup'] = $aplikasi->where('key', 'maksimal_backup')->first()->value ?? '';

return $data;
}
}
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"laravel/tinker": "^2.5",
"laravel/ui": "^3.4",
"maatwebsite/excel": "^3.1",
"spatie/db-dumper": "^2.21",
"spatie/laravel-backup": "^6.16",
"yajra/laravel-datatables-oracle": "^9.18"
},
"require-dev": {
Expand Down
Loading

0 comments on commit a3d7f33

Please sign in to comment.