Skip to content

Commit

Permalink
nc-backup: compress in place and exclude previews folder
Browse files Browse the repository at this point in the history
  • Loading branch information
nachoparker committed Jan 16, 2019
1 parent 0b8252b commit 2419e57
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 66 deletions.
103 changes: 50 additions & 53 deletions bin/ncp/BACKUPS/nc-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,90 +13,87 @@ install()
#!/bin/bash
set -eE
DESTDIR="${1:-/media/USBdrive/ncp-backups}"
INCLUDEDATA="${2:-no}"
COMPRESS="${3:-no}"
BACKUPLIMIT="${4:-0}"
destdir="${1:-/media/USBdrive/ncp-backups}"
includedata="${2:-no}"
compress="${3:-no}"
backuplimit="${4:-0}"
DESTFILE="$DESTDIR"/nextcloud-bkp_$( date +"%Y%m%d_%s" ).tar
DBBACKUP=nextcloud-sqlbkp_$( date +"%Y%m%d" ).bak
OCC="sudo -u www-data php /var/www/nextcloud/occ"
destfile="$destdir"/nextcloud-bkp_$( date +"%Y%m%d_%s" ).tar
dbbackup=nextcloud-sqlbkp_$( date +"%Y%m%d" ).bak
occ="sudo -u www-data php /var/www/nextcloud/occ"
[[ -f /.docker-image ]] && basedir=/data || basedir=/var/www
DATADIR=$( $OCC config:system:get datadirectory ) || {
[[ "$compress" == "yes" ]] && destfile="$destfile".gz
datadir=$( $occ config:system:get datadirectory ) || {
echo "Error reading data directory. Is NextCloud running and configured?";
exit 1;
}
cleanup(){ local RET=$?; rm -f "${DBBACKUP}" ;[[ -f /.docker-image ]] && mv /data/nextcloud /data/app; $OCC maintenance:mode --off; exit $RET; }
fail() { local RET=$?; echo "Abort..." ; rm -f "${DBBACKUP}" "${DESTFILE}";[[ -f /.docker-image ]] && mv /data/nextcloud /data/app; $OCC maintenance:mode --off; exit $RET; }
cleanup(){ local ret=$?; rm -f "${dbbackup}" ; $occ maintenance:mode --off; exit $ret; }
fail() { local ret=$?; echo "Abort..." ; rm -f "${dbbackup}" "${destfile}"; $occ maintenance:mode --off; exit $ret; }
trap cleanup EXIT
trap fail INT TERM HUP ERR
echo "check free space..." # allow at least ~500 MiB for backups without data
mkdir -p "$DESTDIR"
[[ "$INCLUDEDATA" == "yes" ]] && SIZE=$( du -s "$DATADIR" | awk '{ print $1 }' ) || SIZE=500000
FREE=$( df "$DESTDIR" | tail -1 | awk '{ print $4 }' )
echo "check free space..." # allow at least ~100 extra MiB
mkdir -p "$destdir"
[[ "$includedata" == "yes" ]] && \
dsize=$(du -s "$datadir" | awk '{ print $1 }')
nsize=$(du -s "$basedir/nextcloud" | awk '{ print $1 }')
size=$((nsize + dsize + 100*1024))
free=$( df "$destdir" | tail -1 | awk '{ print $4 }' )
[ $SIZE -ge $FREE ] && {
echo "free space check failed. Need $SIZE Bytes";
exit 1;
[ $size -ge $free ] && {
echo "free space check failed. Need $size Bytes";
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
[[ $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
$OCC maintenance:mode --on
[[ -f /.docker-image ]] && mv /data/app /data/nextcloud && DATADIR=/data/nextcloud/data
[[ -f /.docker-image ]] && BASEDIR=/data || BASEDIR=/var/www
cd "$BASEDIR" || exit 1
$occ maintenance:mode --on
cd "$basedir" || exit 1
echo "backup database..."
mysqldump -u root --single-transaction nextcloud > "$DBBACKUP"
mysqldump -u root --single-transaction nextcloud > "$dbbackup"
# files
echo "backup base files..."
mkdir -p "$DESTDIR"
tar --exclude "nextcloud/data/*/files/*" \
echo "backup files..."
[[ "$includedata" == "yes" ]] && data="$(basename "$datadir")"
[[ "$compress" == "yes" ]] && z=z
mkdir -p "$destdir"
tar -c${z}f "$destfile" \
\
"$dbbackup" \
\
--exclude "$data/.opcache" \
--exclude "$data/{access,error,nextcloud}.log" \
--exclude "$data/access.log" \
--exclude "$data/ncp-update-backups/" \
-C "$(dirname "$datadir"/)" $data \
\
--exclude "nextcloud/data/*/files/*" \
--exclude "nextcloud/data/.opcache" \
--exclude "nextcloud/data/{access,error,nextcloud}.log" \
--exclude "nextcloud/data/access.log" \
--exclude "nextcloud/data/appdata_*/previews/*" \
--exclude "nextcloud/data/ncp-update-backups/" \
-cf "$DESTFILE" "$DBBACKUP" "nextcloud"/ \
-C $basedir 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" \
--exclude "data/ncp-update-backups/" \
-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"
}
chmod 600 "$DESTFILE"
rm "$dbbackup"
chmod 600 "$destfile"
echo "backup $DESTFILE generated"
echo "backup $destfile generated"
EOF
chmod +x /usr/local/bin/ncp-backup
}
Expand Down
19 changes: 8 additions & 11 deletions bin/ncp/BACKUPS/nc-restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ PHPVER=7.2
DIR="$( cd "$( dirname "$BACKUPFILE" )" &>/dev/null && pwd )" #abspath
[[ -f /.docker-image ]] && NCDIR=/data/app || NCDIR=/var/www/nextcloud
[[ -f /.docker-image ]] && NCDIR=/data/nextcloud || NCDIR=/var/www/nextcloud
[[ $# -eq 0 ]] && { echo "missing first argument" ; exit 1; }
[[ -f "$BACKUPFILE" ]] || { echo "$BACKUPFILE not found" ; exit 1; }
Expand All @@ -38,16 +38,10 @@ trap cleanup INT TERM HUP ERR EXIT
rm -rf "$TMPDIR" && mkdir -p "$TMPDIR"
# EXTRACT FILES
[[ "$BACKUPFILE" =~ ".tar.gz" ]] && COMPRESSED=1 || COMPRESSED=0
[[ "$COMPRESSED" == "1" ]] && {
echo "decompressing backup file $BACKUPFILE..."
tar -xzf "$BACKUPFILE" -C "$TMPDIR" || exit 1
BACKUPFILE="$( ls "$TMPDIR"/*.tar 2>/dev/null )"
[[ -f "$BACKUPFILE" ]] || { echo "$BACKUPFILE not found"; exit 1; }
}
echo "extracting backup file $BACKUPFILE..."
tar -xf "$BACKUPFILE" -C "$TMPDIR" || exit 1
[[ "$BACKUPFILE" =~ ".tar.gz" ]] && z=z
tar -x${z}f "$BACKUPFILE" -C "$TMPDIR" || exit 1
## SANITY CHECKS
[[ -d "$TMPDIR"/nextcloud ]] && [[ -f "$( ls "$TMPDIR"/nextcloud-sqlbkp_*.bak 2>/dev/null )" ]] || {
Expand Down Expand Up @@ -92,7 +86,7 @@ cd "$NCDIR"
### INCLUDEDATA=yes situation
NUMFILES=$(( 2 + COMPRESSED ))
NUMFILES=2
if [[ $( ls "$TMPDIR" | wc -l ) -eq $NUMFILES ]]; then
DATADIR=$( grep datadirectory "$NCDIR"/config/config.php | awk '{ print $3 }' | grep -oP "[^']*[^']" | head -1 )
Expand Down Expand Up @@ -152,6 +146,9 @@ bash /usr/local/bin/nextcloud-domain.sh
# update the systems data-fingerprint
sudo -u www-data php occ maintenance:data-fingerprint
# refresh thumbnails
sudo -u www-data php occ files:scan-app-data
# restart PHP if needed
[[ "$NEED_RESTART" == "1" ]] && \
bash -c " sleep 3; service php${PHPVER}-fpm restart" &>/dev/null &
Expand Down
6 changes: 4 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

[v1.3.9](https://github.com/nextcloud/nextcloudpi/commit/983456b) (2019-01-15) build: add exfat utils for external storage
[v1.3.10](https://github.com/nextcloud/nextcloudpi/commit/b9fd429) (2019-01-15) nc-backup: compress in place and exclude previews folder

[v1.3.8](https://github.com/nextcloud/nextcloudpi/commit/193d89b) (2019-01-14) nc-datadir: fix fail2ban logpath
[v1.3.9](https://github.com/nextcloud/nextcloudpi/commit/0b8252b) (2019-01-15) build: add exfat utils for external storage

[v1.3.8 ](https://github.com/nextcloud/nextcloudpi/commit/193d89b) (2019-01-14) nc-datadir: fix fail2ban logpath

[v1.3.7 ](https://github.com/nextcloud/nextcloudpi/commit/2ac9b8b) (2019-01-14) ncp-web: allow private IPv6 addresses

Expand Down
13 changes: 13 additions & 0 deletions update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ cp -r ncp-app /var/www/

# docker images only
[[ -f /.docker-image ]] && {
[[ -d /data/app ]] && {
[[ -d /data/nextcloud ]] && mv /data/nextcloud /data/nextcloud-old
mv /data/app /data/nextcloud && \
rm -f /var/www/nextcloud && \
ln -s /data/nextcloud /var/www/nextcloud
}
:
}

Expand Down Expand Up @@ -239,6 +245,13 @@ cp -r ncp-app /var/www/
</Directory>
EOF

# re-enable automount
is_active_app nc-automount && run_app nc-automount

# update nc-backup
install_app nc-backup
install_app nc-restore

# remove redundant opcache configuration. Leave until update bug is fixed -> https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=815968
# Bug #416 reappeared after we moved to php7.2 and debian buster packages. (keep last)
[[ "$( ls -l /etc/php/7.2/fpm/conf.d/*-opcache.ini | wc -l )" -gt 1 ]] && rm "$( ls /etc/php/7.2/fpm/conf.d/*-opcache.ini | tail -1 )"
Expand Down

0 comments on commit 2419e57

Please sign in to comment.