Jump to content

Recommended Posts

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.");
}

 

Link to comment
https://forums.phpfreaks.com/topic/313412-help-with-revise-php-script/
Share on other sites

"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

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 by techc0de
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().

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

 

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 by techc0de
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.