Last active
June 7, 2023 21:10
-
-
Save bootrino/dbbdaaf36644d25e3c23e8b3c5af09a2 to your computer and use it in GitHub Desktop.
Copy EC2 instance to Amazon Lightsail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HOW TO Copy EC2 instance to Amazon Lightsail | |
WARNING - this is a guide to remind myself how to do it!!!! | |
WARNING - I'M NOT RESPONSIBLE FOR ANYTHING YOU DO WITH THIS! | |
WARNING - DON'T ASK ME ANY QUESTIONS ABOUT IT. | |
WARNING - THIS IS NOT A TUTORIAL - I'm typing this from memory. | |
These steps are a good approximation of what is needed but you'll | |
need strong tech skills to do this. Don't do it unless you know what you are doing. | |
************Start by making a backup/snapshot of the old EC2 machine************ | |
Steps: | |
This assumes you are running the same operating system on both machines such as Amazon Linux 2 | |
On the ec2 instance you need to fully upgrade to the latest version of the operating system: | |
sudo apt update | |
#clean up unused files: | |
sudo apt --purge autoremove | |
On the ec2 instance you need to shut down everything except networking and ssh. | |
This ensures the backup is able to read all the files - if things like servers are running then | |
they might be holding files open which might prevent them being backed up. | |
ref: | |
https://serverfault.com/a/1057547/223657 | |
/lib/systemd/system/rescue-ssh.target | |
[Unit] | |
Description=Rescue with network and ssh | |
Documentation=man:systemd.special(7) | |
Requires=network-online.target ssh.service | |
After=network-online.target ssh.service | |
AllowIsolate=yes | |
So now, you can proceed as fallows: | |
1. Enter in the maintenance mode: | |
systemctl isolate rescue-ssh.target (only ssh and networking) | |
2. Check the maintenance mode: | |
lsof -i:1-65535 (and you will only see the port of the ssh running) | |
3. Exit from the maintenance mode: | |
systemctl isolate multi-user.target (and everything is back again) | |
at this stage everything should be shut down. you can check via: | |
sudo systemctl status | |
switch to /mnt | |
then make a backup of all the relevant files: | |
#upgrade to latest version | |
sudo apt update | |
sudo apt upgrade | |
# this command includes the ubuntu version in the name of the backup | |
refer here for xz/multicore compression | |
https://gist.github.com/bootrino/bc4a4b8bfe82180391904046c2c9e344 | |
sudo tar cvpzP --exclude=/tmp/backup$(lsb_release -sr).tar.gz --exclude=/proc --exclude=/tmp --exclude=/dev --exclude=/sys --exclude=/run --exclude=/media --exclude=/usr/src/linux-headers* --exclude=/home/*/.gvfs --exclude=/home/*/.cache --exclude=/home/*/.local/share/Trash --exclude=/mnt/swapfile --exclude=/mnt/scratch --exclude=/var/swap.1 --exclude=/mnt/swap/swapfile -f /tmp/backup$(lsb_release -sr).tar.gz /* | |
then start an instance on lightsail with for example a key file name of lightsail.pem | |
download the pem file from lightsail | |
change the permissions on the pem file: | |
chmod ugo-rwx lightsail.pem | |
chmod u+rx lightsail.pem | |
log in to the lightsail instance: | |
ssh -i lightsail.pem ec2-user@<lightsailhostipaddress> | |
update the lightsail instance | |
sudo apt update | |
sudo apt upgrade | |
from the lightsail instance scp the backup file onto the lightsail instance: | |
scp -i lightsail.pem ec2-user@<ec2hostipaddress>:/mnt/backup.tar.gz . | |
important! the one file you need to ensure is the same is /etc/fstab so | |
make a copy of it in your home directory: | |
cd ~ | |
cp /etc/fstab . | |
also on the lightsail machine make a copy of authorized keys: | |
cd ~ | |
cp .ssh/authorized_keys ~/. | |
if the machine you are copying from has swapfile then you should configure swapfile on this machine too. | |
follow the instructions here: | |
https://aws.amazon.com/premiumsupport/knowledge-center/ec2-memory-swap-file/ | |
and now on the lightsail instance you need to shut down all services except ssh and network: | |
systemctl isolate rescue-ssh.target (only ssh and networking) | |
at this stage everything should be shut down. you can check via: | |
sudo systemctl status | |
switch to /mnt | |
move the backup file into mount: | |
sudo mv ~/backup.tar.gz . | |
we are almost but not quite following these instructions because they are for ubuntu: | |
refer: https://help.ubuntu.com/community/BackupYourSystem/TAR | |
now restore all the files: | |
sudo tar -xvpzf fullbackup.tar.gz -C / --numeric-owner | |
then copy the fstab file that you saved earlier from your home dir back to /etc | |
cp ~/fstab /etc/. | |
add the contents of the authorized_keys that you saved earlier into the authorized_keys file that was restored from backup | |
this will allow you to log into the machine with both the old and new pem files. | |
check the /etc/fstab file to ensure the swapfile is in it | |
Exit from the maintenance mode: | |
systemctl isolate multi-user.target (and everything is back again) | |
reboot | |
everything should nmow be working | |
sudo apt update | |
sudo apt upgrade | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment