Procedure to create a re-usable configuration export script to move a virtual machine configuration to a new one

The following procedure is about deprecating an old Linux server, and
move crucial configuration to a new fresh install that has the desired
configuration by cloning it, then applying the Old VM‘s configuration.

The steps will go as follows:

  1. Grab the original machine’s configuration
  2. Clone the New VM that will replace the Old
  3. Disable the Old VM web application

1. Grab the original machine’s configuration

The steps will create a tar file with the configurations.

  • Connect to the production vm (morpheus) through the cloud provider console (e.g. vSphere client, or VCloud director webapp or Open Stack console)
    • Connect through Putty, or your local development VM terminal the follwing lines (the console do not share the clipboard)
ssh username@morpheus.networkname.net
  • Create this file
vi ~/make-migrate.sh
  • Enter in vim paste mode
:set paste
  • Paste this content
#!/bin/bash 
cd ~ 
mkdir -p ~/migrate/etc/network 
sudo cp /etc/network/interfaces ~/migrate/etc/network/ 
sudo cp /etc/hostname ~/migrate/etc/ 
sudo cp /etc/resolv.conf ~/migrate/etc/ 
mkdir -p ~/migrate/etc/ssh 
sudo cp /etc/ssh/ssh_host* ~/migrate/etc/ssh/ 
mkdir -p ~/migrate/home/username/.ssh 
cp ~/.ssh/* ~/migrate/home/username/.ssh/ 
mkdir -p ~/migrate/home/username/_prod/app/config 
cp ~/_prod/app/config/parameters.ini ~/migrate/home/username/_prod/app/config 
mkdir -p ~/migrate/etc/apache2/sites-available 
sudo cp /etc/apache2/sites-available/* ~/migrate/etc/apache2/sites-available/ 
sudo cp /etc/resolv.conf ~/migrate/etc/ 
sudo chown -R username:username ~/migrate 
tar cfz ~/migrate.tar.gz ~/migrate 
mv migrate.tar.gz _prod/web/
  • Execute it
/bin/bash ~/make-migrate.sh
  • The latter moves into the
  • Download https://morpheus.networkname.net/migrate.tar.gz

2. Clone the New VM that will replace the Old

This step is about cloning the functionnal VM.

Since it is specific to your cloud management system, I will not describe any way to do so.

Important points to consider while doing so, ensure that you:

  • disable or isolate networking
  • have access to an alternate console

Otherwise you might just create conflicts.

2. Render the production machine unavailable

You should have a copy of the original VM (e.g. morpheus-stg) with the config working the way you want.

This step is specific to the way you might commission or decomission your web app.

You may even not need to decomission it if you have multiple database servers not on the same host.

3. Prepare the New VM to use the Original VM

  • Start the cloned VM
  • Connect through the vCenter console
  • Download the file, sorry, no cut in paste in the cloud provider console :(
  • Assuming you are in /home/username
pwd
/home/username
wget --no-check-certificate https://morpheus.networkname.net/migrate.tar.gz
  • Get the migrate folder from the extracted archive (it will be in a ‘home’ folder after extracting)
tar xfz migrate.tar.gz
mv home/username/migrate .
rm -rf home
  • Disable the network configuration
  sudo /etc/init.d/networking stop
  • Run the following commands:
    • Echo the content of the files inside the migrate/ folder, into their original locations
    • See the file listing:
find migrate/ -type f
  • Create the migrate file original contents
cd migrate
find . -type f > prepare.sh
  • Warning, the following commands we will be using vim to prepare our import script, follow the keyboard types in that sequence:
vim prepare.sh
  • (in the top left corner, where it starts), do:
  • NOTE: FOLLOW BLINDLY THOSE VIM COMMANDS … they written in the form of {modifier key}+{letter}, such as: CTRL+v, SHIFT+A, means the key combination like you would on a Graphical user interface, the + in this list is only to mean together.
dd
CTRL+v
100j
$
y
SHIFT+A
  • Add a few spaces, to make the cursor go further than the longest line
ESC
p
  • The following commands, you have to be in ESC mode, and press ENTER when finished:
:%s/  \./ /
:%s/\./cp \./
  • All is done, write and quit
:wq
  • Check the file
cat prepare.sh
  • You will end up with a file similar to
cp ./home/username/_prod/app/config/parameters.ini    /home/username/_prod/app/config/parameters.ini  
cp ./home/username/.ssh/id_rsa                        /home/username/.ssh/id_rsa  
cp ./home/username/.ssh/geritt_dsa.pub                /home/username/.ssh/geritt_dsa.pub  
cp ./home/username/.ssh/geritt_dsa                    /home/username/.ssh/geritt_dsa  
cp ./home/username/.ssh/authorized_keys2              /home/username/.ssh/authorized_keys2  
cp ./home/username/.ssh/config                        /home/username/.ssh/config  
cp ./home/username/.ssh/known_hosts                   /home/username/.ssh/known_hosts 
cp ./home/username/.ssh/id_rsa.pub                    /home/username/.ssh/id_rsa.pub 
cp ./etc/ssh/ssh_host_rsa_key.pub                     /etc/ssh/ssh_host_rsa_key.pub  
cp ./etc/ssh/ssh_host_dsa_key                         /etc/ssh/ssh_host_dsa_key 
cp ./etc/ssh/ssh_host_dsa_key.pub                     /etc/ssh/ssh_host_dsa_key.pub  
cp ./etc/ssh/ssh_host_ecdsa_key.pub                   /etc/ssh/ssh_host_ecdsa_key.pub 
cp ./etc/ssh/ssh_host_rsa_key                         /etc/ssh/ssh_host_rsa_key  
cp ./etc/ssh/ssh_host_ecdsa_key                       /etc/ssh/ssh_host_ecdsa_key 
cp ./etc/hostname                                     /etc/hostname  
cp ./etc/resolv.conf                                  /etc/resolv.conf  
cp ./etc/network/interfaces                           /etc/network/interfaces
  • Execute that newly created script, first check you are in /home/username/migrate
pwd
/home/username/migrate
  • We’ll run as root
sudo -s
  • Echo a file or two, to test BEFORE->AFTER
cat /etc/network/interfaces
...
cat /etc/hostname
morpheus-stg
  • Now, run the file
/bin/bash ./prepare.sh
  • they should be different :)
cat /etc/network/interfaces
...
cat /etc/hostname
morpheus.networkname.net
  • Make sure the /etc/hosts file reflects, and points at 127.0.0.1
127.0.0.1   localhost morpheus.networkname.net morpheus
  • You can use vim regex like so:
sudo vim /etc/hosts
:%s/morpheus-stg/morpheus/
:wq
  • Use apache command tools to disable the old site and enable the prod ones:
sudo ll /etc/apache/sites-available
...
-rw-r--r-- 1 root root 1052 Feb 20 19:19 /etc/apache2/sites-available/default
-rw-r--r-- 1 root root 7469 Feb  6  2012 /etc/apache2/sites-available/default-ssl
-rw-r--r-- 1 root root 1917 Feb 20 15:48 /etc/apache2/sites-available/ssl
  • Enable only ssl, and default (NOT ‘default-ssl‘)
sudo a2dissite
10-project.local.conf
sudo a2ensite
default ssl
  • Restart the server
sudo service apache2 restart

3. Decomission the original, use the new VM as the new Production

This is, again, specific to the way you might commission or decomission your web app