Skip to content

Commit

Permalink
added nc-update-nextcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
nachoparker committed May 14, 2018
1 parent adf38df commit 935cf30
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 4 deletions.
184 changes: 184 additions & 0 deletions bin/ncp-update-nc
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
#!/bin/bash

# Upgrade to a different Nextcloud version
#
# Copyleft 2017 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com>
# GPL licensed (see end of file) * Use at your own risk!
#
# Usage:
# bash ncp-update-nc <version>
#
# More at https://ownyourbits.com
#

# test cases
####################
# - with and without moving datadir
# - failure at each test point
# -> must pass basic NC in tests.py ( meaning it's not in a broken state )

set -eE

VER="$1"

# pre-checks
####################
cd /var/www/
DATADIR="$( grep datadirectory nextcloud/config/config.php | awk '{ print $3 }' | grep -oP "[^']*[^']" | head -1 )"
[[ -d /var/www/nextcloud-old ]] && { echo "Nextcloud backup directory found. Interrupted installation?"; exit 1; }
[[ -d /var/www/nextcloud ]] || { echo "Nextcloud directory not found" ; exit 1; }
[[ -d "$DATADIR" ]] || { echo "Nextcloud data directory not found" ; exit 1; }

# check version
####################

[[ ${EUID} -eq 0 ]] && SUDO="sudo -u www-data"
CURRENT="$( $SUDO php /var/www/nextcloud/occ status | grep "version:" | awk '{ print $3 }' )"

grep -qP "\d+\.\d+\.\d+" <<<"$CURRENT" || { echo "Malformed version $CURRENT"; exit 1; }
grep -qP "\d+\.\d+\.\d+" <<<"$VER" || { echo "Malformed version $VER" ; exit 1; }

MAJOR=$( grep -oP "\d+\.\d+\.\d+" <<<"$VER" | cut -d. -f1 )
MINOR=$( grep -oP "\d+\.\d+\.\d+" <<<"$VER" | cut -d. -f2 )
PATCH=$( grep -oP "\d+\.\d+\.\d+" <<<"$VER" | cut -d. -f3 )

MAJ=$( grep -oP "\d+\.\d+\.\d+" <<<"$CURRENT" | cut -d. -f1 )
MIN=$( grep -oP "\d+\.\d+\.\d+" <<<"$CURRENT" | cut -d. -f2 )
PAT=$( grep -oP "\d+\.\d+\.\d+" <<<"$CURRENT" | cut -d. -f3 )

NEED_UPDATE=false
if [ "$MAJOR" -gt "$MAJ" ]; then
NEED_UPDATE=true
elif [ "$MAJOR" -eq "$MAJ" ] && [ "$MINOR" -gt "$MIN" ]; then
NEED_UPDATE=true
elif [ "$MAJOR" -eq "$MAJ" ] && [ "$MINOR" -eq "$MIN" ] && [ "$PATCH" -gt "$PAT" ]; then
NEED_UPDATE=true
fi

echo "Current Nextcloud version $CURRENT"
echo "Available Nextcloud version $VER"
[[ "$NEED_UPDATE" == "true" ]] || { echo "Nothing to update"; exit 0; }

# cleanup
####################
cleanup() {
local RET=$?
set +eE
echo "Clean up..."
rm -rf /var/www/nextcloud.tar.bz2 /var/www/nextcloud-old
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off
trap "" EXIT
exit $RET
}
trap cleanup EXIT

# get new code
####################
URL="https://download.nextcloud.com/server/releases/nextcloud-$VER.tar.bz2"
echo "Download Nextcloud $VER..."
wget -q "$URL" -O nextcloud.tar.bz2 || { echo "Error downloading"; exit 1; }

# backup
####################
BKPDIR=/var/www/
WITH_DATA=no
COMPRESSED=yes
LIMIT=0

sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on

echo "Back up current instance..."
set +eE
ncp-backup "$BKPDIR" "$WITH_DATA" "$COMPRESSED" "$LIMIT" # && false # test point
RET=$?
set -eE

BKP="$( ls -1t "$BKPDIR"/nextcloud-bkp_*.tar.gz 2>/dev/null | head -1 )"
[[ -f "$BKP" ]] || { set +eE; echo "Error backing up"; false || cleanup; }
[[ $RET -ne 0 ]] && { rm -f "$BKP"; set +eE; echo "Error backing up"; false || cleanup; }

# simple restore if anything fails from here
####################
rollback_simple() {
set +eE
trap "" INT TERM HUP ERR
echo -e "Abort\nSimple roll back..."
rm -rf /var/www/nextcloud
mv /var/www/nextcloud-old /var/www/nextcloud
false || cleanup # so cleanup exits with 1
}
trap rollback_simple INT TERM HUP ERR

# replace code
####################
echo "Install Nextcloud $VER..."
mv -T nextcloud nextcloud-old
tar -xf nextcloud.tar.bz2 # && false # test point
rm -rf /var/www/nextcloud.tar.bz2

# copy old config
####################
cp nextcloud-old/config/config.php nextcloud/config/

# copy old themes
####################
cp -raT nextcloud-old/themes/ nextcloud/themes/

# copy apps
####################
echo "Restore apps..."
for app in $( ls nextcloud-old/apps/*/ -d ); do
app=$( basename "$app" )
[[ ! -d nextcloud/apps/$app ]] && {
echo " * $app"
cp -r nextcloud-old/apps/$app nextcloud/apps
}
done
#false # test point

# copy data if it was at the default location
####################
[[ "$DATADIR" == "/var/www/nextcloud/data" ]] && {
echo "Restore data..."
mv -T nextcloud-old/data nextcloud/data
}

# nc-restore if anything fails from here
####################
rollback() {
set +eE
trap "" INT TERM HUP ERR EXIT
echo -e "Abort\nClean up..."
rm -rf /var/www/nextcloud.tar.bz2 /var/www/nextcloud-old
echo "Rolling back to backup $BKP..."
local TMPDATA="$( mktemp -d "/var/www/ncp-data.XXXXXX" )" || { echo "Failed to create temp dir" >&2; exit 1; }
[[ "$DATADIR" == "/var/www/nextcloud/data" ]] && mv -T "$DATADIR" "$TMPDATA"
ncp-restore "$BKP" || { echo "Rollback failed! Data left at $TMPDATA"; exit 1; }
[[ "$DATADIR" == "/var/www/nextcloud/data" ]] && { rm -rf "$DATADIR"; mv -T "$TMPDATA" "$DATADIR"; }
echo "Rollback successful. Nothing was updated"
exit 1
}
trap rollback INT TERM HUP ERR

# fix permissions
####################
echo "Fix permissions..."
chown -R www-data:www-data nextcloud
find nextcloud/ -type d -exec chmod 750 {} \;
find nextcloud/ -type f -exec chmod 640 {} \;

# upgrade
####################
echo "Upgrade..."
sudo -u www-data php nextcloud/occ upgrade # && false # test point

[[ -f nextcloud-old/.user.ini ]] && cp nextcloud-old/.user.ini nextcloud/

# done
####################
mkdir -p "$DATADIR"/ncp-update-backups
mv "$BKP" "$DATADIR"/ncp-update-backups
chown -R www-data:www-data "$DATADIR"/ncp-update-backups
BKP="$DATADIR"/ncp-update-backups/"$( basename "$BKP" )"
echo "Backup stored at $BKP"

18 changes: 16 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@

[v0.54.7](https://github.com/nextcloud/nextcloudpi/commit/890f0c6) (2018-05-10) armbian: fix static IP
[v0.55.0](https://github.com/nextcloud/nextcloudpi/commit/e270979) (2018-05-11) added nc-update-nextcloud

[v0.54.6](https://github.com/nextcloud/nextcloudpi/commit/008f065) (2018-05-10) nc-notify-updates: fix wrong user
[v0.54.13](https://github.com/nextcloud/nextcloudpi/commit/057f531) (2018-05-11) nc-nextcloud: fix upload tmp dir

[v0.54.12](https://github.com/nextcloud/nextcloudpi/commit/d8fc729) (2018-05-11) nc-restore: fix tmp dirs in backups without data

[v0.54.11](https://github.com/nextcloud/nextcloudpi/commit/784c99e) (2018-05-11) nc-backup: make more robust to unexpected failure

[v0.54.10](https://github.com/nextcloud/nextcloudpi/commit/41b35c8) (2018-05-11) nc-restore: make more robust to unexpected failure

[v0.54.9 ](https://github.com/nextcloud/nextcloudpi/commit/c45f099) (2018-05-11) nc-restore: separate in its own executable

[v0.54.8 ](https://github.com/nextcloud/nextcloudpi/commit/4224e3c) (2018-05-10) nc-backup: better avoid duplicates

[v0.54.7](https://github.com/nextcloud/nextcloudpi/commit/011671a) (2018-05-10) armbian: fix static IP

[v0.54.6 ](https://github.com/nextcloud/nextcloudpi/commit/008f065) (2018-05-10) nc-notify-updates: fix wrong user

[v0.54.5 ](https://github.com/nextcloud/nextcloudpi/commit/ee05dbc) (2018-05-10) armbian: fix mDNS

Expand Down
3 changes: 1 addition & 2 deletions etc/ncp-config.d/nc-nextcloud.sh
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,9 @@ configure()
chown -R www-data:www-data $OPCACHEDIR

## RE-CREATE DATABASE TABLE
echo "Starting mariaDB"

# launch mariadb if not already running (for docker build)
if ! pgrep -c mysqld &>/dev/null; then
echo "Starting mariaDB"
mysqld &
fi

Expand Down
43 changes: 43 additions & 0 deletions etc/ncp-config.d/nc-update-nextcloud.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

# Update current instance to a new Nextcloud version
#
# Copyleft 2017 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com>
# GPL licensed (see end of file) * Use at your own risk!
#
# Usage:
#
# ./installer.sh nc-update-nextcloud.sh <IP> (<img>)
#
# See installer.sh instructions for details
#
# More at https://ownyourbits.com/2017/02/13/nextcloud-ready-raspberry-pi-image/
#

VERSION_=13.0.2
DESCRIPTION="Update current instance to a new Nextcloud version"

configure()
{
bash /usr/local/bin/ncp-update-nc "$VERSION_"
}

install() { :; }

# License
#
# This script is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA

0 comments on commit 935cf30

Please sign in to comment.