Skip to content

Commit

Permalink
tambahkan log
Browse files Browse the repository at this point in the history
  • Loading branch information
apidong committed Jul 3, 2024
1 parent 4f5ddf6 commit 9876d13
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 29 deletions.
17 changes: 11 additions & 6 deletions app/Console/Commands/BackupDatabaseStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Console\Commands;

use App\Http\Controllers\Helpers\CommandController;
use App\Models\LogBackup;
use Exception;
use Illuminate\Console\Command;
use Spatie\DbDumper\Databases\MySql;
Expand Down Expand Up @@ -64,25 +65,29 @@ private function backupDatabase()
->setUserName(env('DB_USERNAME'))
->setPassword(env('DB_PASSWORD'));

$backup->dumpToFile($this->folder_database.'/'.$this->database_name);
$backup->dumpToFile($this->folder_database . '/' . $this->database_name);
} catch (Exception $ex) {
$this->command->notifMessage('Peringatan : gagal backup ke database Pantau, silakan cek koneksi !!!');
LogBackup::create([
'status' => 0,
'log' =>'pantau:backup-database-storage :' . $ex->getMessage()
]);

return exec('rm '.$this->folder_database.'/'.$this->database_name);
return exec('rm ' . $this->folder_database . '/' . $this->database_name);
}
}

private function backupStorage()
{
$folderdesa_from = 'storage'.DIRECTORY_SEPARATOR.'app';
$folderdesa_to = folder_backup().DIRECTORY_SEPARATOR.'storage';
$folderdesa_from = 'storage' . DIRECTORY_SEPARATOR . 'app';
$folderdesa_to = folder_backup() . DIRECTORY_SEPARATOR . 'storage';

if (! file_exists($folderdesa_to)) {
if (!file_exists($folderdesa_to)) {
mkdir($folderdesa_to, 0755, true);
}

if (file_exists($folderdesa_from)) {
exec('cp -R '.$folderdesa_from.' '.$folderdesa_to);
exec('cp -R ' . $folderdesa_from . ' ' . $folderdesa_to);
}
}
}
59 changes: 36 additions & 23 deletions app/Http/Controllers/Helpers/RemoteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace App\Http\Controllers\Helpers;

use App\Http\Controllers\Controller;
use Exception;
use App\Models\LogBackup;
use App\Models\PengaturanAplikasi;
use App\Http\Controllers\Controller;

class RemoteController extends Controller
{
Expand All @@ -25,21 +27,32 @@ public function __construct()
/** proses remote rclone syncs to cloud storage */
public function backupToCloudStorage($storage_type, $remote_name, $akhir_backup, $root)
{
if (file_exists(folder_backup()) && rclone_syncs_storage() == true && cek_tgl_akhir_backup($akhir_backup) == 0) {
// 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);
try {
if (file_exists(folder_backup()) && rclone_syncs_storage() == true && cek_tgl_akhir_backup($akhir_backup) == 0) {
// nama app_url & tanggal backup
$directory_backup = $root . $this->host . '/backupserverdukungan/' . 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);
LogBackup::create([
'status' => 0,
'log' => 'Gagal backup menggunakan tipe ' . $storage_type
]);
}
} catch (Exception $e) {
LogBackup::create([
'status' => 0,
'log' => 'Gagal backup menggunakan tipe ' . $storage_type . '. Pesan error: ' . $e->getMessage()
]);
}
}

Expand All @@ -52,35 +65,35 @@ public function removeBackupCloudStorage($remote_name, $akhir_backup, $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);
$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, $akhir_backup, $root)
{
// folder yang paling lama
$old_dir = exec('sudo rclone lsf '.$remote_name.':'.$root.$this->host.'/ | sort -r | tail -n +'.$directory);
$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;
$directory_backup = $root . $this->host . '/backupserverdukungan/' . $old_dir;

if (rclone_syncs_storage() == true && cek_tgl_akhir_backup($akhir_backup) == 0) {
// hapus folder backup terlama
exec('sudo rclone purge '.$remote_name.':/'.$directory_backup);
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);
$this->command->notifMessage('Berhasil telah menghapus folder ' . $old_dir);
} else {
// notif gagal
$this->command->notifMessage('Gagal hapus folder '.$old_dir.' tidak ada.');
$this->command->notifMessage('Gagal hapus folder ' . $old_dir . ' tidak ada.');
}
}

Expand Down
21 changes: 21 additions & 0 deletions app/Models/LogBackup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class LogBackup extends Model
{
use HasFactory;


protected $table = 'log_backup';


protected $fillable = [
'status',
'log',
];

}
32 changes: 32 additions & 0 deletions database/migrations/2024_07_03_221458_create_log_backup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('log_backup', function (Blueprint $table) {
$table->int('status')->nullable();
$table->text('log', 255)->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('log_backup');
}
};

0 comments on commit 9876d13

Please sign in to comment.