Hi all,
I'm been writing Bash script for a while now, and I use it for most of the Linux servers.
My current workplace is currently using PHP to maintain web servers.
So I'm try to learn PHP by write PHP scripts for my servers.
I wrote this script for my Nextcloud server, and I would like to know if there is room for improvement.
I tried to use curl to check for header but it didn't work.
Therefore, I'm using wget module instead.
Thanks
#!/usr/bin/php
<?php
# prerequisites packages: apt-get install php7.4-cli php-zip wget
$today = date('m-d-Y');
$WWWpath = "/config/www";
$NCpath = "/config/www/nextcloud";
$user = 'xfs';
shell_exec("cd $NCpath");
echo "Current working directory: " . getcwd();
echo "\n";
function ask_for_backup()
{
$prompt = readline('Would you like to make a back up of the current Nextcloud ? [y|n] ' );
echo "You selected '$prompt' . \n";
switch ($prompt)
{
case "y":
case "Y":
$backup_version = readline('Please enter a version of Nextcloud you want to backup: ');
echo "\n";
$verify = readline("Is this the correct version ? [y|n]: '{$backup_version}' ");
echo "$verify\n";
if($verify == "y" || $verify == "Y"){
echo "Backing up current Nextcloud directory... \n";
shell_exec("cp -r $NCpath nextcloud-old_${backup_version}");
break;
}
case "n":
case "N":
break;
}
}
// execute function
ask_for_backup();
echo "turn on Nextcloud maintenance mode." . "\n";
shell_exec("occ maintenance:mode --on");
shell_exec("cd $WWWpath");
$version = readline('Please enter the Nextcloud version you want to update: ');
echo "\n";
$verify_version = readline("Is this the correct version ? [y|n]: '{$version}' ");
switch ($verify_version)
{
case "y":
case "Y":
$url = "https://download.nextcloud.com/server/releases/nextcloud-{$version}.zip";
shell_exec("wget $url -O nextcloud.zip");
break;
case "n":
case "N":
exit;
}
// extract Nextcloud zipped file
shell_exec("unzip nextcloud.zip");
shell_exec("cd $NCpath");
// copy Nextcloud old 'config.php' to new installation folder
shell_exec("cp $WWWpath/nextcloud-old_${backup_version}/config/config.php $NCpath/config/config.php");
echo "change user/group permission to xfs.\n";
shell_exec("chown -R $user:$user $NCpath");
echo "performing upgrade ...";
$cmd = "occ upgrade; echo $?";
$status = shell_exec("$cmd");
$status = rtrim($status);
echo "turn off Nextcloud maintenance mode";
echo "\n";
shell_exec('occ maintenance:mode --off');
if ( (int) $status == "0")
{
echo "Awesome, Nextcloud upgrade is done.";
} else {
exit("Nextcloud Upgrade Failed !");
}
// delete downloaded zip file
echo "\n";
shell_exec("cd $WWWpath");
$file = "nextcloud.zip";
// Use unlink() function to delete a file
if (!unlink($file)) {
echo ("$file cannot be deleted due to an error.");
}
else {
echo ("$file has been deleted.");
}