Skip to content

Instantly share code, notes, and snippets.

@sinbadsalmon
Forked from Refhi/nextcloud-snap-backup.sh
Last active February 16, 2019 14:07
Show Gist options
  • Save sinbadsalmon/d1beb912657c096048238539073887d9 to your computer and use it in GitHub Desktop.
Save sinbadsalmon/d1beb912657c096048238539073887d9 to your computer and use it in GitHub Desktop.
Nextcloud 13 snap backup script - A fork of the original script. This script creates an encrypted archive of the backup and then uploads it to an FTP server of your choosing. I use gpg with keys, see the gpg man pages for information on using this. FTP elements of script inspired by https://gist.github.com/pixeline/0f9f922cffb5a6bba97a. All othe…
#!/bin/bash
user=youruser
backups=/your/chosen/backup/directory
# FTP LOGIN
FTPHOST='ftp://host.domain.something'
FTPUSER='username'
FTPPASSWORD='password'
# DISTANT DIRECTORY
FTPTARGET_DIR='/your/target/directory'
# LOCAL DIRECTORY
FTPSOURCE_DIR='/your/source/directory'
if [[ $EUID > 0 ]]
then echo "Please run as root"
exit
fi
# maintenance mode on
nextcloud.occ maintenance:mode --on
snap restart nextcloud.php-fpm # nextcloud 13 snap version require this...
# if there is no backups directory then create it
if [ ! -d $backups ]
then
mkdir $backups
chown $user:$user $backups
fi
# Directory for backup (with timestamping)
bakdir=$backups/backup-$(date +%Y%m%d)
# if there is no backups directory (with timestamping) then create it
if [ ! -d $bakdir ]
then
mkdir $backups/backup-$(date +%Y%m%d)
chown $user:$user $bakdir
fi
# config
echo "Backing up config files, app files, database files..."
rsync -Aax /var/snap/nextcloud/current/ $bakdir/current/
# database
echo "Backing up database..."
nextcloud.mysqldump > $bakdir/sqlbkp.bak
# nextcloud-data
echo "Backing up data... without previews"
rsync -Aax --exclude=/appdata_*/preview /your/nextcloud/data/directory/ $bakdir/nextcloud-data/
# owner user:user
chown -R $user:$user $bakdir
# maintenance mode off
nextcloud.occ maintenance:mode --off
snap restart nextcloud.php-fpm # nextcloud 13 snap version require this...
# compress $bakdir
tar -cjf $backups/Nextcloud_backup-$(date +%Y%m%d).tgz.pgp $bakdir | gpg -v -e -r 1A2B3C4D5E6F
echo "Creation of Backup Archive finished."
# delete $bakdir
echo "Cleaning up backups directory..."
rm -rf $bakdir
echo "Starting transfer of archive..."
#transfer
lftp -u "$FTPUSER","$FTPPASSWORD" $FTPHOST <<EOF
echo "Starting upload to $FTPHOST $FTPTARGET_DIR from $FTPSOURCE_DIR"
mirror -R --parallel=3 $FTPSOURCE_DIR $FTPTARGET_DIR
echo "Backup finished"
echo "When restoring, don't forget to set the correct permissions."
exit
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment