Skip to content

Instantly share code, notes, and snippets.

@kazumich
Last active March 4, 2022 06:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazumich/0c5ce6ed4e8366628842541ee9101b56 to your computer and use it in GitHub Desktop.
Save kazumich/0c5ce6ed4e8366628842541ee9101b56 to your computer and use it in GitHub Desktop.
<?php
// ----------------------
// a-blog cms バックアップ
// update 2022/03/04
// ----------------------
# 指定すると DOMAIN_YYYYMMDDHHMMSS.zip で
# sql , archives , archives_rev , storage , media , themes , config.system.yaml
# を圧縮してバックアップします。
// ----------------
// 1) 実行方法の設定
// ----------------
# pc : ブラウザからPCに Zipファイルがダウンロードされます。
# server : サーバー上に Zipファイルを保存します。 5) を設定してください。
$zip_download = "pc"; # pc / server
// -------------------
// 2) パスワードチェック
// -------------------
# on : ブラウザ上からパスワードを入力し実行します。(推奨)
# off : cron など画面表示させたくない時に設定ください。
$password_check = "on"; # on / off
// ------------------------
// 3) バックアップテーマの設定
// ------------------------
# テーマファイルをバックアップする際には指定してください。
# "site2020" と設定すると site2020 , lp@site2020 , sp@site2020 がバックアップされます。
# "site2020|blog2020" のように複数指定も可能です。
$useThemes = "blog2020";
// --------------------
// 4) コマンド パスの設定
// --------------------
$mysqldump = "mysqldump";
$mysql = "/usr/bin/mysql";
# MAMP の場合
# $mysqldump = "/Applications/MAMP/Library/bin/mysqldump";
# $mysql ="/Applications/MAMP/Library/bin/mysql";
# 記述の変更が必要なサーバーなどの情報があれば @kazumich に教えてください。
// -----------------------------
// 5) バックアップディレクトリの設定
// -----------------------------
# 1) で server を指定した際に設定してください。
# また、バックアップを保存するディレクトリは public_html , www より上の階層に作成してください。
#$backupDir = "/home/server_name/domain_name/backup";
// -----------------------
// 6) 実行ディレクトリの設定
// -----------------------
# ブラウザからの実行の際には設定の必要はありません。
# cron から実行する際に設定してください。
#$target_dir = "/home/server_name/domain_name/public_html";
// ------------------------------
# これ以下は修正する必要はありません。
$YmdHis = date('YmdHis');
if (!isset($target_dir)) {
$target_dir = realpath('.');
}
require_once($target_dir.'/config.server.php');
$domain = DOMAIN;
$database_name = DB_NAME;
$acount_name = DB_USER;
$acount_password = DB_PASS;
$database_host = DB_HOST;
// 作業用ディレクトリ設定
$tmp_dir = $target_dir."/tmp".$YmdHis;
// ---------------------------
// バックアップディレクトリチェック
// ---------------------------
if ($zip_download == "pc") {
$backupDir = $target_dir;
} else {
$archives_backup = true;
$media_backup = true;
$config_backup = true;
$sql_backup = true;
$storage_backup = true;
$themes_backup = true;
}
if (!is_dir($backupDir)) {
echo "backup dir not found : ".$backupDir;
exit;
}
// バックアップファイル名の設定
$fileName = $domain . "_" . $YmdHis .".zip";
$backup_zipFile = $backupDir ."/". $fileName;
// ------------------------
// パスワード入力画面表示
// ------------------------
if ($password_check == "on") {
$input_pass = filter_input(INPUT_POST, "dbpass");
if ($zip_download == "pc" && $input_pass == DB_PASS) {
$archives_backup = filter_input(INPUT_POST, "archives");
$media_backup = filter_input(INPUT_POST, "media");
$config_backup = filter_input(INPUT_POST, "config");
$sql_backup = filter_input(INPUT_POST, "sql");
$storage_backup = filter_input(INPUT_POST, "storage");
$themes_backup = filter_input(INPUT_POST, "themes");
} else {
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>a-backup</title>
<link rel="stylesheet" href="/themes/system/css/acms-admin.min.css">
<style>
body {
padding : 10px 30px;
background-color : #ddd;
font-family: Courier;
}
</style>
</head>
<body>
<h1>a-backup</h1>
<?php
if ($zip_download == "pc") {
echo "<p>以下の ディレクトリ・ファイル をブラウザからダウンロードします。</p>";
} else {
echo "<p>以下の ディレクトリ・ファイル をサーバーの指定ディレクトリにバックアップします。</p>";
}
?>
<form action="" method="POST" class="acms-admin-form">
<div><input type="checkbox" name="archives" value="true" checked="checked"> archives</div>
<div><input type="checkbox" name="media" value="true" checked="checked"> media</div>
<div><input type="checkbox" name="config" value="true" checked="checked"> private/config.system.yaml</div>
<div><input type="checkbox" name="sql" value="true" checked="checked"> sql/databaseName_yyyymmddhhmmss.sql</div>
<div><input type="checkbox" name="storage" value="true" checked="checked"> storage</div>
<?php
if (isset($useThemes)) {
?><div><input type="checkbox" name="themes" value="true" checked="checked"> themes<?php
echo "<ul>";
if ($handle = opendir($target_dir."/themes")) {
while(false !== ($theme = readdir($handle))) {
if ($theme != "." && $theme != "..") {
if (preg_match("/".$useThemes."/", $theme)) {
echo "<li>".$theme."</li>";
}
}
}
closedir($handle);
}
echo "</ul></div>";
}
?>
</ul>
<input type="password" name="dbpass" id="dbpass" class="acms-admin-form-width-mini">
<input type="submit" class="acms-admin-btn" value="実行">
</form>
<?php
if (!isset($_POST['dbpass'])) {
echo "<p>データベースのパスワードを入力してください。</p></body></html>";
exit;
} elseif ($_POST['dbpass'] != DB_PASS) {
echo "<p class='acms-admin-text-error'>パスワードが間違っています。</p></body></html>";
exit;
} else {
echo "<p>バックアップ処理を実行しています。</p><p>";
echo $backup_zipFile;
echo "</p></body></html>";
}
}
}
// ------------------------
// バックアップファイル コピー
// ------------------------
mkdir ($tmp_dir);
if (isset($archives_backup)) {
dir_shori ("copy", $target_dir."/archives", $tmp_dir."/archives");
dir_shori ("copy", $target_dir."/archives_rev", $tmp_dir."/archives_rev");
}
if (isset($storage_backup)) {
dir_shori ("copy", $target_dir."/storage", $tmp_dir."/storage");
}
if (isset($media_backup)) {
dir_shori ("copy", $target_dir."/media", $tmp_dir."/media");
}
// ----------------
// SQL バックアップ
// ----------------
if (isset($sql_backup)) {
mkdir ($tmp_dir."/sql");
$sql_fileName = $database_name ."_". $YmdHis . ".sql";
$sql_filePath = $tmp_dir ."/sql/". $sql_fileName;
$command_dump = $mysqldump. ' --single-transaction --default-character-set=binary ' . $database_name . ' --host=' . $database_host . ' --user=' . $acount_name . ' --password=' . $acount_password . ' > ' . $sql_filePath;
system($command_dump);
}
// -----------------
// テーマ バックアップ
// -----------------
if (isset($themes_backup)) {
if (isset($useThemes)) {
mkdir ($tmp_dir."/themes");
if ($handle = opendir($target_dir."/themes")) {
while(false !== ($theme = readdir($handle))) {
if ($theme != "." && $theme != "..") {
if (preg_match("/".$useThemes."/", $theme)) {
dir_shori ("copy", $target_dir."/themes/".$theme, $tmp_dir."/themes/".$theme);
}
}
}
closedir($handle);
}
}
}
// ----------------
// 設定 バックアップ
// ----------------
if (isset($config_backup)) {
mkdir ($tmp_dir."/private");
copy($target_dir."/private/config.system.yaml", $tmp_dir."/private/config.system.yaml");
}
// --------
// ZIP処理
// --------
# $fileName = $domain . "_" . $YmdHis .".zip";
# $backup_zipFile = $backupDir ."/". $fileName;
$command_zip = "cd ". $tmp_dir .";".
"zip -r -q ". $backup_zipFile ." .";
system($command_zip);
// ----------------
// zip ダウンロード
// ----------------
if ($zip_download == "pc") {
header('Pragma: public');
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$fileName);
readfile($backup_zipFile);
unlink($backup_zipFile);
}
// ----------------
// tmp Dir delete
// ----------------
dir_shori("delete", $tmp_dir);
exit;
// --------------------------------------------------
// ディレクトリを操作 function ( move / copy / delete )
// --------------------------------------------------
function dir_shori ($shori, $nowDir , $newDir="") {
if ($shori != "delete") {
if (!is_dir($newDir)) {
mkdir($newDir);
}
}
if (is_dir($nowDir)) {
if ($handle = opendir($nowDir)) {
while (($file = readdir($handle)) !== false) {
if ($file != "." && $file != "..") {
if ($shori == "copy") {
if (is_dir($nowDir."/".$file)) {
dir_shori("copy", $nowDir."/".$file, $newDir."/".$file);
} else {
copy($nowDir."/".$file, $newDir."/".$file);
}
} elseif ($shori == "move") {
rename($nowDir."/".$file, $newDir."/".$file);
} elseif ($shori == "delete") {
if (filetype($nowDir."/".$file) == "dir") {
dir_shori("delete", $nowDir."/".$file, "");
} else {
unlink($nowDir."/".$file);
}
}
}
}
closedir($handle);
}
}
if ($shori == "move" || $shori == "delete") {
rmdir($nowDir);
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment