Moving home directory to a new drive

My current host has a unique feature in which it allows me to setup virtual machines easily. Since that is easily possible, I may want to someday switch to another operating system. So I wanted to split all my /home and configuration files out onto a separate drive. Which is entirely possible with my host.

The nice thing about linux systems is since they operate on open source software, things like configurations and setting things up are becoming less of a problem.  So If I set up my files correctly and use some correct symlinks, I could easily switch my operating system without missing a beat.

I will avoid discussing the details of getting the other drive setup on my machine. However, getting to working properly does take a little bit of work, all of which is easy.

Firstly, after I made sure the new drive exists in /dev, I simply created a folder to where I would mount the files.

$ mkdir /home-new

Now the directory exists, I simply just mount the drive to the directory.

$ mount /dev/xvdc /home-new

Doing some basic commands, I tested to ensure that the drive works and is functioning properly.  The next step involves copying files over.  However I had my site setup with permissions already and copying them would result in them being owned by root again.  Luckily the copy command has a argument that allows us to preserve that.

$ cp -Rp /home/* /home-new

Once that completed, I ensured that the files all worked on the new drive.  Next was to edit my /etc/fstab so the drive would mount correctly on reboots.  Simply put, I just copied the one for my root drive, changed the /dev device to the correct drive and the mount point to /home.  Just incase something went wrong, I shut off apache and moved /home to /home-old with a move command.

Now, I could of easily umount the /home-new drive and remount it on /home.  But just to ensure everything worked, I issued a reboot command and waited for my server to reboot.  After the reboot, I was able to see my site working again.  However I was not done yet.  My apache configuration files are still on the main drive.  An easy way for me to get around this is moving all my virtual host configuration files to my home folder and creating a symlink to them.

$ mkdir /home/configs
$ mv /etc/apache2/sites-available /home/configs
$ ln -s /home/configs/sites-available /etc/apache2/sites-available

This completes the move of my apache configs.  I modified the default configuration and have it containing things like port and other apache configuration changes.  I just simply repeated this for other configurations I changed and wish to have them transfer if I switch operating systems.

The only thing left to do is change where my mysql data is being stored.  Although I will work on not breaking that the first time around some other day 🙂

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.