Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a database recovery tool #12

Merged
merged 6 commits into from
Dec 12, 2022
Merged

Conversation

puddly
Copy link
Contributor

@puddly puddly commented May 7, 2022

home-assistant/core#71351

@DeadlySin2 @Frank3501 @wolfgangpurrer I've created a tool to recover your zigbee.db and skip the broken rows that are preventing ZHA from properly migrating your database.

You will need a Python environment to install this package, along with access to sqlite3. macOS and Linux are preferable but you can probably find a pre-compiled SQLite binary for Windows if that's all you have available.

Here are some installation instructions for getting a Python virtual environment set up on every common platform: /~https://github.com/zigpy/zigpy-znp/blob/dev/TOOLS.md#table-of-contents. Ignore the pip install zigpy-znp part and instead substitute the command below:

$ pip install git+/~https://github.com/zigpy/zigpy-cli.git

If you're running Home Assistant OS, you will have to run apk add sqlite to install the sqlite3 utility.

You can then try recovering it:

$ zigpy -v db recover /path/to/your/zigbee.db fixed.db

Please post your corrupted zigbee.db in this issue as well, I'd like to keep track of what exactly is breaking and possibly include this functionality into zigpy in the future. Thanks!

@DeadlySin2
Copy link

I receive an error No such file or directory: 'sqlite3'

@puddly
Copy link
Contributor Author

puddly commented May 8, 2022

What platform are you running this on? SQLite should be pre-installed just about everywhere except Windows.

@DeadlySin2
Copy link

I’m using home assistant os with SSH & Web Terminal addon

@puddly
Copy link
Contributor Author

puddly commented May 8, 2022

Looks like it doesn't come with sqlite3 either. You can install it:

apk add sqlite

@DeadlySin2
Copy link

Everything is working fine now. Thanks for your support and time.

@davidbb
Copy link

davidbb commented Jul 29, 2022

For months now I've been seeing the error below in my HA startup logs:

Logger: zigpy.appdb
Source: components/zha/core/gateway.py:169 
First occurred: July 28, 2022, 2:45:12 PM (1 occurrences) 
Last logged: July 28, 2022, 2:45:12 PM

SQLite database file is corrupted! row 84 missing from index attributes_idx_v7

Everything works well, or at least appears to. I've attempted to run the database recovery script above on a recent copy of my zigbee database, and get a runtime error logged to the console:

2022-07-29 10:00:03.631 pegasus zigpy_cli.database INFO Pragma user version is 10
2022-07-29 10:00:03.631 pegasus zigpy_cli.database INFO Maximum table version is 10
2022-07-29 10:00:03.861 pegasus zigpy_cli.database ERROR Failed to insert INSERT INTO "sqlite_sequence"("_rowid_", "name", "seq") VALUES( 1, 'network_backups_v10', 48 );: IntegrityError('UNIQUE constraint failed: sqlite_sequence.rowid')
2022-07-29 10:00:03.872 pegasus zigpy_cli.database INFO Done

Any ideas on what is going wrong and possibly how to troubleshoot? I can share my zigbee database directly. Thanks!

@puddly
Copy link
Contributor Author

puddly commented Jul 29, 2022

Can you email it to me? I can try to take a look.

@puddly puddly force-pushed the puddly/database-recovery branch from 8ae2a90 to fc76a0c Compare July 29, 2022 19:13
@puddly
Copy link
Contributor Author

puddly commented Jul 29, 2022

Thanks.

The generated database is correct, you can ignore the warning. I've excluded those tables from the backup and have added validation. All 61 devices are present in the recovered database and it loads properly.

@davidbb
Copy link

davidbb commented Jul 30, 2022

Thank you @puddly

@davidbb
Copy link

davidbb commented Jul 30, 2022

Just posting to update that after running the db recovery tool, replacing my home assistant zigbee database with the fixed database, I am no longer seeing the error:

SQLite database file is corrupted! row 84 missing from index attributes_idx_v7

at startup. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants