nextcloud / nextcloudpi

📦 Build code for NextcloudPi: Raspberry Pi, Odroid, Rock64, curl installer...
https://nextcloudpi.com
2.49k stars 295 forks source link

Restoring from BTRFS snapshot results in "ERROR: Not a Btrfs subvolume: Invalid argument" #1830

Open Dimitar-Boychev opened 11 months ago

Dimitar-Boychev commented 11 months ago

Hello, I think that there is an error in the nc-restore-snapshot.sh script. This script tries to delete the data directory, not the mount point on witch the data directory lives. The issue is pretty much the same as #1508

Steps to reproduce:

root@nextcloudpi:/media# ls -la total 24 drwxr-xr-x 3 root root 4096 Sep 22 22:32 . drwxr-xr-x 18 root root 4096 Sep 24 11:22 .. drwxr-xr-x 1 root root 58 Sep 24 13:35 myCloudDrive lrwxrwxrwx 1 root root 19 Sep 22 22:32 USBdrive -> /media/myCloudDrive

root@nextcloudpi:/media/myCloudDrive/ncp-snapshots# btrfs subvolume list /media/myCloudDrive ID 259 gen 192 top level 5 path ncp-snapshots/manual_2023-09-22_215641 ID 260 gen 118 top level 5 path ncp-snapshots/autobackup_2023-09-22_220414 ID 261 gen 121 top level 5 path ncp-snapshots/autobackup_2023-09-22_220636 ID 262 gen 126 top level 5 path ncp-snapshots/autobackup_2023-09-22_221342 ID 263 gen 130 top level 5 path ncp-snapshots/autobackup_2023-09-22_221748 ID 264 gen 131 top level 5 path ncp-snapshots/autobackup_2023-09-22_221843 ID 265 gen 132 top level 5 path ncp-snapshots/autobackup_2023-09-22_221903 ID 266 gen 145 top level 5 path ncp-snapshots/autobackup_2023-09-24_111504 ID 267 gen 149 top level 5 path ncp-snapshots/autobackup_2023-09-24_112121 ID 268 gen 156 top level 5 path ncp-snapshots/autobackup_2023-09-24_120049 ID 269 gen 161 top level 5 path ncp-snapshots/autobackup_2023-09-24_121324 ID 270 gen 164 top level 5 path ncp-snapshots/autobackup_2023-09-24_121658 ID 271 gen 169 top level 5 path ncp-snapshots/autobackup_2023-09-24_130026 ID 272 gen 170 top level 5 path ncp-snapshots/autobackup_2023-09-24_130027 ID 273 gen 174 top level 5 path ncp-snapshots/autobackup_2023-09-24_130412 ID 274 gen 177 top level 5 path ncp-snapshots/autobackup_2023-09-24_132620 ID 276 gen 191 top level 5 path ncp-snapshots/autobackup_2023-09-24_133514 ID 277 gen 204 top level 5 path ncdata

This happens because on line 40 https://github.com/nextcloud/nextcloudpi/blob/master/bin/ncp/BACKUPS/nc-restore-snapshot.sh#L40 the "btrfs subvolume delete" is issued against $datadir instead of what I believe to be correct $mountpoint.

On line 19 the script get NextCloud's data dir with a debug of the php script it takes it via: sudo -u www-data php -r 'include("/var/www/nextcloud/config/config.php"); echo($CONFIG["datadirectory"]);'

Same could be done via ncc config:system:get datadirectory

root@nextcloudpi:/media/myCloudDrive/ncp-snapshots# ncc config:system:get datadirectory /media/myCloudDrive/ncdata/data root@nextcloudpi:/media/myCloudDrive/ncp-snapshots# sudo -u www-data php -r 'include("/var/www/nextcloud/config/config.php"); echo($CONFIG["datadirectory"]);' /media/myCloudDrive/ncdata/data root@nextcloudpi:/media/myCloudDrive/ncp-snapshots#

Then on line 25 it proceeds to determine the data dir's mount point and this correctly returns and stores "/media/myCloudDrive/ncdata" inside the mountpoint variable.

root@nextcloudpi:/media/myCloudDrive/ncp-snapshots# stat -c "%m" "/media/myCloudDrive/ncdata/data" /media/myCloudDrive/ncdata

The automatic snapshot generation on line 37 passes correctly. And then the problem comes.

btrfs subvolume delete "$datadir"

Tries to delete "/media/myCloudDrive/ncdata/data" and this is NOT a BTRFS volume and it throws the error. Instead the BTRFS volume is "/media/myCloudDrive/ncdata" that contains a "data" folder inside.

Modifying lines 40 and 41 of the script to state:

btrfs subvolume delete "$mountpoint" || return 1 btrfs subvolume snapshot "$SNAPSHOT" "$mountpoint"

Fixes this problem and now restoring from a manual and automatic snapshots works as expected.