Last active
June 7, 2023 21:10
-
-
Save bootrino/2ed0b317913960578697499c5d43bd7f to your computer and use it in GitHub Desktop.
Transfer Ubuntu ec2 server to 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
# USE AT YOUR OWN RISK | |
# THIS IS FOR EXPERTS ONLY - KNOW WHAT YOU ARE DOING. | |
#Step 1: | |
Make sure old system and new system are running same OS | |
On both systems: | |
apt update | |
apt upgrade | |
#Step 2: | |
reboot both systems | |
#Step 3: | |
On both systems, create systemd unit maintenance.target | |
This allows you to stop all processes except ssh and networking. | |
Note: you won't be able to transfer stuff onto the server whie it is in maintenance mode | |
create /etc/systemd/system/maintenance.target | |
[Unit] | |
Description=Maintenance Mode with Networking and SSH | |
Requires=maintenance.target systemd-networkd.service sshd.service | |
After=maintenance.target systemd-networkd.service sshd.service | |
AllowIsolate=yes | |
enter your maintenance mode: | |
sudo systemctl isolate maintenance.target | |
exit maintenance mode | |
sudo systemctl isolate multi-user.target | |
#Step 4: | |
Bring the old server to maintenance mode | |
#Step 5: | |
As sudo, run command to create backup file: | |
(check the filenames in the tar first, maybe your swapfile is different) | |
refer here for xz/multicore compression | |
https://gist.github.com/bootrino/bc4a4b8bfe82180391904046c2c9e344 | |
tar -cvpzf backup.tar.gz \ | |
--exclude=/mnt/backup.tar.gz \ | |
--exclude=/var/swap.1 \ | |
--exclude=/proc \ | |
--exclude=/tmp \ | |
--exclude=/mnt \ | |
--exclude=/dev \ | |
--exclude=/sys \ | |
--exclude=/run \ | |
--exclude=/media \ | |
--exclude=/boot \ | |
--exclude=/usr/src/linux-headers* \ | |
--exclude=/home/*/.gvfs \ | |
--exclude=/home/*/.cache \ | |
--exclude=/home/*/.local/share/Trash / | |
#Step 6: | |
put a copy of the target servers pem file into the current directory | |
#Step 7: | |
transfer the backup file to the target server: | |
sudo scp -i tmp.pem backup.tar.gz ubuntu@x.x.x.x:. | |
#Step 8: | |
on the target server, go into maintenance mode | |
sudo systemctl isolate maintenance.target | |
#Step 9: | |
on the target server, copy your ssh authorized keys to your home directory: | |
cd ~ | |
cp .ssh/authorized_keys ~/. | |
#Step 10: | |
on the target server backup /etc/fstab | |
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 . | |
#Step 11: | |
restore the backup file: | |
sudo tar -xvpzf backup.tar.gz -C / --numeric-owner | |
#Step 12: | |
rationalise the two fstab files - examine both and probably use the new one | |
#Step 13: CRITICAL! | |
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. | |
#Step 14: | |
recreate swapfile after restoring backup onto system | |
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/ | |
#Step 15: | |
reboot target machine | |
#Step 16: | |
check everything is working | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment