diff options
author | nachoparker <nacho@ownyourbits.com> | 2017-12-03 16:48:08 +0300 |
---|---|---|
committer | nachoparker <nacho@ownyourbits.com> | 2017-12-03 23:54:40 +0300 |
commit | d40360c82ef890ac743d1071adfdc57e549afdc7 (patch) | |
tree | 344cf902946e5600b5d5c7d35b9494668e84166e | |
parent | beb8f1cb0c96d2524e9a8980a9cda4854638b2e7 (diff) |
added btrfs snapshotsv0.40.0
-rw-r--r-- | changelog.md | 8 | ||||
-rw-r--r-- | etc/nextcloudpi-config.d/nc-datadir.sh | 32 | ||||
-rw-r--r-- | etc/nextcloudpi-config.d/nc-format-USB.sh | 12 | ||||
-rw-r--r-- | etc/nextcloudpi-config.d/nc-snapshot.sh | 83 | ||||
-rwxr-xr-x | update.sh | 6 |
5 files changed, 121 insertions, 20 deletions
diff --git a/changelog.md b/changelog.md index 2570ea76..8b8ef0eb 100644 --- a/changelog.md +++ b/changelog.md @@ -1,11 +1,13 @@ -[v0.39.3](https://github.com/nextcloud/nextcloudpi/commit/601f935) (2017-11-29) nc-export: silent cd +[v0.40.0](https://github.com/nextcloud/nextcloudpi/commit/1aa59f9) (2017-12-03) added btrfs snapshots -[v0.39.2](https://github.com/nextcloud/nextcloudpi/commit/76d45cf) (2017-11-29) nc-import: fix ncp-web appearing to fail when activating options that restart httpd +[v0.39.3](https://github.com/nextcloud/nextcloudpi/commit/fbf2079) (2017-11-29) nc-export: silent cd + +[v0.39.2 ](https://github.com/nextcloud/nextcloudpi/commit/76d45cf) (2017-11-29) nc-import: fix ncp-web appearing to fail when activating options that restart httpd [v0.39.1 ](https://github.com/nextcloud/nextcloudpi/commit/6af40a9) (2017-11-28) motd: update logo -[v0.39.0](https://github.com/nextcloud/nextcloudpi/commit/a3afa14) (2017-11-27) added nc-export-ncp and nc-import-ncp +[v0.39.0 ](https://github.com/nextcloud/nextcloudpi/commit/a3afa14) (2017-11-27) added nc-export-ncp and nc-import-ncp [v0.38.1 ](https://github.com/nextcloud/nextcloudpi/commit/0943d83) (2017-11-27) nc-info: warn distro diff --git a/etc/nextcloudpi-config.d/nc-datadir.sh b/etc/nextcloudpi-config.d/nc-datadir.sh index dd678e16..8aaafe51 100644 --- a/etc/nextcloudpi-config.d/nc-datadir.sh +++ b/etc/nextcloudpi-config.d/nc-datadir.sh @@ -27,6 +27,12 @@ is_active() [[ "$SRCDIR" != "/var/www/nextcloud/data" ]] } +install() +{ + apt-get update + apt-get install -y --no-install-recommends btrfs-tools +} + configure() { ## CHECKS @@ -39,21 +45,18 @@ configure() [[ "$SRCDIR" == "$DATADIR_" ]] && { echo -e "INFO: data already there"; return 0; } - # check datadir empty + # check datadir exists [ -d $DATADIR_ ] && { - [[ $( find "$DATADIR_" -maxdepth 0 -empty | wc -l ) == 0 ]] && { - local BKP="${DATADIR_}-$( date "+%m-%d-%y" )" - echo "INFO: $DATADIR_ is not empty. Creating backup $BKP" - mv "$DATADIR_" "$BKP" - } - rm -rf "$DATADIR_" + local BKP="${DATADIR_}-$( date "+%m-%d-%y" )" + echo "INFO: $DATADIR_ is not empty. Creating backup $BKP" + mv "$DATADIR_" "$BKP" } local BASEDIR=$( dirname "$DATADIR_" ) [ -d "$BASEDIR" ] || { echo "$BASEDIR does not exist"; return 1; } - grep -q ext <( stat -fc%T "$BASEDIR" ) || { echo -e "Only ext filesystems can hold the data directory"; return 1; } + grep -q -e ext -e btrfs <( stat -fc%T "$BASEDIR" ) || { echo -e "Only ext/btrfs filesystems can hold the data directory"; return 1; } sudo -u www-data test -x "$BASEDIR" || { echo -e "ERROR: the user www-data does not have access permissions over $BASEDIR"; return 1; } @@ -67,8 +70,15 @@ configure() sudo -u www-data php occ maintenance:mode --on echo "moving data dir from $SRCDIR to $DATADIR_..." - cp -ra "$SRCDIR" "$DATADIR_" || return 1 - + + # use subvolumes, if BTRFS + [[ "$( stat -fc%T "$BASEDIR" )" == "btrfs" ]] && { + echo "BTRFS filesystem detected" + btrfs subvolume create "$DATADIR_" || return 1 + } + + cp -raT "$SRCDIR" "$DATADIR_" || return 1 + # tmp upload dir mkdir -p "$DATADIR_/tmp" chown www-data:www-data "$DATADIR_/tmp" @@ -85,8 +95,6 @@ configure() sudo -u www-data php occ maintenance:mode --off } -install() { :; } - # License # # This script is free software; you can redistribute it and/or modify it diff --git a/etc/nextcloudpi-config.d/nc-format-USB.sh b/etc/nextcloudpi-config.d/nc-format-USB.sh index d667a945..fef1f287 100644 --- a/etc/nextcloudpi-config.d/nc-format-USB.sh +++ b/etc/nextcloudpi-config.d/nc-format-USB.sh @@ -1,6 +1,6 @@ #!/bin/bash -# Format a USB external drive as a unique ext4 partition +# Format a USB external drive as a unique BTRFS partition # # 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! @@ -14,7 +14,7 @@ # LABEL_=myCloudDrive -DESCRIPTION="Format an external USB drive as a ext4 partition (dangerous)" +DESCRIPTION="Format an external USB drive as a BTRFS partition (dangerous)" INFOTITLE="Instructions for USB drive formatting" INFO="Make sure that ONLY the USB drive that you want to format is plugged in. @@ -29,7 +29,7 @@ configure() # only one plugged in [[ $NUM != 1 ]] && { - echo "ERROR: counted $NUM devices. Please, only plug in the USB drive you want to format to ext4"; + echo "ERROR: counted $NUM devices. Please, only plug in the USB drive you want to format"; return 1; } @@ -52,11 +52,13 @@ configure() parted /dev/"$NAME" --script -- mklabel gpt || return 2 parted /dev/"$NAME" --script -- mkpart primary 0% 100% || return 3 sleep 0.5 - mkfs.ext4 -q -E lazy_itable_init=0,lazy_journal_init=0 -F /dev/"${NAME}1" -L "$LABEL_" + mkfs.btrfs -q /dev/"${NAME}1" -f -L "$LABEL_" + local RET=$? # enable nc-automount if enabled killall -CONT udiskie 2>/dev/null - echo "Drive $NAME formatted successfuly and labeled $LABEL_" + [ $RET -eq 0 ] && echo "Drive $NAME formatted successfuly and labeled $LABEL_" + return $RET } install() { :; } diff --git a/etc/nextcloudpi-config.d/nc-snapshot.sh b/etc/nextcloudpi-config.d/nc-snapshot.sh new file mode 100644 index 00000000..5df87e18 --- /dev/null +++ b/etc/nextcloudpi-config.d/nc-snapshot.sh @@ -0,0 +1,83 @@ +#!/bin/bash +# Nextcloud BTRFS snapshots +# +# 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-snapshot.sh <IP> (<img>) +# +# See installer.sh instructions for details +# +# More at https://ownyourbits.com/2017/02/13/nextcloud-ready-raspberry-pi-image/ +# + +LIMIT_=4 +DESCRIPTION="Create BTRFS snapshot of the datadir" + +INFO="Snapshots take up very little space because only the differences from one +to the next are saved. This requires the datadir to be in a BTRFS filesystem" + +BASEDIR=/var/www + +configure() +{ + local DATADIR + DATADIR=$( cd "$BASEDIR"/nextcloud; sudo -u www-data php occ config:system:get datadirectory ) || { + echo -e "Error reading data directory. Is NextCloud running and configured?"; + return 1; + } + local SNAPSHOT=${DATADIR}_$( date +"%F" ) + + # file system check + [[ "$( stat -fc%T "$DATADIR" )" != "btrfs" ]] && { + echo "The datadir is not in a BTRFS filesystem" + return 1 + } + local DESTDIR="$( dirname "$DATADIR" )" + + # do it + sudo -u www-data php "$BASEDIR"/nextcloud/occ maintenance:mode --on + + [[ -d "$SNAPSHOT" ]] && { + btrfs subvolume delete "$SNAPSHOT" &>/dev/null + rm -rf "$SNAPSHOT" &>/dev/null + } + btrfs subvolume snapshot -r "$DATADIR" "$SNAPSHOT" || return 1 + + # prune older backups + [[ $LIMIT_ != 0 ]] && { + + local SNAPS=( $( btrfs subvolume list -s --sort=gen "$DESTDIR" | awk '{ print $14 }' ) ) + cd "$DESTDIR" || return 1 + [[ ${#SNAPS[@]} -gt $LIMIT_ ]] && \ + echo "Pruning old snapshots..." && \ + for (( i=0; i < $(( ${#SNAPS[@]} - LIMIT_ )); i++ ));do + btrfs subvolume delete "${SNAPS[$i]}" + done + } + + sudo -u www-data php "$BASEDIR"/nextcloud/occ maintenance:mode --off + echo -e "snapshot $SNAPSHOT generated" +} + +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 + @@ -270,6 +270,12 @@ EOF udevadm control --reload-rules && udevadm trigger pgrep -c udiskie &>/dev/null && systemctl restart nc-automount } + + # btrfs tools + type btrfs &>/dev/null || { + apt-get update + apt-get install -y --no-install-recommends btrfs-tools + } } exit 0 |