Skip to content

Commit

Permalink
nc-backup: compress backups and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nachoparker committed Jan 4, 2018
1 parent a1a2f51 commit 54631e2
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 141 deletions.
12 changes: 10 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
81 changes: 8 additions & 73 deletions etc/nextcloudpi-config.d/nc-backup-auto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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]
Expand Down
154 changes: 88 additions & 66 deletions etc/nextcloudpi-config.d/nc-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
6 changes: 6 additions & 0 deletions update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 54631e2

Please sign in to comment.