Skip to content

Instantly share code, notes, and snippets.

@edalzell
Created June 28, 2021 18:57
Show Gist options
  • Save edalzell/d46ba084d65e2145059a36ee0250dab9 to your computer and use it in GitHub Desktop.
Save edalzell/d46ba084d65e2145059a36ee0250dab9 to your computer and use it in GitHub Desktop.
Migrate Servers

Preparation

The preparation of the migration should take place a few days in advance, based off the current TTL of the A record for the domain. To make sure everything runs smoothly I recommend doing this at least two days in advance.

  • Decrease the TTL of the DNS record to 300 in a few days in advance.
  • Create a new server with the server provisioner (i.e. Forge, Ploi)
    • Make sure to set up the timezone correctly on the server
  • Create a new site on the new server
  • Create a new database + corresponding database user
    • Make sure you can connect to the database from your local machine over SSH
  • Set up the SSH connection between the old server and the new server
    • Make sure the firewall allows the SSH connection from the old server
    • Make sure the public key (cat ~/.ssh/id_rsa.pub)of the old server is in the ~/.ssh/authorized_keys of the new server
  • Copy over the .env from the old server to the new server: rsync .env [email protected]:/home/user/website.com/
  • Configure the deploy script of the new site
  • Deploy the app to the new server, fix any errors you get, extend checklist if necessary.
  • Copy over the storage from the old server to the new server: rsync -az --progress ./storage/* [email protected]:/home/user/website.com/storage
  • Check out if everything is migrated correctly
    • Update your /etc/hosts so you can check out the new server !BEFORE! updating any DNS.
    • Double check if the site is displaying correctly using your updated /etc/hosts, fix any errors in configuration and extend checklist if necessary.

The Migration

After you're sure everything is working as intended, it's time to do the actual migration.

  • Activate maintenance mode on the old server: php artisan down
  • Migrate the database from the old server to the new server
    • Export the database on the old server (using TablePlus or whatever you prefer)
    • Import the database on the new server (using TablePlus or whatever you prefer)
  • Migrate the differences in the storage files from the old server to the new server:
    • First run a dry run: rsync -az --progress --delete --dry-run ./storage/ [email protected]:/home/user/website.com/storage
    • If everything checks out, run the actual command without --dry-run
  • Last double check to make sure everything was moved correctly, using your already updated /etc/hosts
  • Update the DNS to the ip of the new server
  • Create a new Let's Encrypt certificate, try again after 5 minutes if it fails.
  • Remove the entry from your /etc/hosts

After the DNS has propagated for the users, the 'maintenance mode' will be automatically turned off (since they're now connecting to the new server). Since your TTL was set to 300, it'll usually take around 5-10 minutes for all users to connect to the new host.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment