Knowledgebase: Operating Systems
Duplicating your Linux OS
Posted by Jude Oliver on 18 June 2010 04:19 PM

updated Jan18 2012

This an article to demonstrate the ease of which you can set up a timed replication of your running OS to another drive for disaster recovery under the Linux OS.  It is Blanchard Systems' recommendation that systems be set up with 2 independent (non-mirrored) drives, and to that effect we configure them this way unless directed by the customer. 


Be very careful in executing the commands in this article, as they do not offer any warning and WILL overwrite the entire contents of the output device with the contents of the input device!

Every system staged with a BSI Boot Disc has a sample crontab entry which runs at 3:30 AM Saturday, all you need do is uncomment that line and restart the cron service.  One of the main reasons we advocate this method is if you have 2 drives configured this way, you can set up a timed replication of the main system drive to the secondary drive - thus if your main system were to die due to a software issue or failure of your primary drive, you would be able to quickly switch drives and be back in business with a system that is an exact duplicate of your operating drive at the time you ran the replication.  If you were running two drives that were actively mirrored to each other, if you had a software failure on your main drive the issue would be replicated to your mirrored drive, resulting in you having two faulty drives.

For Linux the simplest method is to use the built in command dd, and to schedule it via a cron entry. This may take over an hour or two to run depending on the size of the drive to be replicated and how much data it contains. A smaller drive or one with more free space will replicate faster than the same size drive with less space free.


First, let's examine the dd command and its' syntax.  dd stands for "disk dupe" and is called with the main arguments:


"if"- stands for "input file." This is followed by the path to the file or file system you want to duplicate; for example if=/dev/sda would replicate the entire SCSI hard drive A, typically the first drive in the system. You could also point the "if" to a single partition (/dev/sda1) or a file (if=/symlnks/io/jobs/FileName), but for our purposes we would want to copy the entire drive, including all its' partitions.



"of" - stands for "output file." This is followed by the path where you want the "if" replicated to. This could be another drive or a folder on your storage system. For example, of=/dev/sdb would replicate whatever the "if" was to the SCSI hard drive "b", typically the second drive in a system. This of course could also be used to create an image of the hard drive onto your RAID (of=/mnt/RAID/TWiSTBackup.iso), but we want to replicate this to a physical device for ease of disaster recovery.


"bs" stands for block size. This tells the command to copy info over in chunks of this size, and this is the only other argument we suggest you use. We recommend it be set to 2048, as that helps increase the speed of the replication.


So, to summarize, the command we suggest you use is:


 dd if=/dev/sda of=/dev/sdb bs=4096 && dd if=/dev/sda of=/dev/sdb bs=512 count=1

 

You should always verify this setup before scheduling or initiating a dd - you can verify your drive structure by using the Linux command "fdisk".

fdisk -l will list all the partitions and drives physically attached to the system.  On a typical IBM system running Linux, it will return info like this:

 

Disk /dev/sda: 36.4 GB, 36401479680 bytes
255 heads, 63 sectors/track, 4425 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         131     1052226   83  Linux
/dev/sda2             132         653     4192965   82  Linux swap / Solaris
/dev/sda3             654        4425    30298590   83  Linux

Disk /dev/sdb: 36.4 GB, 36401479680 bytes
255 heads, 63 sectors/track, 4425 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System

This represents two drives, both of 36GB in size, which is very important as we want the output disk (of=) to be the same size or larger than the input disk (if=), with the /dev/sda having typical TWiST partitioning setup and the second drive, /dev/sdb, being blank

Once we have verified that we have a spare drive, one that is not already partitioned, mounted and or already in use, we can use those paths (/dev/sda and /dev/sdb) to create into a crontab entry.  The Linux operating system uses a process called "cron" to run scripts or processes at set times.  For this article, we are going to limit ourself to a "simple" entry into the crontab file.  The file /etc/cronttab should already have several lines in it; do not touch those!  We will add a new line at the end of the file using the text editor of your choice (vi, vim, gedit, etc.) as the root user.  The crontab uses a structure of 7 columns to represent minute, hour, day, month, weekday, which user to run the timed command as, and the command itself.  Take for example this entry:
02 4 * * * root run-parts /etc/cron.daily

Column 1, 02 in our sample, represents minutes
Column 2, 4 in our sample, represents hour, using a 24 hour clock, so 23 is 11:00 PM, and this command will run at 11:02 PM.
Column 3, * in our sample, represents Day
Column 4, * in our sample, represents Month
Column 5, * in our sample, represents Day of the week, this can use numbers or letter codes for the day of the week.  In our example, because the wildcard * is used, this command runs every day.
Column  6, root in our sample, represents the user to run the command as.
Column  7, /etc/cron.daily in our sample, represents the command to run.


So if we wanted to run a "dd" command every Saturday at 3:30 AM we would make this entry:

30 3 * * Sat root  dd if=/dev/sda of=/dev/sdb bs=4096 && dd if=/dev/sda of=/dev/sdb bs=512 count=1

You will then need to restart the cron service to get Linux to update its' list of things to run at scheduled times:

service cron restart

and you are done.


(0 votes)
This article was helpful
This article was not helpful

Comments (0)
Post a new comment 
 
Full Name:
Email:
Comments:
Help Desk Software by Kayako Fusion