Last active
September 18, 2024 17:13
-
-
Save computercam/15d5badbaf7b966d9eb6f4df934284b7 to your computer and use it in GitHub Desktop.
docker nextcloud backup / restore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 . |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maintenance mode fails, I replaced rows 24 and 29 this:
I have also this error:
My volume is /var/lib/docker/volumes/nextcloud/appdata:/var/www/html/appdata