Dear All,
I have used ChatGPT and written code to automatically update Nextcloud as follows.
You can refer to and modify it according to your own situation.
Please note that you are responsible for any errors that may occur.
update_nextcloud_fn.php
the community strongly recommends to not using this script because its insecure and really dangerous. It could harm your server and lead to dataloss.
<?php
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
function sendEmail($subject, $body, $to, $from) {
$mail = new PHPMailer(true);
try {
// Server settings
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->isSMTP();
$mail->Host = '';
$mail->SMTPAuth = true;
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Recipients
$mail->setFrom($from, '');
$mail->addAddress($to);
// Content
$mail->isHTML(false);
$mail->Subject = $subject;
$mail->Body = $body;
// Email headers
$mail->addCustomHeader('MIME-Version: 1.0');
$mail->addCustomHeader('Content-Type: text/plain; charset=UTF-8');
$mail->addCustomHeader('Content-Transfer-Encoding: 8bit');
$mail->send();
echo "Message sent successfully\n";
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}\n";
}
}
function getDomainFromConfigFile() {
$configFilePath = '/var/www/nextcloud/config/config.php';
if (file_exists($configFilePath)) {
$config = include($configFilePath);
if (isset($config['trusted_domains'][0])) {
return $config['trusted_domains'][0];
}
}
return null;
}
function getDomainFromNginxConfig() {
$nginxConfigPath = '/etc/nginx/conf.d/nextcloud.conf';
$domain = null;
if (file_exists($nginxConfigPath)) {
$configContent = file_get_contents($nginxConfigPath);
$pattern = '/server_name\s+([\w.-]+);/';
if (preg_match($pattern, $configContent, $matches)) {
$domain = $matches[1];
}
}
return $domain;
}
$bashScript = <<<BASH
#!/bin/bash
if [ ! -d "/var/www/old" ]; then
mkdir /var/www/old
fi
# Download latest Nextcloud release
for i in {1..7}; do
wget https://download.nextcloud.com/server/releases/latest.zip && break
if [ \$i -eq 7 ]; then
php -f /var/www/update_nextcloud_fn.php download_failed
exit 1
fi
sleep 5
done
# Calculate the md5 checksum of both local and remote files
remote_md5=\$(md5sum latest.zip | awk '{print \$1}')
local_md5=\$(md5sum /var/www/old/latest.zip | awk '{print \$1}')
# Compare the md5 checksum
if [ "\$remote_md5" != "\$local_md5" ]; then
# Rename the current version
rm -rf /var/www/old_nextcloud
mv /var/www/nextcloud /var/www/old_nextcloud
# Unzip the latest version to the /var/www/ directory
unzip latest.zip -d /var/www/
# Remove the previous version
rm /var/www/old/latest.zip
# Store the new version
mv latest.zip /var/www/old/
# Remove the new version from the current directory
rm latest.zip
# Remove the new config files
rm -rf /var/www/nextcloud/config/*
# Copy the old config files
cp -r /var/www/old_nextcloud/config/* /var/www/nextcloud/config/
# Set the correct ownership and permissions
restorecon -R '/var/www/nextcloud/'
chown -R nginx:nginx /var/www/nextcloud/
chown -R nginx:nginx /var/lib/php/{session,opcache}
# Restart Nginx
systemctl restart nginx
# Upgrade Nextcloud
sudo -u nginx php /var/www/nextcloud/occ upgrade
# Send email notification if update is successful
if [ \$? -eq 0 ]; then
php -f /var/www/update_nextcloud_fn.php success
fi
else
echo "Nextcloud is already up to date"
# Remove the downloaded file if it's not needed
rm latest.zip
# Set the correct ownership and permissions
chown -R nginx:nginx /var/www/nextcloud/
chown -R nginx:nginx /var/lib/php/{session,opcache}
# Restart Nginx
systemctl restart nginx
fi
BASH;
file_put_contents('update_nextcloud_2023.sh', $bashScript);
chmod('update_nextcloud_2023.sh', 0755);
if (isset($argv[1]) && $argv[1] === 'success') {
$domain = getDomainFromConfigFile();
if ($domain === null) {
$domain = getDomainFromNginxConfig();
}
if ($domain === null) {
echo "Could not find domain in Nextcloud or Nginx config files.\n";
exit(1);
}
$subject = "Nextcloud has been updated";
$body = "The Nextcloud update was successful on $domain.";
$to = "";
$from = "";
sendEmail($subject, $body, $to, $from);
} elseif (isset($argv[1]) && $argv[1] === 'download_failed') {
$subject = "Nextcloud update failed";
$body = "Nextcloud update failed because the latest.zip file could not be downloaded after 7 attempts.";
$to = "";
$from = "";
sendEmail($subject, $body, $to, $from);
}
Please configure crontab to run the update_nextcloud_2023.sh script daily.
Thank you sincerely,