Skip to content

Instantly share code, notes, and snippets.

@atrakeur
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atrakeur/9765785 to your computer and use it in GitHub Desktop.
Save atrakeur/9765785 to your computer and use it in GitHub Desktop.
A simple php utility to make a backup actual version and extract a new version of a website from an uploaded zip.
<?php
/*
* Configuration
*/
// Le nom de ce fichier
$filename = "updater.php";
// Le repertoire ou extraire la nouvelle version
$extractdir= "./";
// Le repertoire ou sauvegarder l'ancienne version
$backupdir = "backups";
// Le zip a extraire
$sourcezip = "wwwnew.zip";
// Le script php ou rediriger l'utilisateur a la fin de la migration
// Dans mon cas, ce script sert à migrer la base de données
$installpath = "/install";
/*
* Execution
*/
//Check environement
$env_errors = array();
//Check backup destination directory
if (!file_exists($backupdir)) {
mkdir($backupdir);
}
if (!is_writable($backupdir)) {
$env_errors[] = $backupdir." isn't writable";
}
//Check source zip
if (!(file_exists($sourcezip) && is_readable($sourcezip))) {
$env_errors[] = $sourcezip." isn't readable";
}
//Check destination directory
if (!file_exists($extractdir)) {
mkdir($extractdir);
}
if (!is_writable($extractdir)) {
$env_errors[] = $extractdir." isn't writable";
}
$helpmessage = "Cliquez sur backup pour effectuer la sauvegarde. L'extraction sera disponible une fois cette sauvegarde effectuée";
if (!empty($env_errors)) {
$helpmessage = "Corrigez les erreurs d'environnement pour continuer";
}
$nextState = 'backup';
$nextSave = '';
$nextStateText = 'Start';
$errors = array();
//Get the current state
if (empty($env_errors) && isset($_GET['state'])) {
switch ($_GET['state']) {
case 'backup': {
$nextState = 'backup';
$nextStateText = 'Continue backup';
$backupsubdir = date("Ymdims").'/';
mkdir($backupdir.'/'.$backupsubdir);
$files = scandir($extractdir);
foreach($files as $file) {
if ($file == "." || $file == ".." || $file == $backupdir || $file == $sourcezip || $file == 'updater.php') {
continue;
}
echo 'rename '.$file.' to '.$backupdir.'/'.$backupsubdir.$file."\n";
rename($file, $backupdir.'/'.$backupsubdir.$file);
}
$nextState = 'extract';
$nextStateText = 'Extract';
$helpmessage = "Cliquez sur extract pour commencer l'extraction";
} break;
case 'extract': {
$zip = new ZipArchive;
if ($zip->open($sourcezip) === TRUE) {
$zip->extractTo($extractdir);
$zip->close();
$nextState = 'done';
$nextStateText = 'Done!';
} else {
$errors[] = "Error zip!";
}
$helpmessage = "Cliquez sur Done pour aller sur votre script d'installation";
} break;
case 'done' : {
header('Location: '.$installpath);
exit();
}
default: {
$errors[] = "Unknow state";
} break;
}
}
?>
<html>
<head>
<title></title>
</head>
<body>
<h1>Updater</h1>
<?php
if (!empty($env_errors)){
foreach($env_errors AS $error) {
echo '<p>'.$error.'</p>';
}
}
if (!empty($errors)){
foreach($errors AS $error) {
echo '<p>'.$error.'</p>';
}
}
?>
<?php echo $helpmessage; ?>
<?php echo '<a href="updater.php?state='.$nextState.'"">'.$nextStateText.'</a>'; ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment