Skip to content

Instantly share code, notes, and snippets.

@kazumich
Last active August 10, 2022 11:07
Show Gist options
  • Save kazumich/66d00482c486b80e7ad3407c5e41df59 to your computer and use it in GitHub Desktop.
Save kazumich/66d00482c486b80e7ad3407c5e41df59 to your computer and use it in GitHub Desktop.
<?php
ini_set('max_execution_time', 0);
// ----------------------
// a-blog cms サイトコピー (XSERVER用)
// update 2022/07/18
// ----------------------
// --------------------
// 移行前環境設定
// --------------------
// このファイルと同じディレクトリにある config.server.php の
// データベース設定を行います。
$database_host_before = "localhost";
$database_name_before = "database_name";
$acount_name_before = "database_user";
$acount_password_before = "database_pass";
// --------------------
// 移行後環境設定
// --------------------
// a-blog cms が設置済みの移行後の設定を行います。
// 移行後パス設定
$system_dir_after = "/home/appleple/appleple.xsrv.jp/public_html";
// 移行後のバックアップ
$backup_dir_name = "_backup";
// コピーしないファイル
$untouched = array(
'config.server.php',
'license.php',
'.htaccess',
'x-copy.php'
);
// --------------------
// コマンド パスの設定
// --------------------
$mysqldump = "mysqldump";
$mysql = "/usr/bin/mysql";
// ------------------------------
# これ以下は修正する必要はありません。
$system_domain_before = $_SERVER['HTTP_HOST'];
$config_after = $system_dir_after.'/config.server.php';
if (is_file($config_after)) {
require_once($config_after);
$database_name_after = DB_NAME;
$acount_name_after = DB_USER;
$acount_password_after = DB_PASS;
$database_host_after = DB_HOST;
$database_prefix_after = DB_PREFIX;
// 移行後のドメイン設定
$system_domain_after = DOMAIN;
$install = "ok";
} else {
$install = "ng";
$database_name_after = "";
$system_domain_after = "";
}
$YmdHis = date('Ymd_His');
$target_dir = realpath('.');
$system_dir_after_backup = $system_dir_after . $backup_dir_name;
$msg = array();
// ------------------------
// パスワード入力画面表示
// ------------------------
$input_pass = filter_input(INPUT_POST, "dbpass");
if (!isset($input_pass)) {
array_push($msg,"移行先のデータベースのパスワードを入力してください。");
} elseif ($input_pass != DB_PASS) {
array_push($msg,"データベースのパスワードが間違っています。");
} else {
// ------------------------
// データベース エクスポート 移行元
// ------------------------
$sql_fileName = $YmdHis . '_databasedump.sql';
$sql_filePath = $target_dir .'/'. $sql_fileName;
$command_dump = $mysqldump. ' --single-transaction --default-character-set=binary ' . $database_name_before . ' --host=' . $database_host_before . ' --user=' . $acount_name_before . ' --password=' . $acount_password_before . ' > ' . $sql_filePath;
exec($command_dump);
// ------------------------
// データベース エクスポート 移行後 (バックアップ)
// ------------------------
$sql_fileName_after = $YmdHis . '_backup.sql';
$sql_filePath_after = $target_dir .'/' . $sql_fileName_after;
$command_dump = $mysqldump. ' --single-transaction --default-character-set=binary ' . $database_name_after . ' --host=' . $database_host_after . ' --user=' . $acount_name_after . ' --password=' . $acount_password_after . ' > ' . $sql_filePath_after;
exec($command_dump);
// ------------------------
// データベース インポート
// ------------------------
$command_restore = "$mysql -u $acount_name_after -p$acount_password_after -h $database_host_after $database_name_after < $sql_filePath";
exec($command_restore);
// SQL 削除
unlink($sql_filePath);
// ---------------------------
// データベース ドメイン名書き換え
// ---------------------------
try {
$dbh = new PDO('mysql:host='.$database_host_after.';dbname='.$database_name_after.'', $acount_name_after, $acount_password_after);
$sql = sprintf("UPDATE %sblog SET blog_domain = '%s'",$database_prefix_after,$system_domain_after);
$dbh->query($sql);
$sth = null;
$dbh = null;
} catch (PDOException $e) {
print "エラー!: " . $e->getMessage();
die();
}
// ------------------------
// ドキュメントルートをコピー
// ------------------------
// 上書きされるディレクトリをバックアップ用にディレクトリ名を変更
$command ='mv '. $system_dir_after . ' ' . $system_dir_after_backup;
exec($command);
// 上書きされたデータベースのバックアップを保存
$command ='mv '. $sql_filePath_after . ' ' . $system_dir_after_backup .'/'. $sql_fileName_after;
exec($command);
// ディレクトリを作成
mkdir ($system_dir_after);
// ディレクトリをコピー
$command = 'cp -r '. $target_dir .'/. '. $system_dir_after;
exec($command);
// ------------------------
// ドキュメントルートをコピー
// ------------------------
foreach ( $untouched as $data ) {
$backup_untouched = $system_dir_after_backup."/".$data;
if (is_file($backup_untouched)) {
copy ($backup_untouched, $system_dir_after."/".$data);
} elseif (is_dir($backup_untouched)) {
dir_shori ("copy", $backup_untouched, $system_dir_after."/".$data);
}
}
array_push($msg,"コピー作業が終了しました。");
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>x-copy</title>
<link rel="stylesheet" href="/themes/system/css/acms-admin.min.css">
<style>
body {
padding : 10px 30px;
background-color : #ddd;
font-family: Courier;
}
.error {
color: #aa0000;
font-weight: bold;
}
</style>
</head>
<body>
<h1>x-copy</h1>
<p>XSERVERで公開ドメインと初期サブドメインの環境間でドキュメントルート内のディレクトリ・ファイル、データベースをコピーします。</p>
<p>実行後には公開サーバーの public_html_backup に実行前のファイルはコピーされ、そのディレクトリ内には データベースもバックアップされています。</p>
<h2>移行元</h2>
<ul>
<li>Domain: <?php echo $system_domain_before; ?></li>
<li>Path: <?php echo $target_dir; ?></li>
<li>DB: <?php echo $database_name_before; ?></li>
</ul>
<h2>移行先</h2>
<ul>
<?php
if ($install == "ng") {
echo "<li class=\"error\">移行先のインスールが行われておりません。</li>";
} else {
?>
<li>Domain: <?php echo $system_domain_after; ?></li>
<li>Path: <?php echo $system_dir_after; ?></li>
<li>DB: <?php echo $database_name_after; ?></li>
</ul>
<h2>対象外ファイル・ディレクトリ</h2>
<ul>
<?php
foreach ( $untouched as $data ) {
$backup_untouched = $system_dir_after."/".$data;
if (is_file($backup_untouched)) {
echo "<li>file:".$data."</li>";
} elseif (is_dir($backup_untouched)) {
echo "<li>dir:".$data."</li>";
} else {
echo "<li>404:".$data."</li>";
}
}
?>
</ul>
<form action="" method="POST" class="acms-admin-form">
<input type="password" name="dbpass" id="dbpass" value="<?php echo $input_pass; ?>" class="acms-admin-form-width-mini">
<input type="submit" class="acms-admin-btn" value="実行">
</form>
<?php
foreach ( $msg as $text ) {
echo "<p>".$text."</p";
}
}
?>
</body></html>
<?php
// --------------------------------------------------
// ディレクトリを操作 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