nextcloud / nextcloud/nextcloudpi

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

Open
#1,830 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Shell
Stars
2.9k
Forks
318
PR merge metrics
No merged PRs in 30d

Description

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:

  • I installed NextCloudPi_RaspberryPi_v1.52.2.zip on a RaspberryPi 4 8Gb.
  • Inserted a usb flash drive and formated it via the web interface's nc-format-USB page with a label of "myCloudDrive"
  • Moved the nextcloud data dir to "/media/USBdrive/ncdata" (the default) via the web interface's nc-datadir page.
    This made a symlink of "/media/USBdrive" pointing to "/media/myCloudDrive"

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

  • Moved NextCloud's data dir via web interface's nc-datadir to "/media/USBdrive/ncdata"
  • Moved MariaDB's datadir via web interface's nc-database to "/media/USBdrive/ncdatabase" Warning this has nothing to do with the issue, but just sharing all steps.
  • Uploaded a few files in nextcloud
  • Created a BTRFS snapshot via web interface's nc-snapshot. This created a snapshot "manual_2023-09-22_215641" in "/media/myCloudDrive/ncp-snapshots"

root@nextcloudpi:/media/myCloudDrive/ncp-snapshots# btrfs subvolume show /media/myCloudDrive/ncp-snapshots/manual_2023-09-22_215641
ncp-snapshots/manual_2023-09-22_215641
Name: manual_2023-09-22_215641
UUID: ec88b6d6-02c8-7d40-b036-38bb4f598407
Parent UUID: 7ca578b9-c2b7-9b48-a88f-9323daf7e379
Received UUID: -
Creation time: 2023-09-22 21:56:41 +0100
Subvolume ID: 259
Generation: 192
Gen at creation: 105
Parent ID: 5
Top level ID: 5
Flags: readonly
Snapshot(s):
ncdata

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

  • Then deleted all files from nextcloud and proceeded with restoring from the manual snapshot.
  • If you open the web interface and try "nc-restore-snapshot" with a snapshot path of "/media/myCloudDrive/ncp-snapshots/manual_2023-09-22_215641"
    The nc-restore-snapshot.sh script will fail with "ERROR: Not a Btrfs subvolume: Invalid argument"

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read bin/ncp/BACKUPS/nc-restore-snapshot.sh, focusing on the datadir and mountpoint discovery at lines 19 and 25, automatic snapshot handling at line 37, and restore operations at lines 40-41. Reproduce the Btrfs setup described in the issue and run nc-restore-snapshot against manual and automatic snapshots; done means both restore without the reported error.

Written by the indexing model from the issue text.

Assessment

Tech stack
shell
Domain
cli, infrastructure
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.