Skip to content

Instantly share code, notes, and snippets.

@notFloran
Created May 15, 2013 07:18
Show Gist options
  • Save notFloran/5582166 to your computer and use it in GitHub Desktop.
Save notFloran/5582166 to your computer and use it in GitHub Desktop.
PHP Script to automate the update of drupal core on ovh shared hosting without ssh access
<?php
$hostBdd = "localhost";
$nameBdd = "";
$userBdd = "";
$passwordBdd = "";
$urlDrupal = "http://ftp.drupal.org/files/projects/drupal-7.21.zip";
function backupFiles() {
$dateBackup = new DateTime();
$fileName = sprintf('backup_%s', $dateBackup->format('Ymdhis'));
ob_start();
passthru(sprintf('zip -r %s.zip .',$fileName));
$resultatCommande=ob_get_contents();
ob_end_clean();
return array(
'nomFichier' => $fileName,
'resultatCommande' => $resultatCommande
);
}
function backupBdd($hostBdd, $nameBdd, $userBdd, $passwordBdd) {
$dateBackup = new DateTime();
$fileName = sprintf('backup_%s', $dateBackup->format('Ymdhis'));
ob_start();
passthru(sprintf("mysqldump --host=%s --user=%s --password=%s %s > %s.sql",$hostBdd, $userBdd, $passwordBdd, $nameBdd, $fileName));
passthru(sprintf("gzip %s.sql", $fileName));
$resultatCommande=ob_get_contents();
ob_end_clean();
return array(
'nomFichier' => $fileName . '.sql.gz',
'resultatCommande' => $resultatCommande
);
}
function getDrupal($urlDrupal) {
$nomFichier = basename($urlDrupal);
$nomDossier = str_replace('.zip', '', $nomFichier);
if(file_exists($nomDossier) || file_exists($nomFichier)) {
die("le dossier de maj ou l'archive existe déjà");
}
ob_start();
system(sprintf("wget %s", $urlDrupal));
system(sprintf("unzip %s", $nomFichier));
system(sprintf("rm %s", $nomFichier));
$resultatCommande=ob_get_contents();
ob_end_clean();
ob_start();
system(sprintf("diff %s/.htaccess .htaccess",$nomDossier));
$resultatDiffHtaccess=ob_get_contents();
ob_end_clean();
ob_start();
system(sprintf("diff %s/.gitignore .gitignore",$nomDossier));
$resultatDiffGitIgnore=ob_get_contents();
ob_end_clean();
ob_start();
system(sprintf("diff %s/robots.txt robots.txt",$nomDossier));
$resultatDiffRobots=ob_get_contents();
ob_end_clean();
ob_start();
system(sprintf("diff %s/sites/default/default.settings.php sites/default/settings.php",$nomDossier));
$resultatDiffSettings=ob_get_contents();
ob_end_clean();
return array(
'nomDossier' => $nomDossier,
'resultatCommande' => $resultatCommande,
'resultatDiffHtaccess' => $resultatDiffHtaccess,
'resultatDiffSettings' => $resultatDiffSettings,
'resultatDiffGitIgnore' => $resultatDiffGitIgnore,
'resultatDiffRobots' => $resultatDiffRobots
);
}
function majDrupalCore($dossierPath) {
$aConserver = array(
'.htaccess',
'.gitignore',
'robots.txt',
'sites/',
'profiles/'
);
ob_start();
foreach($aConserver as $itemAConserver) {
system(sprintf("rm -rf %s/%s", $dossierPath,$itemAConserver));
}
$aSupprimer = glob($dossierPath.'/*');
foreach($aSupprimer as $itemASupprimer) {
system(sprintf("rm -rf %s",basename($itemASupprimer)));
}
system(sprintf("mv %s/* ./",$dossierPath));
system(sprintf("rm -rf %s/",$dossierPath));
$resultatCommande=ob_get_contents();
ob_end_clean();
return array(
'resultatCommande' => $resultatCommande
);
}
?>
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Système de mise à jour de Drupal sur OVH</title>
</head>
<body>
<?php if(empty($_POST)): ?>
<h1>Système de mise à jour de Drupal sur OVH</h1>
<form action="" method="post">
<input type='hidden' name="etape" value="1" />
<input type="submit" value="Etape 1 : Backup des fichiers" />
</form>
<?php elseif(isset($_POST['etape']) && !empty($_POST['etape']) && $_POST['etape'] == 1): ?>
<?php $resultatEtape = backupFiles(); ?>
<p>
<a href="<?php echo $resultatEtape['nomFichier']; ?>" >Télécharger le backup</a>
</p>
<form action="" method="post">
<input type='hidden' name="etape" value="2" />
<input type="submit" value="Etape 2 : Backup de la base de données" />
</form>
<br />
<div>
<p>Log des commandes :</p>
<pre style="background-color: gray;">
<?php echo $resultatEtape['resultatCommande']; ?>
</pre>
</div>
<?php elseif(isset($_POST['etape']) && !empty($_POST['etape']) && $_POST['etape'] == 2): ?>
<?php $resultatEtape = backupBdd($hostBdd, $nameBdd, $userBdd, $passwordBdd); ?>
<p>
<a href="<?php echo $resultatEtape['nomFichier']; ?>" >Télécharger le backup de la base de données</a>
</p>
<br />
<form action="" method="post">
<input type='hidden' name="etape" value="3" />
<input type="submit" value="Etape 3 : Téléchargement de Drupal" />
</form>
<div>
<p>Log des commandes :</p>
<pre style="background-color: gray;">
<?php echo $resultatEtape['resultatCommande']; ?>
</pre>
</div>
<?php elseif(isset($_POST['etape']) && !empty($_POST['etape']) && $_POST['etape'] == 3): ?>
<?php $resultatEtape = getDrupal($urlDrupal); ?>
<form action="" method="post">
<input type='hidden' name="etape" value="4" />
<input type='hidden' name="dossierPath" value="<?php echo $resultatEtape['nomDossier']; ?>" />
<input type="submit" value="Etape 4 : Mise à jour des fichiers" />
</form>
<p>
<a target="_blank" href="<?php $resultatEtape['nomDossier'] ?>/CHANGELOG.txt" >Voir le changelog</a>
</p>
<div>
<p>Diff du htaccess:</p>
<pre style="background-color: gray;">
<?php echo $resultatEtape['resultatDiffHtaccess']; ?>
</pre>
</div>
<div>
<p>Diff du .gitignore:</p>
<pre style="background-color: gray;">
<?php echo $resultatEtape['resultatDiffGitIgnore']; ?>
</pre>
</div>
<div>
<p>Diff du robots.txt:</p>
<pre style="background-color: gray;">
<?php echo $resultatEtape['resultatDiffRobots']; ?>
</pre>
</div>
<div>
<p>Diff du settings.php:</p>
<pre style="background-color: gray;">
<?php echo $resultatEtape['resultatDiffSettings']; ?>
</pre>
</div>
<div>
<p>Log des commandes :</p>
<pre style="background-color: gray;">
<?php echo $resultatEtape['resultatCommande']; ?>
</pre>
</div>
<?php elseif(isset($_POST['etape']) && !empty($_POST['etape']) && $_POST['etape'] == 4 && isset($_POST['dossierPath']) && !empty($_POST['dossierPath'])): ?>
<?php
$dossierPath = $_POST['dossierPath'];
$resultatEtape = majDrupalCore($dossierPath);
?>
<a target="_blank" href="update.php" >Mettre à jour la base de données</a>
<div>
<p>Log des commandes :</p>
<pre style="background-color: gray;">
<?php echo $resultatEtape['resultatCommande']; ?>
</pre>
</div>
<?php endif; ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment