Lost my sites.json. Any way to regenerate?

I have several hundred sites on my Local which I’m no longer able to access, as my sites.json is now blank.

How it happened: I was importing a large site, and ran out of disk space (this is on Ubuntu). I got a generic crash message, emptied my trash, and upon my second attempt, I couldn’t get Local to open. I then realized my sites.json was fully blank (0kb). I had to delete the blank file, upon which Local regenerated it and I was able to open Local, albeit with no sites listed.

All of the sites still exist in my Local directory.

My question: is there a way to regenerate sites.json somehow?

It seems that much of the data still exists in ~/.config/Local/run – the project IDs, the project path, and some of the service port numbers and service versions. I thought to write a bash script that will iterate through the folders in /run and generate a new sites.json. I was able to get as far as getting my sites back in local, but they wouldn’t actually start. I don’t have much knowledge as to how all of the architecture works – maybe my strategy isn’t plausible.

Possibly adding to the complexity is that, in the meantime, I’ve needed to manually import some sites I’m working on. I notice that the new sites.json has ports starting at 10000, which I gather is the starting point. I imagine this might cause conflicts with the original first few sites that I had in Local, that I still hope to recover.

Any advice/insight would be appreciated.

Sorry to hear about the blank sites.json, @alvin.

There isn’t a built-in way to regenerate sites.json but you have a few options:

  1. Restore your sites.json from a system backup if you have one.
  2. Re-import your sites from the Local Sites folder to have Local create new sites.json entries for you, following this re-import guide. I recognise that this could be slow-going with hundreds of sites, though. Note that this will also duplicate your sites in the Local Sites folder — you may have to give imported sites a new name as a result, or move the original out of the Local Sites folder before importing the zip.
  3. Try again to recreate the sites.json from your script, ensuring each site entry uses this pattern:

Overall structure:

{
  "site-id-from-run-folder": {
  },
  "site-id2-from-run-folder": {
  }
}

For each site (you may have to scroll the code area to see the full content):

  "site-id-from-run-folder": {
    "id": "site-id-from-run-folder",
    "name": "mysite",
    "path": "/Path/To/Local Sites/mysite",
    "domain": "mysite.local",
    "localVersion": "9.1.1+6738",
    "mysql": { "database": "local", "user": "root", "password": "root" },
    "services": {
      "mailpit": {
        "name": "mailpit",
        "version": "1.12.1",
        "type": "lightning",
        "ports": { "WEB": [10000], "SMTP": [10001] }
      },
      "php": {
        "name": "php",
        "version": "8.2.23",
        "type": "lightning",
        "role": "php",
        "ports": { "cgi": [10002] }
      },
      "nginx": {
        "name": "nginx",
        "version": "1.26.1",
        "type": "lightning",
        "role": "http",
        "ports": { "HTTP": [10003] }
      },
      "mysql": {
        "name": "mysql",
        "version": "8.0.16",
        "type": "lightning",
        "role": "db",
        "ports": { "MYSQL": [10004] }
      }
    },
    "workspace": null,
    "multiSite": "",
    "xdebugEnabled": true,
    "ports": {}
  },

The important things to get right so that sites start are the site-id-from-run-folder, the path, and the services list.

I notice that the new sites.json has ports starting at 10000, which I gather is the starting point. I imagine this might cause conflicts with the original first few sites that I had in Local, that I still hope to recover.

Local will attempt to assign new ports to any services with conflicting port numbers, so it should not matter too much where ports start or that they’ve ‘reset’ to 10000 as a result of the blank sites.json. Even if two services end up with the same port number in sites.json, Local will try to reassign the conflicting one when you start the site.

I hope that helps you to restore your sites, but let us know if you have any further questions.

@nickc As it turns out, my bash script was working after all. My sites are back! The only additional step I need to do is change the PHP version for a site, and then things seem to reset and I’m able to load the site. This is much much easier then a full re-import of each site.

Thanks for your feedback as it confirmed I wasn’t missing something and encouraged me to keep trying in the direction I was going. I think I better set up a scheduled backup for sites.json now.

1 Like

Great to hear, @alvin, thanks for letting us know!