Encryption for Nextcloud Backups

Hello im new here and hope to find help for my problem.
I have nextcloud installed and everything is running perfectly.
Just one problem when it comes to Backups.
I want to make encypted backups and upload them to another server via SFTP/rsync or WebDav.
Looking for backup scripts i just found normal ones which don´t Encrypt the data.
I hope someone can help me or even provide me a simple backup script with Encryption.

Thanks for your help.

kind regards
Dominik

Have a look at restic

some examples you find with the search function of this board: “nippes backup restic”

1 Like

Danke für deinen Tipp. Ich habe es mit Restic versucht. Ich bekomme ein Verzeichnis Backup per SFTP oder Local hin. Die Datenbank muss ja auch gesichert werden was ich per mysqldump versuche wie in den Restic Docs gezeigt " Reading data from stdin" leider funktionier dies überjaupüt nicht. Es lässt sich kein sump per SFTP B2 oder Local erstellen. Dies ist mein Befehl

mysqldump --single-transaction -h localhost -u nextclouduser -pXYZ nextcloud | restic -r b2:ncdb-backup:db backup --stdin  

Bekommen aber immer nur diese antwort:

reading repository password from stdin
Fatal: wrong password or no key found
mysqldump: Got errno 32 on write

Wo liegt mein fehler?
Die Repos sind alle erstellt worden Local / B2 / SFTP und funktioniern auch alle bei normalen Verzeichnis Backups, sobald die stdin funktion dazu kommt funktioniert nichts mehr.
Einen normalen mysqldump ohne “| restic -r b2:ncdb-backup:db backup --stdin” funktionier auch.
Ich bin etwas überfragt was das Problem angeht.

ich mach das so nicht. ich schreibe den dump, sind ja meist nur ein paar mb, erst irgendwo auf die platte (/tmp,/var/tmp, o.ä) und sicher dann alles (dump, data, config) auf einmal. dann ist das immer in einer sicherung. die sicherung hat dann eine “id”. dann kommt man beim full restore nicht versehentlich durcheinander. für mich ist das nur wegen der “optik”.

hast du das restic password als env-variable gesetzt?

1 Like

Es hat jetzt alles so funktionier, keine ahnung warum aber wenn man das ganze in einem Bash skript ausführt klappt alles und es gibt keine fehler.
Aber dein Vorschlag ist auch gut und sehe das es so noch übersichtlicher werden kann.
Wenn ich das Skript ein mal am Tag laufen lasse kann ich ja anhand der der Zeitspempel sehen welche Backups zusammen gehören. Sonst dachte ich mir ich könnte mit Tags arbeiten. Kann man in Tags Variablen setzen? Wenn ja könnte man die zusammengehörenden Snapshots immer mit einem Gleichnamigen Tag makieren.
Nochmal vielen dank für deine Hilfe.

Skript:

# set nextcloud to maintenance mode
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on
export B2_ACCOUNT_ID=<MY_APPLICATION_KEY_ID>
export B2_ACCOUNT_KEY=<MY_APPLICATION_KEY>
export RESTIC_PASSWORD=<MY_REPO_PASSWORD>     
# mysqldump
mysqldump --single-transaction -h localhost -u nextclouduser -p<MY_DB_PASSWORD> nextcloud | restic -r b2:ncdb-backup:db  backup --tag SQL-Dump --stdin --stdin-filename db_mysql_nextcloud.s>

# backup www directory
restic -r b2:ncdb-backup:db --tag www-Backup --verbose backup /var/www/nextcloud/

# backup data directory 
restic -r b2:ncdb-backup:db --tag Data-Backup --verbose backup /home/data/      

# turn maintenance mode off
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off

# delete trap
trap "" EXIT

bitte auch das kleingedruckte in der restic doku lesen. :nerd_face:

wenn restic mal hängenbleibt (netzwackler) kann es sein, dass beim nächsten backup das repo als geöffnet angezeigt wird. dann muss man da erstmal aufräumen. (kaputt gegangen ist mir bisher noch nix dadurch.) auch muss man ab an an mal ein housekeeping machen.

keine angst: restic ist rock solid.

p.s.: restore üben. :wink: backup ist nämlich total überflüssig. was man braucht ist ein funktionierendes restore. :sunglasses:

1 Like

@DWenninger
Du kannst deine verschlüsselten Backups auch anders machen. So kannst du z. B. Tools wie 7z / 7za inkl. Passwort verwenden und dann das Archiv auf einen unsicheren Speicher hochladen. Das Kommandozeilen-Tool 7z ist unter Linux vielleicht etwas gewöhnungsbedürftig. Aber wenn es erst mal funktioniert, ist es toll. Fraglich ist eher die Dateigröße des ZIP-Archivs.

