Skip to content

Instantly share code, notes, and snippets.

@simonschaufi
Created May 5, 2021 21:00
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/868a454dcfd6ccde05134ca915088697 to your computer and use it in GitHub Desktop.
Save simonschaufi/868a454dcfd6ccde05134ca915088697 to your computer and use it in GitHub Desktop.
Owncloud update script on netcup server
#!/usr/bin/env bash
# See https://doc.owncloud.org/server/10.5/admin_manual/installation/manual_installation.html#install-owncloud
# Required settings
php="/usr/local/php72/bin/php"
owncloud_dir="/owncloud"
shell_data_dir="/owncloud_data"
# Options: stable, daily, beta, production
channel="stable"
edition="Community"
owncloud_config="${owncloud_dir}/config/config.php"
version=$($php -r "include('${owncloud_config}'); echo \$CONFIG['version'];")
echo "[DEBUG] Current version: $version"
response="$(curl https://updates.owncloud.com/server/?version="${version//./x}"xinstalledatxlastupdatedatx${channel}x${edition}x 2>/dev/null)"
url=$(sed -n -e 's/.*<url>\(.*\)<\/url>.*/\1/p' <<< "$response")
[[ -z "${url}" ]] && {
echo "[INFO] No update available.";
exit 0;
}
newVersion=$(sed -n -e 's/.*<version>\(.*\)<\/version>.*/\1/p' <<< "$response")
echo "[DEBUG] New version available: $newVersion";
echo "[DEBUG] Downloading: $url";
wget -q ${url}
# Save datadirectory
datadirectory=$(${php} -r "include('${owncloud_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',#" ${owncloud_config}
echo "[DEBUG] Turn on maintenance mode..."
cd ${owncloud_dir}
${php} occ maintenance:mode --on
cd ..
# Backup database
dbuser=$(${php} -r "include('${owncloud_config}'); echo \$CONFIG['dbuser'];")
dbpassword=$(${php} -r "include('${owncloud_config}'); echo \$CONFIG['dbpassword'];")
dbhost=$(${php} -r "include('${owncloud_config}'); echo \$CONFIG['dbhost'];")
dbname=$(${php} -r "include('${owncloud_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" > owncloud_"$(date +"%Y%m%d%H%M%S")".sql
# Cleanup before upgrade
cd ${owncloud_dir}
rm -rf core
rm -rf lib
rm -rf updater
cd ..
# Run the upgrade
unzip -o -q owncloud-*.zip
echo "[DEBUG] Run the upgrade..."
cd ${owncloud_dir}
${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',#" ${owncloud_config}
version=$($php -r "include('${owncloud_config}'); echo \$CONFIG['version'];")
echo "[DEBUG] Successfully updated to version: $version"
# Cleanup
cd ..
rm owncloud-*.zip
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment