LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-13-2008, 06:39 AM   #16
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
Fun with dd and losetup.


Here is an example run through using my /boot partition.
Code:
df /boot
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sdc1               155543     45050    102463  31% /boot
hpmedia:~ # dd if=/dev/zero of=/boot/zerofile bs=1024 count=$((102462))
102462+0 records in
102462+0 records out
104921088 bytes (105 MB) copied, 1.61883 s, 64.8 MB/s
hpmedia:~ # rm /boot/zerofile
hpmedia:~ # dd if=/dev/sdc1 bs=512 | gzip >boot.img
321237+0 records in
321237+0 records out
164473344 bytes (164 MB) copied, 10.9868 s, 15.0 MB/s
hpmedia:~ # ls -l boot.img
-rw-r--r-- 1 root root 31089205 Sep 13 05:26 boot.img
hpmedia:~ # ls -lh boot.img
-rw-r--r-- 1 root root 30M Sep 13 05:26 boot.img
I subtracted 1 from the df results and used that for dd's count, also using the same block size as the df results. Since I just need to save the compressed version, I can pipe the output of dd through gzip. I should have named it boot.img.gz however.

To restore from this image backup, I just need to reverse the process:
zcat boot.img | dd bs=512 of=/dev/sdc1

To do this for an entire disk, you would use the /dev/sda or /dev/sdc, etc device instead.

There is one disadvantage however. If your image file is compressed you can't mount it using a loop device. Even if you create an uncompressed image, it may be a good idea to zero out the free space so you aren't accidentally saving the contents of a deleted confidential file.

---

For an uncompressed disk image backup, suppose that a partition is damaged or a number of files are deleted accidentally. ( I'll admit to doing this myself ) You can mount a partition that is inside your uncompressed image file and use that to copy back the missing contents or partition image. The trick is to use "fdisk -lu <imagefile>" to determine where the partition starts in 512 byte blocks. Then take the starting block * 512 and use that as the offset for the losetup command.

----
Here is an example. I created an empty file, partitioned and created an ext3 filesystem on each partition. In other words, I created an image of a disk from scratch to work with. Now let's mount it from the disk image.
Code:
fdisk -lu sdc.img
You must set cylinders.
You can do this from the extra functions menu.

Disk sdc.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x71534f6e

  Device Boot      Start         End      Blocks   Id  System
sdc.img1              63      481949      240943+  83  Linux
sdc.img2          481950      803249      160650   83  Linux
hpmedia:~ # losetup -fs sdc.img -o $((63*512))
/dev/loop0
hpmedia:~ # losetup -fs sdc.img -o $((481950*512))
/dev/loop1
hpmedia:~ # fsck.ext3 /dev/loop0
e2fsck 1.40.8 (13-Mar-2008)
/dev/loop0: clean, 18/99960 files, 145366/398988 blocks
hpmedia:~ # fsck.ext3 /dev/loop1
e2fsck 1.40.8 (13-Mar-2008)
/dev/loop1: clean, 13/39520 files, 13824/158048 blocks
hpmedia:~ # mount /dev/loop0 /mnt/sdc_img_p1
hpmedia:~ # mount /dev/loop0 /mnt/sdc_img_p2
hpmedia:~ # ls /mnt/sdc_img_p1
NVIDIA-Gelato-Linux-x86-2.0R4-pkg.run  NVIDIA-Linux-x86_64-173.14.05-pkg0.run  NVIDIA-Linux-x86_64-173.14.05-pkg2.run  NVIDIA_kernel-1.0-4499.tar.gz
NVIDIA-Linux-x86_64-1.0-9746-pkg2.run  NVIDIA-Linux-x86_64-173.14.05-pkg1.run  NVIDIA_GLX-1.0-4499.tar.gz              lost+found
hpmedia:~ # ls /mnt/sdc_img_p2
NVIDIA-Gelato-Linux-x86-2.0R4-pkg.run  NVIDIA-Linux-x86_64-173.14.05-pkg0.run  NVIDIA-Linux-x86_64-173.14.05-pkg2.run  NVIDIA_kernel-1.0-4499.tar.gz
NVIDIA-Linux-x86_64-1.0-9746-pkg2.run  NVIDIA-Linux-x86_64-173.14.05-pkg1.run  NVIDIA_GLX-1.0-4499.tar.gz              lost+found
So if you created an image of an identical machine, you could mount the partitions on the image and simply copy the files over to the partitions of your new disk if the partitions are a different size, or use dd to copy the entire disk image if the disks & partitions are identical.

Suppose you want to use an image to quickly install to a number of identical workstations. You could set up the first two nearly identically, except for host name and the user's name so that each can operate on the network. Then create an image of the first one. Mount the image (on an external drive I presume) and perform a diff of the /etc/ and /home/ partitions. This is a way to determine which files are different. You could try creating a script that takes the few parameters that will differ, and after copying the disk image to the hard drive, run the script to apply the changes you need.

Here is an odd hybrid idea. Mount the partitions of your image. Create an initial full tar backup. Then since the timestamps are all identical with the machine you installed the image on, you can use it as your first full backup and only need to run a differential backup on the actual machine after making the changes needed to suit your needs. This may work better than the above patch idea.

Last edited by jschiwal; 09-13-2008 at 06:46 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Radiant Data Launches 64-bit Linux HA Replicating File System LXer Syndicated Linux News 0 04-07-2007 11:46 AM
Linux-based replicating file server options rolliehawk Linux - Enterprise 4 03-01-2007 04:35 AM
replicating mysql on windows to mysql on linux sunethj Linux - Server 0 01-22-2007 08:40 PM
replicating the lycoris 'my linux system'? aesahaettr Linux - Distributions 2 04-29-2004 05:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration