# The script to automatically fix a malformed database

**URL:** <https://forum.storj.io/t/the-script-to-automatically-fix-a-malformed-database/27250>\
**Category:** troubleshooting\
**Tags:** linux, database, useful-tools\
**Created:** [July 24, 2024, 4:01am UTC](https://forum.storj.io/t/the-script-to-automatically-fix-a-malformed-database/27250 "2024-07-24T04:01:38Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ItsHass](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/itshass/32/1122_2.png) [@ItsHass](https://forum.storj.io/u/ItsHass)\
**Post date:** [July 24, 2024, 4:01am UTC](https://forum.storj.io/t/the-script-to-automatically-fix-a-malformed-database/27250/1 "2024-07-24T04:01:38Z")

</div>

I have in the last few hours created a script which actually checks / fixes .db files which is from [that help guide](https://support.storj.io/hc/en-us/articles/360029309111-How-to-fix-a-database-disk-image-is-malformed). ( i found this after i posted. ) 👍

[uploaded here](https://github.com/storjdashboard/storj_useful_scripts/tree/main/fix_malformed_database) happy for contributions to help solve db issues quicker.

#[storj\_useful\_scripts/fix\_malformed\_database at main · storjdashboard/storj\_useful\_scripts · GitHub](https://github.com/storjdashboard/storj_useful_scripts/tree/main/fix_malformed_database)

---

<div class="post-metadata">

**Author:** ![Kopcap](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/kopcap/32/1137_2.png) [@Kopcap](https://forum.storj.io/u/Kopcap)\
**Post date:** [July 25, 2024, 4:59pm UTC](https://forum.storj.io/t/the-script-to-automatically-fix-a-malformed-database/27250/2 "2024-07-25T16:59:20Z")

</div>

Thank you for the contribution.  
Unfortunately in my case the script did not help.  
Here is an output:

```auto
> Do you acknowledge and accept the disclaimer? (yes/no): yes
> Enter the directory containing the SQLite database files: /home/sk/StorjTEST/BAD
> Enter the directory where you want to save backups: BAK
> Enter the directory where you want to save the fixed databases: GOOD
> Checking integrity of /home/sk/StorjTEST/BAD/bandwidth.db...
> The database /home/sk/StorjTEST/BAD/bandwidth.db is not malformed.
> Checking integrity of /home/sk/StorjTEST/BAD/garbage_collection_filewalker_progress.db...
> The database /home/sk/StorjTEST/BAD/garbage_collection_filewalker_progress.db is not malformed.
> Checking integrity of /home/sk/StorjTEST/BAD/heldamount.db...
> The database /home/sk/StorjTEST/BAD/heldamount.db is not malformed.
> Checking integrity of /home/sk/StorjTEST/BAD/info.db...
> The database /home/sk/StorjTEST/BAD/info.db is not malformed.
> Checking integrity of /home/sk/StorjTEST/BAD/notifications.db...
> The database /home/sk/StorjTEST/BAD/notifications.db is not malformed.
> Checking integrity of /home/sk/StorjTEST/BAD/orders.db...
> The database /home/sk/StorjTEST/BAD/orders.db is not malformed.
> Checking integrity of /home/sk/StorjTEST/BAD/piece_expiration.db...
> Integrity check failed for /home/sk/StorjTEST/BAD/piece_expiration.db. Attempting to fix...
> Backing up the original database...
> Exporting the database to SQL...
> Creating a new database from the SQL dump...
> Cleaning up...
> Checking integrity of the fixed database...
> The fixed database GOOD/piece_expiration.db passed the integrity check.
> Do you want to replace the original database with the fixed database? (yes/no): no
> Kept the fixed database in GOOD/piece_expiration.db.
> Checking integrity of /home/sk/StorjTEST/BAD/pieceinfo.db...
> The database /home/sk/StorjTEST/BAD/pieceinfo.db is not malformed.
> Checking integrity of /home/sk/StorjTEST/BAD/piece_spaced_used.db...
> The database /home/sk/StorjTEST/BAD/piece_spaced_used.db is not malformed.
> Checking integrity of /home/sk/StorjTEST/BAD/pricing.db...
> The database /home/sk/StorjTEST/BAD/pricing.db is not malformed.
> Checking integrity of /home/sk/StorjTEST/BAD/reputation.db...
> The database /home/sk/StorjTEST/BAD/reputation.db is not malformed.
> Checking integrity of /home/sk/StorjTEST/BAD/satellites.db...
> The database /home/sk/StorjTEST/BAD/satellites.db is not malformed.
> Checking integrity of /home/sk/StorjTEST/BAD/secret.db...
> The database /home/sk/StorjTEST/BAD/secret.db is not malformed.
> Checking integrity of /home/sk/StorjTEST/BAD/storage_usage.db...
> The database /home/sk/StorjTEST/BAD/storage_usage.db is not malformed.
> Checking integrity of /home/sk/StorjTEST/BAD/used_serial.db...
> The database /home/sk/StorjTEST/BAD/used_serial.db is not malformed.
> Checking integrity of /home/sk/StorjTEST/BAD/used_space_per_prefix.db...
> The database /home/sk/StorjTEST/BAD/used_space_per_prefix.db is not malformed.
> All databases have been processed.

```

However, the file piece\_expiration.db turned out to be zero length 😒  
I could attach the corrupted database file if you are interested in debugging the script.

---

<div class="post-metadata">

**Author:** ![Alexey](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/alexey/32/41_2.png) [@Alexey](https://forum.storj.io/u/Alexey)\
**Post date:** [July 26, 2024, 6:45am UTC](https://forum.storj.io/t/the-script-to-automatically-fix-a-malformed-database/27250/3 "2024-07-26T06:45:19Z")

</div>

This is one of the reason why we never tried to implement an automatic fixer. Too many different cases.  
Are you able to fix it manually?

---

<div class="post-metadata">

**Author:** ![ItsHass](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/itshass/32/1122_2.png) [@ItsHass](https://forum.storj.io/u/ItsHass)\
**Post date:** [July 26, 2024, 7:31am UTC](https://forum.storj.io/t/the-script-to-automatically-fix-a-malformed-database/27250/4 "2024-07-26T07:31:20Z")

</div>

If you have recently had issue with ‘piece\_expiration.db’ during a DB migration from the storj node update.

This is an issue reported in this thread. [V58 db migrate fail - piece\_expiration.db - 1.108.3](https://forum.storj.io/t/v58-db-migrate-fail-piece-expiration-db-1-108-3/27238)

Is the BAD piece-expiration file , 0kb? or the new one it created 0kb?

Cheers,

---

<div class="post-metadata">

**Author:** ![Kopcap](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/kopcap/32/1137_2.png) [@Kopcap](https://forum.storj.io/u/Kopcap)\
**Post date:** [July 26, 2024, 7:45am UTC](https://forum.storj.io/t/the-script-to-automatically-fix-a-malformed-database/27250/5 "2024-07-26T07:45:43Z")

</div>

The new one was created 0b

---

<div class="post-metadata">

**Author:** ![ItsHass](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/itshass/32/1122_2.png) [@ItsHass](https://forum.storj.io/u/ItsHass)\
**Post date:** [July 26, 2024, 7:48am UTC](https://forum.storj.io/t/the-script-to-automatically-fix-a-malformed-database/27250/6 "2024-07-26T07:48:35Z")

</div>

No issues with read/write to the directory ?

Has doing it manually fixed it ?

> **[How to fix a "database disk image is malformed"](https://support.storj.io/hc/en-us/articles/360029309111-How-to-fix-a-database-disk-image-is-malformed)**
>
> Problem
> Sometimes a Storage Node Operator may encounter the "database disk image is malformed" error in their log. This could happen during unplanned shutdown or reboot. The error indicates that on...

---

<div class="post-metadata">

**Author:** ![Kopcap](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/kopcap/32/1137_2.png) [@Kopcap](https://forum.storj.io/u/Kopcap)\
**Post date:** [July 26, 2024, 7:55am UTC](https://forum.storj.io/t/the-script-to-automatically-fix-a-malformed-database/27250/7 "2024-07-26T07:55:56Z")

</div>

No, I was upset by the failure and simply deleted all the databases.  
But I still have a backup, I’ll try to do a manual fix for fun.  
Is it possible to replace a new database file (created from scratch) with a fixed database later?

---

<div class="post-metadata">

**Author:** ![ItsHass](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/itshass/32/1122_2.png) [@ItsHass](https://forum.storj.io/u/ItsHass)\
**Post date:** [July 26, 2024, 8:00am UTC](https://forum.storj.io/t/the-script-to-automatically-fix-a-malformed-database/27250/8 "2024-07-26T08:00:01Z")

</div>

@Kopcap

> [@Kopcap](#):
>
> Is it possible to replace a new database file (created from scratch) with a fixed database later?

Yes

> [@Kopcap](#):
>
> No, I was upset by the failure and simply deleted all the databases.

I have just made an update to the file which checks for read/write before proceeding.

I have also made it so it does check file sizes and copies rather than moves any files so that you will always have the backups

> <https://github.com/storjdashboard/storj_useful_scripts/commit/573eca7eba1ce7a4f7d069faa7e53bb65c7fd76d>
>
> Updated to COPY not MOVE files.
> 
> Directories READ/WRITE permission checks befo…re proceeding.
> 
> After copying any files - make sure file sizes match.

---

<div class="post-metadata">

**Author:** ![Kopcap](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/kopcap/32/1137_2.png) [@Kopcap](https://forum.storj.io/u/Kopcap)\
**Post date:** [July 26, 2024, 9:24am UTC](https://forum.storj.io/t/the-script-to-automatically-fix-a-malformed-database/27250/9 "2024-07-26T09:24:46Z")

</div>

> [@ItsHass](#):
>
> I have just made an update to the file which checks for read/write before proceeding.

Updated version created zero file, too 😞  
Will try manual fix

---

<div class="post-metadata">

**Author:** ![ItsHass](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/itshass/32/1122_2.png) [@ItsHass](https://forum.storj.io/u/ItsHass)\
**Post date:** [July 26, 2024, 10:36am UTC](https://forum.storj.io/t/the-script-to-automatically-fix-a-malformed-database/27250/10 "2024-07-26T10:36:21Z")

</div>

Can you upload it here at all? - not sure if we can - I will try to form a fix

---

<div class="post-metadata">

**Author:** ![Kopcap](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/kopcap/32/1137_2.png) [@Kopcap](https://forum.storj.io/u/Kopcap)\
**Post date:** [July 26, 2024, 10:45am UTC](https://forum.storj.io/t/the-script-to-automatically-fix-a-malformed-database/27250/11 "2024-07-26T10:45:31Z")

</div>

I fixed the corrupted database manually.  
Thank you for detailed instructions! 🙏

---

<div class="post-metadata">

**Author:** ![Kopcap](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/kopcap/32/1137_2.png) [@Kopcap](https://forum.storj.io/u/Kopcap)\
**Post date:** [July 26, 2024, 10:50am UTC](https://forum.storj.io/t/the-script-to-automatically-fix-a-malformed-database/27250/12 "2024-07-26T10:50:26Z")

</div>

I can’t find the option to upload files here.  
I will send you a link to the file in a private message

---

<div class="post-metadata">

**Author:** ![ItsHass](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/itshass/32/1122_2.png) [@ItsHass](https://forum.storj.io/u/ItsHass)\
**Post date:** [July 26, 2024, 10:52am UTC](https://forum.storj.io/t/the-script-to-automatically-fix-a-malformed-database/27250/13 "2024-07-26T10:52:23Z")

</div>

Glad you managed to fix it… - Yes if you can do that - I can then debug this DB … to see what was causing it to not work

---

<div class="post-metadata">

**Author:** ![MRF](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/mrf/32/1125_2.png) [@MRF](https://forum.storj.io/u/MRF)\
**Post date:** [February 7, 2025, 11:20am UTC](https://forum.storj.io/t/the-script-to-automatically-fix-a-malformed-database/27250/14 "2025-02-07T11:20:40Z")

</div>

These instructions for docker worked well for me and fixed my broken storage\_usage.db - even tho I had no idea what I was doing 🙂 Thx a lot.

---

<div class="post-metadata">

**Author:** ![ItsHass](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/itshass/32/1122_2.png) [@ItsHass](https://forum.storj.io/u/ItsHass)\
**Post date:** [February 7, 2025, 4:13pm UTC](https://forum.storj.io/t/the-script-to-automatically-fix-a-malformed-database/27250/15 "2025-02-07T16:13:23Z")

</div>

Nice ! 🙂

Thanks for the feedback
