Skip to content

Added repair script to upgrade the legacy home storage ids - #7539

Merged
DeepDiver1975 merged 4 commits into
masterfrom
repair-legacystorageid
Aug 22, 2014
Merged

DeepDiver1975 merged 4 commits into
masterfrom
repair-legacystorageid

Conversation

@PVince81

@PVince81 PVince81 commented Mar 4, 2014

Copy link
Copy Markdown
Contributor

I'm tired of these legacy storage id issues when people move their homes, so here is the repair script.
Fixes #6493

I've tested with MySQL:

  • correctly migrates legacy storage ids to home:: based ids
  • doesn't migrate existing home:: other storages
  • doesn't migrate "local::/path/to/datadir/" which I still wonder why it exists, some kind of root storage?

This needs to be tested on all DBs.

TODO:

  • Test on more DBs
  • Add unit tests containing a mix of legacy storage ids, home storage ids and external storage ids

@DeepDiver1975 @icewind1991 @karlitschek

@ghost

ghost commented Mar 4, 2014

Copy link
Copy Markdown

@childnode

Copy link
Copy Markdown

If you can provide me some startup cables, I'll plug it onto my ownCar ;)
=> howto run this repair script manually?

s.th. like

# php lib/private/repair.run.php
<?php
require_once("lib/base.php");
require_once("lib/private/repair.php");

$run = new OC\Repair();
$run->run();

didn't changed anything. Unfortunatly I'm not familar with the Symfony Command used for OC\Core\Command\Maintenance\Repair

@childnode

Copy link
Copy Markdown

without knowing much about the details, it seems more robust to me if comparision is made case insensitive:

                        if (strtolower($currentId) === strtolower($dataDirId)) {
                                continue;
                        }

and if there would be an error / hint if anything failed would be great too:

                        if (($userId = $this->extractUserId($currentId)) != null) {
                                $newId = 'home::' . $userId;
                                // ...
                        } else {
                                $this->emit('\OC\Repair', 'step', array('Failed to upgrade legacy home storage id ' . $currentId));
                        }

@PVince81

PVince81 commented Mar 5, 2014

Copy link
Copy Markdown
Contributor Author

@childnode If you check out this branch you can run the script this way from the command line:
./occ maintenance:repair

The thing is, the repair script architecture design isn't finished yet, so I'm not sure what way we should use to return errors, I was hoping for @icewind1991 to clarify on this since he started implementing the skeleton of the repair scrips.

Also, thanks for your code comments, I'll look into them.

@PVince81

PVince81 commented Mar 5, 2014

Copy link
Copy Markdown
Contributor Author
  • make sure that the script doesn't break anything if another user's home's subdir is mounted

@childnode

Copy link
Copy Markdown

@PVince81 ok, for me, the script doesn't work but crashed with contraints

  [Doctrine\DBAL\DBALException]
  An exception occurred while executing 'UPDATE "oc_storages" SET id = ? WHERE id = ?':

  SQLSTATE[23000]: Integrity constraint violation: 19 column id is not unique

  [PDOException]
  SQLSTATE[23000]: Integrity constraint violation: 19 column id is not unique

iteration stops while trying to add an home::user1 which already exists:

echo-stack

dataDir /srv/owncloud/data/
dataDirId local::/srv/owncloud/data/
currentId local::/srv/owncloud/data/
skipped
currentId local::/srv/owncloud/data/user4/
userId from 'local::/srv/owncloud/data/user4': user4
newId home::user4
currentId local::/srv/owncloud/data/user1/
userId from 'local::/srv/owncloud/data/user1': user1
newId home::user1

for oc_storage

1|local::/srv/owncloud/data/user1/
2|local::/srv/owncloud/data/user2/
3|local::/srv/owncloud/data/
4|google::1/FOOOOOOOBAR1
5|local::/srv/owncloud/data/user3/
6|local::/srv/owncloud/data/user4/
7|google::1/FOOOOOOOBAR2
8|home::user1
9|home::user5

@PVince81

PVince81 commented Mar 5, 2014

Copy link
Copy Markdown
Contributor Author

Right, in your case logging in as those users already created a separate storage home::user id.
I suppose the old ones weren't used because you already moved the data dir ?

I think the migration script needs to be adapted to take such cases into account, maybe delete the old entries (might be dangerous) if new ones exist already, or just leave them as is and skip them (and report the skipped count)

@childnode

Copy link
Copy Markdown

If you can read in my comment on #6493 there are duplicate entries in the oc_filecache table and I suppose the old ones are still used aside the new ones ...

echo 'sqlite3 owncloud.db "select count(*) from oc_filecache where storage in ('{1,2,3,4,5,6,7,8,9}')";'|sh
4313
452
1350
1073
12
3
1538
2939
14

@karlitschek

Copy link
Copy Markdown
Contributor

@PVince81 Do you see a way to fix the duplicate entries at the same time? 😈

@PVince81

PVince81 commented Mar 6, 2014

Copy link
Copy Markdown
Contributor Author

Like I said the old storage entries could be deleted by the script if a new one already exists.
The filecache entries (and potentially share information...) could be deleted as well, if that's ok and not too risky.

@PVince81

PVince81 commented Mar 6, 2014

Copy link
Copy Markdown
Contributor Author
  • Delete legacy home entries if new entries already exist for that user (including matching filecache entries)

@karlitschek

Copy link
Copy Markdown
Contributor

Well I think that the DB is already "broken" if duplicate entries exist. So deleting them has to be done sooner or later anyways

@PVince81

Copy link
Copy Markdown
Contributor Author

Deleting old entries should be possisble as long as the data folder is still the same.
The trouble is when the data folder was moved, in which case the path in the DB isn't the same any more, so we can't know directly whether that "local::" folder is the old home or is an external mount (unless we maybe scan mount.json for all users, which might still be risky)

@PVince81

Copy link
Copy Markdown
Contributor Author

Ok, first, to recreate this situation, here are some test steps:

  1. Install OC 5.0.12 in "/srv/www/htdocs/owncloud5" (the version that didn't have the home:: storages yet)
  2. Create three users user1, user2, user3
  3. Let user1 share a folder with user2
  4. Let user2 reshare the folder with user3
    My storage table looks like this:
+-------------------------------------------------+------------+
| id                                              | numeric_id |
+-------------------------------------------------+------------+
| local::/srv/www/htdocs/owncloud5/data/       |          3 |
| local::/srv/www/htdocs/owncloud5/data/root/  |          1 |
| local::/srv/www/htdocs/owncloud5/data/user1/ |          2 |
| local::/srv/www/htdocs/owncloud5/data/user2/ |          4 |
| local::/srv/www/htdocs/owncloud5/data/user3/ |          5 |
+-------------------------------------------------+------------+
  1. Log out
  2. Extract OC 6.0.2 into "/srv/www/htdocs/owncloud6"
  3. Ajust permissions and copy the "data" and "config" folder from owncloud5 to owncloud6 (the goal is to move the data dir)
  4. Edit config.php and change the data dir to "/srv/www/htdocs/owncloud6/data"
  5. Run the upgrade
  6. Login as user1

Since no home entry was found under the given data dir, a new entry gets created:

+-------------------------------------------------+------------+
| id                                              | numeric_id |
+-------------------------------------------------+------------+
| home::user1                                     |          6 |
| local::/srv/www/htdocs/owncloud5/data/       |          3 |
| local::/srv/www/htdocs/owncloud5/data/root/  |          1 |
| local::/srv/www/htdocs/owncloud5/data/user1/ |          2 |
| local::/srv/www/htdocs/owncloud5/data/user2/ |          4 |
| local::/srv/www/htdocs/owncloud5/data/user3/ |          5 |
+-------------------------------------------------+------------+

And the oc_share table has shares still pointing to the old home storage...

Let's see how to fix that situation without losing the shares...

@PVince81

Copy link
Copy Markdown
Contributor Author

Here are some tentative fixing steps:

Step 1: Try and find the old data dir path.

For some reason, the DB always has one entry with the data.
So, to find it, find an entry with the format local::/some/path/ where exists other paths where known user ids are appended, like local::/some/path/user1, local::/some/path/user2, with known users.

Step 2: Find duplicate home:: based formats and pair them with the old data dir entries.

So we'd get a structure like:

array(
    array(
        'userID' => 'user1', 
        'oldStorageId' => 'local::/srv/www/htdocs/owncloud5/user1',
        'newStorageId' => 'home::user1'
    ),
    array(
        'userID' => 'user2', 
        'oldStorageId' => 'local::/srv/www/htdocs/owncloud5/user2',
        'newStorageId' => 'home::user2'
    ),
    ...
)

Step 3: We want to keep the shares and possibly other tables that might use file ids from the file cache, that might point to the old storage.

So we delete the home:: based storage entry and its entries in the oc_filecache table.
Here we'd delete home::user1, home::user2, home::user3.

Step 4: Rename the local:: ids to home::.

For example local::/srv/www/htdocs/owncloud5/user1 gets renamed to home::user1.

Step 5: Trigger a filecache rescan for each user

Because the oc_filecache from the old storage might be outdated.
It depends how long the user has been using the new storage entry.

Additional Issues

If a user has been using the new storage for a long time, the following might happen:
The user might have had shares defined for local::/srv/www/htdocs/user1. These are conserved by the repair steps above.
But what happens if the user already created new shares while the duplicate home::user1 was existing ? Those share entries pointed to the home::user1 file cache entries instead of the local:: one.

I don't think we want to "merge" the filecache entries in such case. We also are unable to modify the shares from the home:: storage to the other storage.

I have the feeling that it won't be possible to conserve user shares reliably with this repair script, unless it is executed early, before users start to use the migrated system...

We should probably have auto-migrated home storages on upgrade when it was still possible.
It might still possible to provide this, but some installs might be messed up so a repair script would still be required.

Any comments on this @icewind1991 ?

@PVince81

Copy link
Copy Markdown
Contributor Author

How to fix the share table (just writing down ideas):

  1. Find the numeric id of the "home::" storages
  2. Find all share entries in oc_share that point to a file on that (to be deleted) storage
  3. For each file pointed by the share, find if the matching (duplicate) storage file cache has the same file (by path). If yes, then update the fileid of the share to that file, unless there is already a share entry for that specific case.

This is getting quite complex...

@PVince81

Copy link
Copy Markdown
Contributor Author

These days I had some new ideas that might be relevant to fixing cases where the data directory was moved:

  1. We could add a setting "olddatadir" in config.php that can be used to identify old storage entries
  2. As long as the repair script isn't run, "olddatadir" could be used to make the old homes still work when logging in, even when datadir was moved.
  3. Have the entry be fixed automatically upon first login ?
  4. The repair script could use "olddatadir" to identify the old "local::" entries and rename them with "home::" entries, but only if the following conditions are met:
    a) the "home::" entry doesn't have anything in the "oc_filecache" table, which means no data yet
    b) the "local::" entry has data in the "oc_filecache"
    If both "home::" and "local::" entries have "oc_filecache" entries, then the admin will have to manually fix the DB (we could add instructions), as the script cannot decide which one of "local::" or "home::" needs to be kept for that particular user.

@PVince81

Copy link
Copy Markdown
Contributor Author

I've modified the script to do the following:

  1. If no "home::" exists, then rename "local::" to "home::"
  2. If "home::" exist, whichever of both storages that has no entries in the filecache will be used as the new "home::"
  3. Added unit tests

There is no code yet for the old data dir, still need to think about it.

I think in most cases this situation exists only because people moved the data dir. Maybe they put it back, maybe didn't.

@PVince81

Copy link
Copy Markdown
Contributor Author

@karlitschek just re-saw your comment:

Well I think that the DB is already "broken" if duplicate entries exist. So deleting them has to be
done sooner or later anyways

It means that if conflict exists, then we always delete the old one.

This means that share entries would also be broken, so this would need to be cleant up as well (I raised #7874 to clean up orphaned entries, so that could also include shares from files from dead storages)

@karlitschek another comment: I saw in the code that the repair routines are run during migration, is that intended ? Or do we want admins to always run this here manually ?

@ghost

ghost commented Mar 25, 2014

Copy link
Copy Markdown

@ghost

ghost commented Mar 25, 2014

Copy link
Copy Markdown

@ghost

ghost commented Mar 25, 2014

Copy link
Copy Markdown

@PVince81

Copy link
Copy Markdown
Contributor Author

Failing unit tests are due to Oracle.

Still, would be good to get some feedback from you guys on this version of the script, as a first version. Do you agree that in case of conflict between "home::" and "local::" (both having data), then "home::" should win regardless ?

@ghost

ghost commented Mar 25, 2014

Copy link
Copy Markdown

@PVince81

Copy link
Copy Markdown
Contributor Author

Looks like Oracle doesn't like closeCursor(), it closes it the first time but the second time we recreate that prepared statement, it fails with oci_bind_by_name(): 19559 is not a valid oci8 statement resource 😠

@icewind1991

Copy link
Copy Markdown
Contributor

So far we always had this "policy" that we don't need to close files or cursors, but sometimes you just can't rely on garbage collection to do this early enough, especially with unit tests...

I would guess the reason gc isn't enough here is that you store the statements as class members instead of local variables

@PVince81

Copy link
Copy Markdown
Contributor Author

@icewind1991 yes, possibly. And the class instance probably gets collected too late during unit tests run.

@scrutinizer-notifier

Copy link
Copy Markdown

A new inspection was created.

@ghost

ghost commented Aug 22, 2014

Copy link
Copy Markdown

@PVince81

Copy link
Copy Markdown
Contributor Author

Tests finally pass! YAY!

@icewind1991 please have a last sanity check at b8b2ef8 and the closeCursor parts here: https://github.com/owncloud/core/pull/7539/files#diff-07a5cf597a8775f7351a9bbaa5eb5084R89

Then this can be merged.

@icewind1991

Copy link
Copy Markdown
Contributor

👍

DeepDiver1975 added a commit that referenced this pull request Aug 22, 2014
Added repair script to upgrade the legacy home storage ids
@DeepDiver1975
DeepDiver1975 merged commit 0e8916b into master Aug 22, 2014
@DeepDiver1975
DeepDiver1975 deleted the repair-legacystorageid branch August 22, 2014 12:22
@PVince81

Copy link
Copy Markdown
Contributor Author

@karlitschek do you think we should backport this for 7.0.3 ?

One trouble with this is that users with "broken" database will get an exception and might be frustrated that a minor version upgrade causes this. "Broken" as in a state that cannot be repaired automatically and need manual operation.

Another idea would be to only integrate this in the CLI "repair" command for 7.0.3 for those who do want to try it, but make it auto-run on OC 8.

@PVince81

PVince81 commented Sep 5, 2014

Copy link
Copy Markdown
Contributor Author

Follow up here: #10897
We might need to detect whether the datadir was changed in older OC versions and show a warning to avoid breakage.

@MorrisJobke

Copy link
Copy Markdown
Contributor

@karlitschek The backport request was triggered by @butonic

@PVince81

Copy link
Copy Markdown
Contributor Author

Beware, if any user in OC 7 has broken storages the upgrade will REFUSE to run because of the exception. I thought it isn't acceptable to block users in a minor upgrade. Major upgrade might be ok.

Else we'll need to reconsider making it a soft fail: #13513

@PVince81

Copy link
Copy Markdown
Contributor Author

"broken storage" as in "a broken storage situation that cannot be repaired automatically"

@karlitschek

Copy link
Copy Markdown
Contributor

I agree with @PVince81 We should the broken entries or ignore them. But blocking upgrade without the option to repair it is not good in a minor version.

@idmacdonald

Copy link
Copy Markdown

Hi, can anyone tell me whether I should expect the 'occ maintenance:repair' task to fix the absolute paths in oc_storages on v8.2.3? I just migrated an ownCloud install from one server to another and in the end I had to install it in the same filesystem location on the new server as the old one (which in this case is rather inconvenient), because I was not able to fix the storage IDs. I've run 'occ maintenance:repair', but after running it I still see all the absolute paths in the oc_storages table, so it doesn't appear to actually be doing anything. Cheers, Ian.

@PVince81

PVince81 commented Aug 9, 2016

Copy link
Copy Markdown
Contributor Author

@idmacdonald

Copy link
Copy Markdown

Hi @PVince81 I have posted my issue there:
https://central.owncloud.org/t/shared-files-broken-after-moving-install-to-a-different-filesystem-location/1537

I couldn't figure out how to send a private message over there, so just letting you know. Cheers, Ian.

@lock

lock Bot commented Aug 5, 2019

Copy link
Copy Markdown

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock Bot locked as resolved and limited conversation to collaborators Aug 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate script to update home storage ids