image-backup.rst 7.0 KB

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