diff options
-rw-r--r-- | changelog.md | 12 | ||||
-rw-r--r-- | etc/nextcloudpi-config.d/nc-backup-auto.sh | 81 | ||||
-rw-r--r-- | etc/nextcloudpi-config.d/nc-backup.sh | 154 | ||||
-rwxr-xr-x | update.sh | 6 |
4 files changed, 112 insertions, 141 deletions
diff --git a/changelog.md b/changelog.md index 99fb0a3c..dbb84611 100644 --- a/changelog.md +++ b/changelog.md @@ -1,7 +1,15 @@ -[v0.44.1](https://github.com/nextcloud/nextcloudpi/commit/72699f5) (2017-12-28) nc-snapshot: use btrfs-snp +[v0.44.5](https://github.com/nextcloud/nextcloudpi/commit/0ab0c5a) (2018-01-04) nc-backup: compress backups and refactoring -[v0.44.0](https://github.com/nextcloud/nextcloudpi/commit/15587e8) (2017-12-28) added nc-snapshot-auto +[v0.44.4 ](https://github.com/nextcloud/nextcloudpi/commit/56576da) (2018-01-03) nc-restore: update redis password + +[v0.44.3 ](https://github.com/nextcloud/nextcloudpi/commit/2a91f86) (2018-01-03) nc-export: protect file from read + +[v0.44.2](https://github.com/nextcloud/nextcloudpi/commit/9a18925) (2018-01-01) nc-snapshot: update btrfs-snp + +[v0.44.1 ](https://github.com/nextcloud/nextcloudpi/commit/5a80299) (2017-12-28) nc-snapshot: use btrfs-snp + +[v0.44.0 ](https://github.com/nextcloud/nextcloudpi/commit/15587e8) (2017-12-28) added nc-snapshot-auto [v0.43.3 ](https://github.com/nextcloud/nextcloudpi/commit/f6ab161) (2017-12-26) nc-datadir: use clone on btrfs systems diff --git a/etc/nextcloudpi-config.d/nc-backup-auto.sh b/etc/nextcloudpi-config.d/nc-backup-auto.sh index a53d36f1..5c1ebcdd 100644 --- a/etc/nextcloudpi-config.d/nc-backup-auto.sh +++ b/etc/nextcloudpi-config.d/nc-backup-auto.sh @@ -15,14 +15,13 @@ ACTIVE_=no -DESTDIR_=/media/USBdrive +DESTDIR_=/media/USBdrive/ncp-backups INCLUDEDATA_=no +COMPRESS_=no BACKUPDAYS_=7 BACKUPLIMIT_=4 DESCRIPTION="Periodic backups" -BASEDIR=/var/www - install() { cat > /etc/systemd/system/nc-backup.service <<EOF @@ -31,7 +30,7 @@ Description=Backup Nextcloud instance [Service] Type=simple -ExecStart=/usr/local/bin/ncp-backup +ExecStart=/usr/local/bin/ncp-backup-auto [Install] WantedBy=default.target @@ -47,77 +46,13 @@ configure() return 0 } - cat > /usr/local/bin/ncp-backup <<EOF + cat > /usr/local/bin/ncp-backup-auto <<EOF #!/bin/bash -DESTFILE=$DESTDIR_/nextcloud-bkp_\`date +"%Y%m%d"\`.tar -DBBACKUP=nextcloud-sqlbkp_\`date +"%Y%m%d"\`.bak - -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; -} - -echo -e "check free space..." -local SIZE=\$( du -s "\$DATADIR" | awk '{ print \$1 }' ) -local FREE=\$( df "\$DATADIR" | tail -1 | awk '{ print \$4 }' ) - -[ \$SIZE -ge \$FREE ] && { - echo -e "free space check failed. Need \$( du -sh "\$DATADIR" | awk '{ print \$1 }' )"; - return 1; -} - -cd $BASEDIR/nextcloud -sudo -u www-data php occ maintenance:mode --on - -# delete older backups -[[ $BACKUPLIMIT_ != 0 ]] && { - NUMBKPS=\$( ls "$DESTDIR_"/nextcloud-bkp_* 2>/dev/null | wc -l ) - [[ \$NUMBKPS -ge $BACKUPLIMIT_ ]] && \ - ls -t $DESTDIR_/nextcloud-bkp_* | tail -\$(( NUMBKPS - $BACKUPLIMIT_ + 1 )) | while read f; do - echo -e "clean up old backup \$f" - rm \$f - done -} - -# database -cd $BASEDIR -echo -e "backup database..." -mysqldump -u root --single-transaction nextcloud > \$DBBACKUP - -# files -[[ "$INCLUDEDATA_" == "yes" ]] && echo -e "backup datadir... " -echo -e "backup files..." -mkdir -p $DESTDIR_ -tar --exclude "nextcloud/data/*/files/*" \ - --exclude "nextcloud/data/.opcache" \ - --exclude "nextcloud/data/{access,error,nextcloud}.log" \ - --exclude "nextcloud/data/access.log" \ - -cf "\$DESTFILE" "\$DBBACKUP" nextcloud/ \ - || { - echo -e "error generating backup" - sudo -u www-data php "$BASEDIR"/nextcloud/occ maintenance:mode --off - return 1 - } -rm "\$DBBACKUP" - -[[ "$INCLUDEDATA_" == "yes" ]] && { - echo -e "backup data files..." - tar --exclude "data/.opcache" \ - --exclude "data/{access,error,nextcloud}.log" \ - --exclude "data/access.log" \ - -rf "\$DESTFILE" -C "\$DATADIR"/.. "\$( basename "\$DATADIR" )" \ - || { - echo -e "error generating backup" - sudo -u www-data php "$BASEDIR"/nextcloud/occ maintenance:mode --off - return 1 - } -} -echo -e "backup \$DESTFILE generated" - -cd $BASEDIR/nextcloud -sudo -u www-data php occ maintenance:mode --off +sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on +ncp-backup "$DESTDIR_" "$INCLUDEDATA_" "$COMPRESS_" "$BACKUPLIMIT_" +sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off EOF - chmod +x /usr/local/bin/ncp-backup + chmod +x /usr/local/bin/ncp-backup-auto cat > /etc/systemd/system/nc-backup.timer <<EOF [Unit] diff --git a/etc/nextcloudpi-config.d/nc-backup.sh b/etc/nextcloudpi-config.d/nc-backup.sh index 9cc86dd9..548fe03a 100644 --- a/etc/nextcloudpi-config.d/nc-backup.sh +++ b/etc/nextcloudpi-config.d/nc-backup.sh @@ -14,82 +14,104 @@ # -DESTDIR_=/media/USBdrive +DESTDIR_=/media/USBdrive/ncp-backups INCLUDEDATA_=no +COMPRESS_=no BACKUPLIMIT_=4 DESCRIPTION="Backup this NC instance to a file" -DESTFILE="$DESTDIR_"/nextcloud-bkp_$( date +"%Y%m%d" ).tar +install() +{ + cat > /usr/local/bin/ncp-backup <<'EOF' +#!/bin/bash + +DESTDIR="$1" +INCLUDEDATA="$2" +COMPRESS="$3" +BACKUPLIMIT="$4" + +DESTFILE="$DESTDIR"/nextcloud-bkp_$( date +"%Y%m%d" ).tar DBBACKUP=nextcloud-sqlbkp_$( date +"%Y%m%d" ).bak BASEDIR=/var/www +DATADIR=$( cd "$BASEDIR"/nextcloud; sudo -u www-data php occ config:system:get datadirectory ) || { + echo "Error reading data directory. Is NextCloud running and configured?"; + exit 1; +} + +echo "check free space..." +SIZE=$( du -s "$DATADIR" | awk '{ print $1 }' ) +FREE=$( df "$DATADIR" | tail -1 | awk '{ print $4 }' ) + +[ $SIZE -ge $FREE ] && { + echo "free space check failed. Need $( du -sh "$DATADIR" | awk '{ print $1 }' )"; + exit 1; +} + +# delete older backups +[[ $BACKUPLIMIT != 0 ]] && { + NUMBKPS=$( ls "$DESTDIR"/nextcloud-bkp_* 2>/dev/null | wc -l ) + [[ $NUMBKPS -ge $BACKUPLIMIT ]] && \ + ls -t $DESTDIR/nextcloud-bkp_* | tail -$(( NUMBKPS - BACKUPLIMIT + 1 )) | while read -r f; do + echo "clean up old backup $f" + rm "$f" + done +} + +# database +cd "$BASEDIR" || exit 1 +echo "backup database..." +mysqldump -u root --single-transaction nextcloud > "$DBBACKUP" + +# files +echo "backup base files..." +mkdir -p "$DESTDIR" +tar --exclude "nextcloud/data/*/files/*" \ + --exclude "nextcloud/data/.opcache" \ + --exclude "nextcloud/data/{access,error,nextcloud}.log" \ + --exclude "nextcloud/data/access.log" \ + -cf "$DESTFILE" "$DBBACKUP" nextcloud/ \ + || { + echo "error generating backup" + exit 1 + } +rm "$DBBACKUP" + +[[ "$INCLUDEDATA" == "yes" ]] && { + echo "backup data files..." + tar --exclude "data/.opcache" \ + --exclude "data/{access,error,nextcloud}.log" \ + --exclude "data/access.log" \ + -rf "$DESTFILE" -C "$DATADIR"/.. "$( basename "$DATADIR" )" \ + || { + echo "error generating backup" + exit 1 + } +} + +[[ "$COMPRESS" == "yes" ]] && { + echo "compressing backup file..." + tar -czf "${DESTFILE}.gz" -C "$( dirname "$DESTFILE" )" "$( basename "$DESTFILE" )" + rm "$DESTFILE" + DESTFILE="${DESTFILE}.gz" +} + +echo "backup $DESTFILE generated" +EOF + chmod +x /usr/local/bin/ncp-backup +} + 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; - } - - echo -e "check free space..." - local SIZE=$( du -s "$DATADIR" | awk '{ print $1 }' ) - local FREE=$( df "$DATADIR" | tail -1 | awk '{ print $4 }' ) - - [ $SIZE -ge $FREE ] && { - echo -e "free space check failed. Need $( du -sh "$DATADIR" | awk '{ print $1 }' )"; - return 1; - } - - sudo -u www-data php "$BASEDIR"/nextcloud/occ maintenance:mode --on - - # delete older backups - [[ $BACKUPLIMIT_ != 0 ]] && { - local NUMBKPS=$( ls "$DESTDIR_"/nextcloud-bkp_* 2>/dev/null | wc -l ) - [[ $NUMBKPS -ge $BACKUPLIMIT_ ]] && \ - ls -t $DESTDIR_/nextcloud-bkp_* | tail -$(( NUMBKPS - BACKUPLIMIT_ + 1 )) | while read -r f; do - echo -e "clean up old backup $f" - rm "$f" - done - } - - # database - cd "$BASEDIR" || return 1 - echo -e "backup database..." - mysqldump -u root --single-transaction nextcloud > "$DBBACKUP" - - # files - echo -e "backup base files..." - mkdir -p "$DESTDIR_" - tar --exclude "nextcloud/data/*/files/*" \ - --exclude "nextcloud/data/.opcache" \ - --exclude "nextcloud/data/{access,error,nextcloud}.log" \ - --exclude "nextcloud/data/access.log" \ - -cf "$DESTFILE" "$DBBACKUP" nextcloud/ \ - || { - echo -e "error generating backup" - sudo -u www-data php "$BASEDIR"/nextcloud/occ maintenance:mode --off - return 1 - } - rm "$DBBACKUP" - - [[ "$INCLUDEDATA_" == "yes" ]] && { - echo -e "backup data files..." - tar --exclude "data/.opcache" \ - --exclude "data/{access,error,nextcloud}.log" \ - --exclude "data/access.log" \ - -rf "$DESTFILE" -C "$DATADIR"/.. "$( basename "$DATADIR" )" \ - || { - echo -e "error generating backup" - sudo -u www-data php "$BASEDIR"/nextcloud/occ maintenance:mode --off - return 1 - } - } - echo -e "backup $DESTFILE generated" - - sudo -u www-data php "$BASEDIR"/nextcloud/occ maintenance:mode --off -} + sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on + + ncp-backup "$DESTDIR_" "$INCLUDEDATA_" "$COMPRESS_" "$BACKUPLIMIT_" + local RET=$? -install() { :; } + sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off + + return $RET +} # License # @@ -200,6 +200,12 @@ EOF wget https://raw.githubusercontent.com/nachoparker/btrfs-snp/master/btrfs-snp -O /usr/local/bin/btrfs-snp chmod +x /usr/local/bin/btrfs-snp } + + # update ncp-backup + cd /usr/local/etc/nextcloudpi-config.d &>/dev/null + install_script nc-backup.sh + cd - &>/dev/null + } # end - only live updates exit 0 |