How to downgrade Digital Ocean Droplet

Ali
3 min readDec 9, 2021

--

At start, it is very scary to tinker with server resources to assemble them properly to make a running web app work like Laravel. For starters, it is very tempted to just resize the droplet and be good with it; but the resources you want to increase also increases other resources as by product which you absolutely don’t need at all. Today I’m going to show you how to cope with this issue.

We’ll do it in 3 steps:

STEP1: Move mysql directory to a volume (do it as root user):

  • Create a new volume.
  • Attach it to current droplet.
  • SSH into your server
  • Make yourself root

sudo -i

  • Make sure it’s properly mounted and resized

df -h

  • Put Laravel app into maintenance mode

cd into /SITE_ROOT/ => php artisan down

  • Stop mysql service (to allow for data consistency)

systemctl status mysql => systemctl stop mysql => systemctl status mysql

  • Make database backup (for safety, you can delete it later)

cp -r /var/lib/mysql /mnt/temporary/backup (this could be temporary volume, which you can delete later)

  • Move mysql folder to new volume

cp -r /var/lib/mysql /mnt/new

  • Create a symbolic link to this folder

ln -s /mnt/new/mysql /var/lib/mysql

ll -h /var/lib (mysql should point to this new volume folder)

  • Configure AppArmor Access

vi /etc/apparmor.d/tunables/alias

alias /var/lib/mysql/ => /mnt/new/mysql/, (comma is important)

cat /etc/apparmor.d/tunables/alias (make sure new rule is there)

  • Restart AppArmor

systemctl restart apparmor

  • Start MySQL Service

systemctl start mysql -> systemctl status mysql

  • Make app up again

cd into /SITE_ROOT/ => php artisan up

STEP2: Attach volume to new droplet (do it as root user):

-Make a new server with minimum resouces. -Detach the volume from current droplet and attach it to new droplet. -Now point your new droplet /var/lib/mysql folder to attached volume /mnt/new/mysql

  • SSH into your server -Make yourself root

sudo -i

  • Make sure volume is properly mounted and resized

df -h

  • Put Laravel app into maintenance mode

cd into /SITE_ROOT/ => php artisan down

  • Stop mysql service (to allow for data consistency)

systemctl status mysql => systemctl stop mysql => systemctl status mysql

  • Rename mysql folder to mysql_backup

cd /var/lib -> mv mysql mysql_backup

  • Create a symbolic link to this folder

ln -s /mnt/new/mysql /var/lib/mysql (make sure link is there: ll -h /var/lib); mysql should point to this new volume folder

  • Configure AppArmor Access

vi /etc/apparmor.d/tunables/alias

alias /var/lib/mysql/ -> /mnt/new/mysql/, (comma is important)

cat /etc/apparmor.d/tunables/alias (make sure new rule is there)

  • Restart AppArmor
  • systemctl restart apparmor -Start MySQL Service
  • systemctl start mysql -> systemctl status mysql -Make app up again

cd into /SITE_ROOT/ => php artisan up

At this point you will be getting: SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO)

That’s because in you current mysql folder there are credentials of current droplet, NOT the new one. So you have to plugin current droplet’s mysql credentials to new droplet’s environment file (for instance if you are using Laravel Forge). Delete (/SITE_ROOT/bootstrap/cache/config.php). That’s it; at this point you should be seeing your app with old data up and running.

Now you can delete expensive droplet :-)

STEP 3 [OPTIONAL]: Transfer your logs to S3 Spaces:

If you’re concerned about that your main droplet’s disk will be getting full with huge logs then worry not. You can do it in two ways: (suit your needs to use both or one)

Using via S3cmd tool:

https://www.youtube.com/watch?v=um_oZPhjak8 (forgive me if it’s not in English, but you will get the idea)

Using vie S3 API and Laravel:

--

--