image-backup.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. .. include:: ../global.rst.inc
  2. .. highlight:: none
  3. Backing up entire disk images
  4. =============================
  5. Backing up disk images can still be efficient with Borg because its `deduplication`_
  6. technique makes sure only the modified parts of the file are stored. Borg also has
  7. optional simple sparse file support for extract.
  8. Decreasing the size of image backups
  9. ------------------------------------
  10. Disk images are as large as the full disk when uncompressed and might not get much
  11. smaller post-deduplication after heavy use because virtually all file systems don't
  12. actually delete file data on disk but instead delete the filesystem entries referencing
  13. the data. Therefore, if a disk nears capacity and files are deleted again, the change
  14. will barely decrease the space it takes up when compressed and deduplicated. Depending
  15. on the filesystem, there are several ways to decrease the size of a disk image:
  16. Using ntfsclone (NTFS, i.e. Windows VMs)
  17. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  18. ``ntfsclone`` can only operate on filesystems with the journal cleared (i.e. turned-off
  19. machines), which somewhat limits its utility in the case of VM snapshots. However, when
  20. it can be used, its special image format is even more efficient than just zeroing and
  21. deduplicating. For backup, save the disk header and the contents of each partition::
  22. HEADER_SIZE=$(sfdisk -lo Start $DISK | grep -A1 -P 'Start$' | tail -n1 | xargs echo)
  23. PARTITIONS=$(sfdisk -lo Device,Type $DISK | sed -e '1,/Device\s*Type/d')
  24. dd if=$DISK count=$HEADER_SIZE | borg create repo::hostname-partinfo -
  25. echo "$PARTITIONS" | grep NTFS | cut -d' ' -f1 | while read x; do
  26. PARTNUM=$(echo $x | grep -Eo "[0-9]+$")
  27. ntfsclone -so - $x | borg create repo::hostname-part$PARTNUM -
  28. done
  29. # to back up non-NTFS partitions as well:
  30. echo "$PARTITIONS" | grep -v NTFS | cut -d' ' -f1 | while read x; do
  31. PARTNUM=$(echo $x | grep -Eo "[0-9]+$")
  32. borg create --read-special repo::hostname-part$PARTNUM $x
  33. done
  34. Restoration is a similar process::
  35. borg extract --stdout repo::hostname-partinfo | dd of=$DISK && partprobe
  36. PARTITIONS=$(sfdisk -lo Device,Type $DISK | sed -e '1,/Device\s*Type/d')
  37. borg list --format {archive}{NL} repo | grep 'part[0-9]*$' | while read x; do
  38. PARTNUM=$(echo $x | grep -Eo "[0-9]+$")
  39. PARTITION=$(echo "$PARTITIONS" | grep -E "$DISKp?$PARTNUM" | head -n1)
  40. if echo "$PARTITION" | cut -d' ' -f2- | grep -q NTFS; then
  41. borg extract --stdout repo::$x | ntfsclone -rO $(echo "$PARTITION" | cut -d' ' -f1) -
  42. else
  43. borg extract --stdout repo::$x | dd of=$(echo "$PARTITION" | cut -d' ' -f1)
  44. fi
  45. done
  46. .. note::
  47. When backing up a disk image (as opposed to a real block device), mount it as
  48. a loopback image to use the above snippets::
  49. DISK=$(losetup -Pf --show /path/to/disk/image)
  50. # do backup as shown above
  51. losetup -d $DISK
  52. Using zerofree (ext2, ext3, ext4)
  53. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  54. ``zerofree`` works similarly to ntfsclone in that it zeros out unused chunks of the FS,
  55. except it works in place, zeroing the original partition. This makes the backup process
  56. a bit simpler::
  57. sfdisk -lo Device,Type $DISK | sed -e '1,/Device\s*Type/d' | grep Linux | cut -d' ' -f1 | xargs -n1 zerofree
  58. borg create --read-special repo::hostname-disk $DISK
  59. Because the partitions were zeroed in place, restoration is only one command::
  60. borg extract --stdout repo::hostname-disk | dd of=$DISK
  61. .. note:: The "traditional" way to zero out space on a partition, especially one already
  62. mounted, is to simply ``dd`` from ``/dev/zero`` to a temporary file and delete
  63. it. This is ill-advised for the reasons mentioned in the ``zerofree`` man page:
  64. - it is slow
  65. - it makes the disk image (temporarily) grow to its maximal extent
  66. - it (temporarily) uses all free space on the disk, so other concurrent write actions may fail.
  67. Virtual machines
  68. ----------------
  69. If you use non-snapshotting backup tools like Borg to back up virtual machines, then
  70. the VMs should be turned off for the duration of the backup. Backing up live VMs can
  71. (and will) result in corrupted or inconsistent backup contents: a VM image is just a
  72. regular file to Borg with the same issues as regular files when it comes to concurrent
  73. reading and writing from the same file.
  74. For backing up live VMs use filesystem snapshots on the VM host, which establishes
  75. crash-consistency for the VM images. This means that with most file systems (that
  76. are journaling) the FS will always be fine in the backup (but may need a journal
  77. replay to become accessible).
  78. Usually this does not mean that file *contents* on the VM are consistent, since file
  79. contents are normally not journaled. Notable exceptions are ext4 in data=journal mode,
  80. ZFS and btrfs (unless nodatacow is used).
  81. Applications designed with crash-consistency in mind (most relational databases like
  82. PostgreSQL, SQLite etc. but also for example Borg repositories) should always be able
  83. to recover to a consistent state from a backup created with crash-consistent snapshots
  84. (even on ext4 with data=writeback or XFS). Other applications may require a lot of work
  85. to reach application-consistency; it's a broad and complex issue that cannot be explained
  86. in entirety here.
  87. Hypervisor snapshots capturing most of the VM's state can also be used for backups and
  88. can be a better alternative to pure file system based snapshots of the VM's disk, since
  89. no state is lost. Depending on the application this can be the easiest and most reliable
  90. way to create application-consistent backups.
  91. Borg doesn't intend to address these issues due to their huge complexity and
  92. platform/software dependency. Combining Borg with the mechanisms provided by the platform
  93. (snapshots, hypervisor features) will be the best approach to start tackling them.