diff --git a/backend/app/Console/Kernel.php b/backend/app/Console/Kernel.php index 8f47767f..0f923f19 100644 --- a/backend/app/Console/Kernel.php +++ b/backend/app/Console/Kernel.php @@ -39,6 +39,41 @@ class Kernel extends ConsoleKernel } } })->everyMinute(); + $schedule->call(function () { + $dbbckp = '/work/storage/backup/mysql/'; + $flbckp = '/work/storage/backup/file/'; + $week = 604800; // 1週間 + $fname = date('Ymd').date('H'); // 日付+時 + + // DBのバックアップ + exec('/usr/bin/mysqldump --no-tablespaces --user='.config('database.connections.mysql.username').' --host='.config('database.connections.mysql.host').' --password='.config('database.connections.mysql.password').' --databases '.config('database.connections.mysql.database').' > '.$dbbckp.$fname.'_mysql.sql'); + + // GZIPでDBを圧縮する(ストレージ為) + exec('/bin/gzip '.$dbbckp.$fname.'_mysql.sql'); + + // ファイルのバックアップ + exec('/bin/tar zcfv '.$flbckp.$fname.'.tar.gz /work/storage/app/private/*'); + + // 全てのSQLファイルを読み込むと、古いやつを削除する。 + foreach (scandir($dbbckp) as $s) { + if ($s == '.' || $s == '..') continue; + $t = str_replace('_mysql.sql.gz', '', $s); // extの削除 + $t = preg_replace('/[\w\d]{2}$/', '', $t); // 時間の削除 + if (strtotime($t) < time()-604800) { + exec('/bin/rm -rf '.$dbbckp.$s); + } + } + + // 全てのTARファイルを読み込むと、古いやつを削除する。 + foreach (scandir($flbckp) as $s) { + if ($s == '.' || $s == '..') continue; + $t = str_replace('.tar.gz', '', $s); // extの削除 + $t = preg_replace('/[\w\d]{2}$/', '', $t); // 時間の削除 + if (strtotime($t) < time()-604800) { + exec('/bin/rm -rf '.$flbckp.$s); + } + } + })->twiceDaily(0, 12); // $schedule->command('inspire')->hourly(); } diff --git a/backend/storage/backup/file/.index b/backend/storage/backup/file/.index new file mode 100755 index 00000000..e69de29b diff --git a/backend/storage/backup/mysql/.index b/backend/storage/backup/mysql/.index new file mode 100755 index 00000000..e69de29b diff --git a/backup/.index b/backup/.index new file mode 100644 index 00000000..e69de29b diff --git a/backup/file/.index b/backup/file/.index new file mode 100644 index 00000000..e69de29b diff --git a/backup/mysql/.index b/backup/mysql/.index new file mode 100644 index 00000000..e69de29b diff --git a/crontab.txt b/crontab.txt new file mode 100644 index 00000000..adc8f8bf --- /dev/null +++ b/crontab.txt @@ -0,0 +1,7 @@ +【docker内】 +まずは下記のコマンドで新しいフォルダを創作して下さい。 +mkdir -p storage/backup/{file,mysql} + +【docker外】 +「crontab -e」を実行すると、下記の行列を追加して下さい。 +0 0,12 * * * /usr/bin/docker cp 3463d3554a6f:/work/storage/backup/mysql/ /var/www/html/kikikanri/backup