techc0de Posted July 22, 2021 Share Posted July 22, 2021 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."); } Quote Link to comment https://forums.phpfreaks.com/topic/313412-help-with-revise-php-script/ Share on other sites More sharing options...
requinix Posted July 22, 2021 Share Posted July 22, 2021 If you're looking for a suggestion, I would say to stop using PHP and switch to an actual bash script: there's nothing in there that needs PHP, and what's more, a lot of what it does is shell commands. Quote Link to comment https://forums.phpfreaks.com/topic/313412-help-with-revise-php-script/#findComment-1588488 Share on other sites More sharing options...
techc0de Posted July 22, 2021 Author Share Posted July 22, 2021 Hello, I have a bash script for the task, but if I continue to use it, then I'm not going to learn anything new. People have the tendency to stick within their comfort zone. Quote Link to comment https://forums.phpfreaks.com/topic/313412-help-with-revise-php-script/#findComment-1588493 Share on other sites More sharing options...
requinix Posted July 22, 2021 Share Posted July 22, 2021 "For the sake of learning" is a good argument, but so is "use the right tool for the job". Learning about PHP includes knowing what it's good for and what it isn't good for. Surely there are other situations where you could apply PHP? Perhaps some scripting that isn't so straightforward? But if you really want to do this in PHP then there are things to consider, like - Make this a proper CLI script by taking arguments instead of prompting the user with questions - Don't use chdir to change directories and instead do whatever work needs to be done using relative paths - There's no need to call wget when PHP is perfectly capable of "copying" files from the internet - It can even extract ZIP files Quote Link to comment https://forums.phpfreaks.com/topic/313412-help-with-revise-php-script/#findComment-1588499 Share on other sites More sharing options...
techc0de Posted July 22, 2021 Author Share Posted July 22, 2021 (edited) I agree with you that PHP is not the best tool for this task, so far this is the first script that I converted to PHP from Bash. When you said using arguments, did you mean using functions or a class ? regarding download file, I tried curl but it didn't work. If you can show some examples, that would be helpful. Edited July 22, 2021 by techc0de Quote Link to comment https://forums.phpfreaks.com/topic/313412-help-with-revise-php-script/#findComment-1588500 Share on other sites More sharing options...
kicken Posted July 22, 2021 Share Posted July 22, 2021 26 minutes ago, techc0de said: When you said using arguments, did you mean using functions or a class ? The suggestion is to use command-line arguments rather than prompts. Doing that makes your script more versatile as you don't need to be there to answer questions when it's run. For example, you'd set it up so you can run say: ./update_nextcloud --backup-current --version 1.0 Using a library such as Commando makes this relatively simple. 35 minutes ago, techc0de said: regarding download file, I tried curl but it didn't work. If you have http wrappers enabled, which they usually are by default, you don't need curl, you can just copy. copy("https://download.nextcloud.com/server/releases/nextcloud-{$version}.zip", "nextcloud.zip"); Also, Quote shell_exec("cd $NCpath"); Calling shell_exec to run cd is a bit silly, just chdir(). Quote Link to comment https://forums.phpfreaks.com/topic/313412-help-with-revise-php-script/#findComment-1588501 Share on other sites More sharing options...
techc0de Posted July 22, 2021 Author Share Posted July 22, 2021 switch ($verify_version) { case "y": case "Y": if(!@copy("https://download.nextcloud.com/server/releases/nextcloud-{$version}.zip", "nextcloud.zip")) { $errors= error_get_last(); echo "Can't download Nextcloud zip folder: ".$errors['type']; echo "<br />\n".$errors['message']; } else { echo "Nextcloud zipped file downloaded!"; } break; case "n": case "N": exit; } I tried this method, but received error: Can't download Nextcloud zip folder: 2<br /> copy(https://download.nextcloud.com/server/releases/nextcloud-22.0.0.zip): failed to open stream: HTTP request failed! HTTP/1.1 429 Too Many Requests Quote Link to comment https://forums.phpfreaks.com/topic/313412-help-with-revise-php-script/#findComment-1588505 Share on other sites More sharing options...
kicken Posted July 22, 2021 Share Posted July 22, 2021 39 minutes ago, techc0de said: HTTP/1.1 429 Too Many Requests It would seem you've been making too many requests so their server has decided to stop serving you. Wait a while and see if it starts working later. Quote Link to comment https://forums.phpfreaks.com/topic/313412-help-with-revise-php-script/#findComment-1588507 Share on other sites More sharing options...
techc0de Posted July 22, 2021 Author Share Posted July 22, 2021 That's odd, I just tried once, and I got that message. Quote Link to comment https://forums.phpfreaks.com/topic/313412-help-with-revise-php-script/#findComment-1588508 Share on other sites More sharing options...
techc0de Posted July 23, 2021 Author Share Posted July 23, 2021 (edited) I tried the curl module, but it downloaded 0 bytes file. Why is it so difficult to download a file with PHP module ? It's much easier to download with wget module. #!/usr/bin/php <?php $url = "https://download.nextcloud.com/server/releases/nextcloud-22.0.0.zip"; $path = '/tmp/nextcloud.zip'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); curl_close($ch); file_put_contents($path, $data); Edited July 23, 2021 by techc0de Quote Link to comment https://forums.phpfreaks.com/topic/313412-help-with-revise-php-script/#findComment-1588511 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.