Shell script for backup and upgrade (nextcloud or owncloud)

This is my cloud-update.sh shell script for my owncloud/nextcloud running on my shared hosting. This shell script is for running as root of the server. Feel free to use it or edit it. I used the script for upgrading from owncloud 9.0.2 to nextcloud 9.0.51 without any problems, only the contacts and calendar apps had to be installed through browser again.

You need to edit this script to fit your server and Cloud config! Edit at least the following variables:

  • wwpath
  • ocpath
  • ocdata
  • htuser
  • htgroup
  • downloadbase: first part of downloadlink from nextcloud or onwloud
  • filebase: nextcloud or owncloud
  • DBUser
  • DBName
  • DBPASS
  • You need to import the GPG key of nextcloud to get a valid GPG check!
ocpath='/var/www/vhosts/example.de/cloud.example.de'
ocdata='/var/www/vhosts/example.de/clouddata'
htuser='user'
htgroup='group'
downloadbase='https://download.nextcloud.com/server/releases/' # Version is asked .tar.bz2 is used
filebase='nextcloud'
#https://download.nextcloud.com/server/releases/
#https://download.owncloud.org/community/
datetime=$(date "+%F-%H-%M-%S")

echo "Starting cloud upgrade"

cd ${ocpath}
sudo -u ${htuser} php occ -V
read -p "Which version do you like to install? e.g. 9.0.51: " DLVERSION
sudo -u ${htuser} php occ maintenance:mode --on
sudo -u ${htuser} php occ app:disable contacts
sudo -u ${htuser} php occ app:disable calendar

echo " -> backup, systemfiles, started"
cd ${wwpath}
sudo mkdir ${wwpath}/${datetime}-cloud-backup
sudo chmod 777 ${wwpath}/${datetime}-cloud-backup/
sudo cp -R ${ocpath}/. ${wwpath}/${datetime}-cloud-backup/ #/. copping also hidden files
echo " -> backup, systemfiles, finished"
echo " -> backup, database, started"
sudo mysqldump --opt --user=DBuser --password="DBPASS" --databases DBName | gzip -c -9 > ${wwpath}/${datetime}-cloud-backup/database.gz
sudo chown -R ${htuser}:${htgroup} ${wwpath}/${datetime}-cloud-backup/
echo " -> backup, database, finished"
echo " -> download files started"
wget ${downloadbase}/${filebase}-${DLVERSION}.tar.bz2
wget ${downloadbase}/${filebase}-${DLVERSION}.tar.bz2.sha256
wget ${downloadbase}/${filebase}-${DLVERSION}.tar.bz2.asc
echo " -> download files finished"
sha256sum -c ${filebase}-${DLVERSION}.tar.bz2.sha256
gpg --verify ${filebase}-${DLVERSION}.tar.bz2.asc ${filebase}-${DLVERSION}.tar.bz2
echo " -> download, check"
echo -n "Please check if the above sha256 and GPG check is ok and enter: (yes/no)? "
read answer
if echo "$answer" | grep -iq "^yes" ;then
  echo "-> delete cloud folder: ${ocpath}"
  sudo rm -rf ${wwpath}/cloud.example.de
  echo "-> create new cloud folder"
  sudo mkdir ${wwpath}/cloud.example.de
  #we are still in wwpath
  echo "-> extract files"
  tar -xjvf ${filebase}-${DLVERSION}.tar.bz2
  echo "-> move extrated files to new folder"
  sudo cp -R ${wwpath}/${filebase}/. ${wwpath}/cloud.example.de
  echo "-> copy old config to new folder"
  sudo cp ${wwpath}/${datetime}-cloud-backup/config/config.php ${wwpath}/cloud.example.de/config/
  echo "-> delete downloads and tmp folder"
  #delete no more needed files and folders
  sudo rm ${filebase}-${DLVERSION}.tar.bz2
  sudo rm ${filebase}-${DLVERSION}.tar.bz2.sha256
  sudo rm ${filebase}-${DLVERSION}.tar.bz2.asc
  sudo rm -rf ${wwpath}/${filebase}
  echo "-> set user rights in new folder ...this will take some time (10 minute or more) please be patient!"
  #set user rights
  sudo find ${ocpath}/ -type f -print0 | xargs -0 chmod 0644 # maybe 0640 is also ok for your config!
  echo "-> set user rights 1/6"
  sudo find ${ocpath}/ -type d -print0 | xargs -0 chmod 0755 # maybe 0750 is also ok for your config!
  echo "-> set user rights 2/6"
  sudo chown -R ${htuser}:${htgroup} ${ocpath}/
  echo "-> set user rights 3/6"
  find ${ocdata}/ -type f -print0 | xargs -0 chmod 0640
  echo "-> set user rights 4/6"
  find ${ocdata}/ -type d -print0 | xargs -0 chmod 0750
  echo "-> set user rights 5/6"
  sudo chown -R ${htuser}:${htgroup} ${ocdata}/

  if [ -f ${ocpath}/.htaccess ]
   then
  chmod 0644 ${ocpath}/.htaccess
  chown ${htuser}:${htgroup} ${ocpath}/.htaccess
  fi

  if [ -f {ocdata}/.htaccess ]
   then
  chmod 0644 {ocdata}/.htaccess
  chown ${htuser}:${htgroup} ${ocdata}/.htaccess
  fi
  echo "-> set user rights 6/6"
  #do the upgrade
  cd ${ocpath}
  sudo -u ${htuser} php occ upgrade
else
    echo "No files have been copied or extracted, please delete downloads manual!"
fi
cd ${ocpath}
sudo -u ${htuser} php occ maintenance:mode --off
sudo -u ${htuser} php occ -V
sudo -u ${htuser} php occ app:enable contacts
sudo -u ${htuser} php occ app:enable calendar
7 Likes

I use that for updating programatically the apps:

I still can’t figure out how to upgrade programatically…

Anyway, thanks @flower for the nice script!

2 Likes

Thanks! Please note that the variable ${wwpath} does not seem to be defined in your script.

Indeed, the script is missing many variable declarations