https://kifarunix.com/how-to-install-and-use-7zip-on-ubuntu-18-04-command-line/

@devnull man kann bäume auch mit einem faustkeil fällen. es hat aber einen grund, warum man das heute nicht mehr so macht. :wink:

1 Like

Das stimmt. Ich nehme meine Lösung zurück.

Restore funktioniert!
Nur leider bekomme ich beim ausführen des Skriptes:

#!/bin/bash
set -e
set -u
set -o pipefail

export B2_ACCOUNT_ID=
export B2_ACCOUNT_KEY=
export RESTIC_PASSWORD=
export RESTIC_REPOSITORY=b2:we-nc-backup:backup

# set nextcloud to maintenance mode
echo "Set maintenance mode on"
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on
# just keep the 2 latest snapshots
echo "Lifecycle management"
restic forget --keep-last 0 --option b2.connections=50

# generate new Index
echo "Generate new Index"
restic prune --option b2.connections=50

# Check Repo
echo "Check Repo"
restic check --option b2.connections=50

# mysqldump
echo "Create Mysqldump"
mysqldump --single-transaction -h localhost -u nextclouduser -pXYZ nextcloud | restic backup --tag SQL-Dump --stdin --stdin-filename db_mysql_nextcloud.sql --option b2.connect>

# backup www directory
echo "Backup www directory"
restic --tag www-Backup --verbose backup /var/www/nextcloud/ --option b2.connections=50

# backup data directory
echo "Backup Data directory"
restic --tag Data-Backup --verbose backup /home/data/ --option b2.connections=50

# turn maintenance mode off
echo "maintenance mode off"
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off

#show snapshots
echo "Current Snapshots"
restic snapshots

immer wieder so einen Fehler: “Remove(<lock/e0b0405c86>)”

Backup Data directory
open repository
repository bbb5f310 opened successfully, password is correct
lock repository
load index files
start scan on [/home/data/]
start backup on [/home/data/]
scan finished in 11.523s: 94 files, 26.658 MiB

Files:          94 new,     0 changed,     0 unmodified
Dirs:          117 new,     0 changed,     0 unmodified
Data Blobs:     49 new
Tree Blobs:    100 new
Added to the repo: 4.846 MiB

processed 94 files, 26.658 MiB in 0:35
snapshot beef6355 saved
Remove(<lock/e0b0405c86>) returned error, retrying after 396.550928ms: Delete: b2_delete_file_version: 400: File not present: backup/locks/e0b0405c86de6762a3f9abcb649ecd7f59ed1ec2b696ef73655ebff0e9050358 4_z4fb1e34eb7773b1a79cb0310_f104749d3ab4d01c6_d20210929_m133146_c000_v0001401_t0041
Remove(<lock/e0b0405c86>) returned error, retrying after 527.390157ms: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 968.480344ms: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 1.806761153s: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 3.448806451s: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 3.011355647s: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 4.539634288s: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 10.7006923s: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 15.335301653s: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 12.654175283s: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 289.726811ms: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 821.106448ms: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 629.010732ms: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 2.011541491s: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 2.028854286s: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 2.556307748s: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 5.929389018s: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 7.886460409s: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 10.02194025s: Delete: b2_download_file_by_name: 404: File with such name does not exist.
Remove(<lock/e0b0405c86>) returned error, retrying after 26.5363577s: Delete: b2_download_file_by_name: 404: File with such name does not exist.
error in cleanup handler: Delete: b2_download_file_by_name: 404: File with such name does not exist.
maintenance mode off
Maintenance mode disabled
Current Snapshots
repository bbb5f310 opened successfully, password is correct
ID        Time                 Host        Tags         Paths
-------------------------------------------------------------------------------
24163d7e  2021-09-29 15:24:10  nc          SQL-Dump     /db_mysql_nextcloud.sql
bae19ba6  2021-09-29 15:24:48  nc          www-Backup   /var/www/nextcloud
beef6355  2021-09-29 15:31:35  nc          Data-Backup  /home/data
-------------------------------------------------------------------------------
3 snapshots

Ich wieß diesbezüglich nicht mehr weiter. Lösungsvorschläge aus änlichen fällen habe ich versucht, leider ohne erfolg. Das Frustrierende ist das der Fehler nicht Reproduzierbar ist und auch mal an anderen stellen auftreten kann.
Könntest du mir vielleicht mal dein Backup script zeigen vielleicht komme ich so auf ein funktionierendes ergebnis.

das ist mehr eine frage für’s restic forum. e.g. Restic prune fails to remove lock - Getting Help - restic forum

das skript ist korrect. ich würde aber forget/prune/check nach dem beenden des maintenance mode ausführen.