2

I have a 100 GB image file which is created using dd. This image file is mounted via a loop device as read-write.

I want to store a backup of this image file at a remote location. I have a script which compresses the image file (using zip) and copies the zip file to the remote storage (scheduled using cron).

Now, my question is: should I make my script unmount the image file before zipping it? (If some read-write operation is in progress, then I would not be able to unmount it and the script would thus fail).

Or is it okay to zip it when it is still mounted? What happens if there is some write operation in progress when the zipping starts? Would the image file still be consistent?

I would appreciate some insights.

Anjan
  • 307
  • 1
  • 2
  • 14

1 Answers1

4

If you copy a filesystem image while it is mounted read&write, you will get an inconsistent copy. This is true whether the image is a file underlying a loop device or, more simply, a normal block device. It is not okay to zip it while it is still mounted.

By the way, zip seems like an unlikely choice for compressing a single large file. The streaming nature of the more commonly used tools gzip or bzip2 seems more appropriate.

Instead of accessing a live image, you will want to take a snapshot of it instead. One option would be to use a LVM LV instead of a file-backed loopback device. Then you could take an LVM snapshot of the LV before compressing it. LVM's snapshotting capability automatically communicates with the filesystem to ensure a consistent snapshot, if the filesystem supports it (xfs and, I think, ext4 support this).

Celada
  • 6,200
  • 1
  • 21
  • 17
  • To use LVM, I need to re-partition my physical disk to setup LVM PVs and LVs on the physical disk, right? Or am I missing something here? – Anjan Oct 25 '12 at 18:41
  • Well, technically you could create an PVM PV on top of a file-backed loop device, and create a VG and some LVs in that, but that is even quite a lot more overhead than you already have! I do recommend using LVM directly on your physical disk, especially if you deal with filesystem images as large as 100GB and their backups, because creating those as LVs would save the overhead of using file-backed loopbacks. In fact on Linux servers, I *always* use LVM because it's so much more flexible and user friendly than disk partitions. But you're right: you have to make that decision from the start. – Celada Oct 25 '12 at 19:27
  • without LVM, is there some way to consistently suspend the r/w operation to make the copy manually? I think, may be, the `mount` "remount" option could be used ? http://askubuntu.com/a/175742/46437 – Aquarius Power Jan 21 '15 at 20:20
  • 1
    @AquariusPower yes, you can use `fsfreeze` to temporarily lock the filesystem in a consistent state. Without a snapshot, you must wait until the copy is finished before you thaw the filesystem. With a snapshot, you can thaw as soon as the snapshot is taken. `fsfreeze` uses the same interface as LVM uses when it makes its snapshots. – Celada Jan 22 '15 at 02:15
  • so I guess, we would need to have total control over what applications are doing on that filesystem to not break the applications funcionality either right? I thought `kill -SIGSTOP` could do the trick in this case; but overall, I think we would just be doing what LVM already does in a much better way; but it is good to know about `fsfreeze`, thanks! – Aquarius Power Jan 22 '15 at 20:19
  • 1
    @AquariusPower `kill -SIGSTOP` is different. It does stop a process from running, but it doesn't make the filesystem promise to write outstanding data to disk. `kill -SIGSTOP` of all processes that could potentially write to the affected filesystem plus running `sync` would come closer, but `fsfreeze` is both easier and offers a more robust guarantee. – Celada Jan 23 '15 at 02:01