Skip to content

Instantly share code, notes, and snippets.

@simonschaufi
Created October 26, 2023 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonschaufi/b022b3e78898fa1e00d9620aea73d8a5 to your computer and use it in GitHub Desktop.
Save simonschaufi/b022b3e78898fa1e00d9620aea73d8a5 to your computer and use it in GitHub Desktop.
Update nextcloud bash script
#!/usr/bin/env bash
# Console colors: https://dev.to/ifenna__/adding-colors-to-bash-scripts-48g4
# Required settings (adjust as nessesary)
php="/usr/bin/php7.4"
nextcloud_dir="/home/www/nextcloud"
shell_data_dir="/home/nextcloud_data"
# Options: stable, daily, beta, production
channel="stable"
edition=""
nextcloud_config="${nextcloud_dir}/config/config.php"
version_info="${nextcloud_dir}/version.php"
version=$($php -r "include('${version_info}'); echo implode('.', \$OC_Version);")
echo -e "\e[36m[INFO] Current version: $version\e[0m";
php_major=$($php -r "echo PHP_MAJOR_VERSION;")
php_minor=$($php -r "echo PHP_MINOR_VERSION;")
echo "[DEBUG] PHP version: ${php_major}.${php_minor}"
check_url="https://updates.nextcloud.com/updater_server/?version=${version//./x}xxx${channel}x${edition}xx${php_major}x${php_minor}x"
echo "[DEBUG] Check url: ${check_url}"
response="$(curl "${check_url}" 2>/dev/null)"
url=$(sed -n -e 's/.*<url>\(.*\)<\/url>.*/\1/p' <<< "$response")
[[ -z "${url}" ]] && {
echo -e "\e[36m[INFO] No update available.\e[0m";
exit 0;
}
newVersion=$(sed -n -e 's/.*<version>\(.*\)<\/version>.*/\1/p' <<< "$response")
echo -e "\e[33m[INFO] New version available: $newVersion\e[0m";
echo "[DEBUG] Downloading: $url";
wget -q "${url}"
returnValue=$?
if [ ${returnValue} -ne 0 ]; then
echo -e "\e[32mDownload failed\e[0m"
exit ${returnValue}
fi
# Save datadirectory
datadirectory=$(${php} -r "include('${nextcloud_config}'); echo \$CONFIG['datadirectory'];")
echo "[DEBUG] Data directory: ${datadirectory}"
echo "[DEBUG] Replace datadirectory with ${shell_data_dir}"
sed -i "s#'datadirectory' => .*#'datadirectory' => '$shell_data_dir',#" ${nextcloud_config}
echo "[DEBUG] Turn on maintenance mode..."
cd ${nextcloud_dir} || exit
${php} occ maintenance:mode --on
cd ..
# Backup database
dbuser=$(${php} -r "include('${nextcloud_config}'); echo \$CONFIG['dbuser'];")
dbpassword=$(${php} -r "include('${nextcloud_config}'); echo \$CONFIG['dbpassword'];")
dbhost=$(${php} -r "include('${nextcloud_config}'); echo \$CONFIG['dbhost'];")
dbname=$(${php} -r "include('${nextcloud_config}'); echo \$CONFIG['dbname'];")
# Thanks to https://unix.stackexchange.com/a/417188
default_port=3306
case ${dbhost} in
(*:*) host=${dbhost%:*} port=${dbhost##*:};;
(*) host=${dbhost} port=${default_port};;
esac
echo "[DEBUG] Creating mysql backup..."
mysqldump -u "$dbuser" -p"$dbpassword" -h "$host" -P "$port" "$dbname" > nextcloud_"$(date +"%Y%m%d%H%M%S")".sql
# Cleanup before upgrade
cd ${nextcloud_dir} || exit
rm -rf core
rm -rf lib
rm -rf updater
cd ..
# Run the upgrade
unzip -o -q nextcloud-*.zip
returnValue=$?
if [ ${returnValue} -ne 0 ]; then
echo -e "\e[31munzip failed: ${returnValue}\e[0m"
exit ${returnValue}
fi
echo "[DEBUG] Run the upgrade..."
cd ${nextcloud_dir} || exit
${php} occ upgrade
echo "[DEBUG] Turn off maintenance mode..."
${php} occ maintenance:mode --off
# Restore old datadirectory
echo "[DEBUG] Restore datadirectory with ${datadirectory}"
sed -i "s#'datadirectory' => .*#'datadirectory' => '$datadirectory',#" ${nextcloud_config}
version=$($php -r "include('${version_info}'); echo implode('.', \$OC_Version);")
echo -e "\e[32m[INFO] Successfully updated to version: $version\e[0m"
changeLog=$(sed -n -e 's/.*<changes>\(.*\)<\/changes>.*/\1/p' <<< "$response")
echo -e "\e[34m[INFO] Changelog: $changeLog\e[0m";
# Cleanup
cd ..
rm nextcloud-*.zip
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment