Skip to content

Instantly share code, notes, and snippets.

@computercam
Last active September 18, 2024 17:13
Show Gist options
  • Save computercam/15d5badbaf7b966d9eb6f4df934284b7 to your computer and use it in GitHub Desktop.
Save computercam/15d5badbaf7b966d9eb6f4df934284b7 to your computer and use it in GitHub Desktop.
docker nextcloud backup / restore
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
TIMESTAMP=`date +"%Y-%m-%d-%T"`
BACKUP_DIR="${DIR}/${TIMESTAMP}"
APPDATA_DIR="${DIR}/../appdata"
MYSQL_USER="root"
MYSQL_PASSWORD="abc123"
MYSQL_DB="nextcloud_db"
if [[ ! $UID -eq 0 ]];
then
echo "Please run this script as root"
exit 1
fi
# UNLOCK
chattr -i .
# CREATE BACKUP DIR
mkdir $BACKUP_DIR
# TURN ON MAINTAINENCE MODE
docker exec --user abc nextcloud /config/www/nextcloud/occ maintenance:mode --on
# SQL BACKUP
docker exec nextcloudsql mysqldump --single-transaction --host=localhost --user=${MYSQL_USER} --password=${MYSQL_PASSWORD} ${MYSQL_DB} > "$BACKUP_DIR/${MYSQL_DB}.sql"
# APPDATA BACKUP
rsync --quiet -a -zz "${APPDATA_DIR}" "${BACKUP_DIR}"
# COMPRESS BACKUP
sudo tar -czf "./${TIMESTAMP}.tar.gz" ${BACKUP_DIR}
# REMOVE UNCOMPRESSED DIR
rm -rf $BACKUP_DIR
# TURN OFF MAINTAINENCE MODE
docker exec --user abc nextcloud /config/www/nextcloud/occ maintenance:mode --off
# REMOVE OLD BACKUPS
find . -maxdepth 1 -iname "*.tar.gz" -mtime +30 -exec rm {} \;
# LOCK
chattr +i .
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
BACKUP_FILE="${DIR}/backup.sh"
SQL_FILE="${DIR}/restore.sql"
MYSQL_PASSWORD="abc123"
MYSQL_DB="nextcloud_db"
if [[ ! $UID -eq 0 ]];
then
echo "Please run this script as root"
exit 1
fi
if [[ ! -e $SQL_FILE ]];
then
echo "NOT FOUND: $SQL_FILE"
echo "Please please a restore.sql file in the current directory before running this script."
exit 1
fi
# BACKUP FIRST
echo "Backing up Nextcloud before restoring . . ."
sudo $BACKUP_FILE
# UNLOCK
chattr -i .
# TURN ON MAINTAINENCE MODE
docker exec --user abc nextcloud /config/www/nextcloud/occ maintenance:mode --on
# SQL RESTORE
cat $SQL_FILE | docker exec -i nextcloudsql mysql --password="$MYSQL_PASSWORD" "$MYSQL_DB"
# TURN OFF MAINTAINENCE MODE
docker exec --user abc nextcloud /config/www/nextcloud/occ maintenance:mode --off
# LOCK
chattr +i .
@Tony2k
Copy link

Tony2k commented Jul 9, 2022

Maintenance mode fails, I replaced rows 24 and 29 this:

docker exec --user www-data nextcloud php occ maintenance:mode --on
docker exec --user www-data nextcloud php occ maintenance:mode --off

I have also this error:

rsync: [sender] link_stat "/appdata" failed: No such file or directory (2)`

My volume is /var/lib/docker/volumes/nextcloud/appdata:/var/www/html/appdata

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