Skip to content

Instantly share code, notes, and snippets.

@Refhi
Forked from kapcom01/nextcloud-snap-backup.sh
Last active October 10, 2024 12:14
Show Gist options
  • Save Refhi/d212303dad4d561b1f1aef7a3d86759e to your computer and use it in GitHub Desktop.
Save Refhi/d212303dad4d561b1f1aef7a3d86759e to your computer and use it in GitHub Desktop.
Nextcloud 13 snap backup script
#! /bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
user=refhi
backups=/home/refhi/backups
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
#bakdir=$backups/backup-$(date +%Y%m%d)
bakdir=$backups
# 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 /var/snap/nextcloud/common/nextcloud/data/ $bakdir/nextcloud-data/
# owner manolis:manolis
chown -R $user:$user $bakdir
# maintenance mode off
nextcloud.occ maintenance:mode --off
snap restart nextcloud.php-fpm # nextcloud 13 snap version require this...
echo "Backup finished."
echo "When restoring, don't forget to set the correct permissions."
@Refhi
Copy link
Author

Refhi commented Aug 29, 2018

Tried to make a sturdy backup script for nextcloud 13 snap

It makes 3 main things :

  • save snap current & common
  • save database

For restoration I use this (which does not require a database restoration, but saving the sqlbkp.bak seems safer anyway) :

#!/bin/sh
# restauration :
# 0 - préparation
sudo snap install nextcloud
sudo snap stop nextcloud

# restore data
## si besoin pour que l'user refhi puisse copier les données (plus simple pour rsync)
sudo mkdir /var/snap/nextcloud/common/nextcloud/data/
sudo chmod o+x /var/snap/nextcloud/common/nextcloud/
sudo chown -R refhi:refhi /var/snap/nextcloud/common/nextcloud/data/
## puis (attention à l'exclusion de preview)
rsync -Aax --progress --exclude 'preview' refhi@shuttle:/home/refhi/backups/nextcloud-data/ /var/snap/nextcloud/common/nextcloud/data/
sudo chmod o-x /var/snap/nextcloud/common/nextcloud/
sudo chown -R root:root /var/snap/nextcloud/common/nextcloud/data/

# restore curent & config
sudo chown -R refhi:refhi /var/snap/nextcloud/current/
rsync -Aax --progress refhi@shuttle:/home/refhi/backups/current/ /var/snap/nextcloud/current/
sudo chown -R root:root /var/snap/nextcloud/current/

# start it
sudo snap start nextcloud

@sinbadsalmon
Copy link

Thanks for this, incredibly useful! I've used this as a base and added in creating an encrypted archive of the backup folder. Then I upload that encrypted archive to my backup storage on an ftp server.

I will fork and paste the script in once I've removed all the sensitive information - any comments welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment