Corrupted SQL Database Fix (Ubuntu/Debian)

Hi All,

After a whirl-wind of a weekend with trying to upgrade to Seafile Pro, I thought I’d share a what-to-do in the case of your SQL database gets corrupted, overwritten (as in my case, completely on accident!) and/or you are looking to move the server but want to have a fresh setup!

This tutorial is for the case that you haven’t got any backups that you can revert back to, but you have still got the main Seafile-Data folder from within the main directory. I was unlucky enough to not be able to scan the blocks from within the Seafile-Data/storage/…

Just a quick over view:

  • Your database has corrupted, or been written over
  • You have no database backups that you would be able to go back to
  • You still have the SeaFile-Data Folder
  • You are able to do a fresh installation of Seafile

@daniel.pan - Please let me know if I am technically incorrect anywhere in here!

Now, first’s things first, DO A BACKUP OF THE SEAFILE-DATA FOLDER!!!

It may be scary to start with, but if you can do this backup anywhere on the system, or preferably an external system, this will help ensure that nothing is lost!

Second step, you will need to rename the Seafile folder to a different name, in my case, I have called it “Seafile2”. So your OLD Seafile install would now be “/opt/seafile2/”.

Use the move command to do this:

  • cd /opt/
  • mv seafile seafile2

Now, you will need to create the new directory and add Seafile as the owner:

  • cd /opt/ (in-case you have left it)
  • mkdir Seafile
  • chown seafile seafile/

There is 2 possible next steps with re-installing this:
If you use Pro (10.0.11 in this example}

If you use CE (Community Edition)

Once you have done the re-installation of the software, and you are able to access it through a web browser, please do the following:

  • cd /opt/seafile
  • rm -r seafile-data
  • cd /opt/seafile2
  • mv seafile-data /opt/seafile/
    (Please move the original data, but ensure that you have a copy elsewhere).

Now you need to get the folder names, do this, please do the following:

  • cd /opt/seafile/seafile-data/storage/blocks
  • ls -a
    (make a note of ALL of the folders in a notepad or somewhere safe).

Once you have gotten the names, you will need to connect your your SQL, to do this:

  • mysql -u root
    (Please note, the next steps are SQL commands)
  • USE Seafile_DB;
  • Select * from Repo;

From the list, you should see some ID numbers, please ensure that you start with a number higher than the highest number on that list, if you want to make it list it in order, use the following command:

  • select * from Repo group by ID asc;

Now you need to import the folders that you have found, to do this, please use the following command:

  • insert into Repo (id, repo_id) values ({unique ID}, ‘{folder name}’);
  1. {unique id} = the next number in the ID list
  2. {folder name} = the name of the folder from the blocks list (i.e. ######-120d-82js-9823-1027192jud73)
  3. Please remove the {} from the names

Once you have run this command for all of the libraries that you want to import, you then need to add the RepoOwner, use the following command to do this:

  • insert into RepoOwner (id, repo_id, owner_id) values ({The corresponding ID} , ‘{the corresponding folder name}’, ‘admin@seafile.local’);

Repeat this command for all libraries and then go to the following folder and run the following command:

  • cd /opt/seafile/seafile-server-latest
  • su seafile
  • ./ --repair

Once this run has been completed, please run:

  • ./ restart
  • ./ restart

You should now be able to see all of your libraries :slight_smile:

You will still need to re-create all of the shares and users, but at least the files will be there!

Please feel free to comment below if you have any questions, or if there is anything that is incorrect and needs to be updated!

Kind regards,