Difference between revisions of "System backup"

From ArchWiki
Jump to navigation Jump to search
(→‎With a single command: rw to emphasize gvfs must be excluded if the package is installed)
(update interlanguage links)
Tag: wiki-scripts
 
(73 intermediate revisions by 26 users not shown)
Line 1: Line 1:
[[Category:System recovery]]
+
[[Category:Backup]]
[[cs:Full System Backup with rsync]]
+
[[cs:Full system backup with rsync]]
[[zh-tw:Full System Backup with rsync]]
+
[[es:System backup]]
 +
[[ja:Rsync によるフルシステムバックアップ]]
 +
[[pt:System backup]]
 +
[[zh-hans:System backup]]
 +
[[zh-hant:Full system backup with rsync]]
 
{{Related articles start}}
 
{{Related articles start}}
{{Related|Backup programs}}
+
{{Related|Synchronization and backup programs}}
{{Related|rsync}}
+
{{Related|System maintenance#Backup}}
 +
{{Related|Disk cloning}}
 +
{{Related|Migrate installation to new hardware}}
 +
{{Related|File recovery}}
 
{{Related articles end}}
 
{{Related articles end}}
This article is about using [[rsync]] to transfer a copy of your "/" tree, excluding a few select folders. This approach is considered to be better than [[disk cloning]] with {{ic|dd}} since it allows for a different size, partition table and filesystem to be used, and better than copying with {{ic|cp -a}} as well, because it allows greater control over file permissions, attributes, Access Control Lists (ACLs) and extended attributes. [http://www.bestbits.at/acl/about.html]
 
  
Either method will work even while the system is running. Since it's going to take a while, you may freely browse the web during this time. Worst case scenario you won't get the same opened tabs when you restore the backup (or boot from it) because they weren't saved. Not a big deal.
+
It is important to regularly backup system and user data stored for example in {{ic|/etc}}, {{ic|/home}}, {{ic|/var}} and for server installations, also {{ic|/srv}}.
  
== With a single command ==
+
== Using Btrfs snapshots ==
  
As root, run:
+
See [[Btrfs#Snapshots]] and [[Snapper]].
  
# rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /* ''/path/to/backup/folder''
+
== Using LVM snapshots ==
  
Using the {{ic|-aAX}} set of options, the files are transferred in archive mode, ensuring that symbolic links, devices, permissions and ownerships, modification times, [[ACL]]s and extended attributes are preserved.
+
See [[LVM#Snapshots]] and [[Create root filesystem snapshots with LVM]].
  
The {{ic|--exclude}} option will cause files that match the given patterns to be excluded. The contents of {{ic|/dev}}, {{ic|/proc}}, {{ic|/sys}}, {{ic|/tmp}} and {{ic|/run}} were excluded because they are populated at boot (while the folders themselves are ''not'' created), {{ic|/lost+found}} is filesystem-specific. Quoting the exclude patterns will avoid expansion by [[shell]], which is necessary e.g. when backing up over [[SSH]].
+
== Using rsync ==
  
{{Note|
+
See [[rsync#As a backup utility]].
* If you plan on backing up your system somewhere other than {{ic|/mnt}} or {{ic|/media}}, do not forget to add it to the list of exclude patterns to avoid an infinite loop.
 
* If there are any bind mounts in the system, they should be excluded as well, so that the bind mounted contents is copied only once.
 
* If you use a [[Swap#Swap_file|swap file]], make sure to exclude it as well.
 
* Consider also if you want to backup the {{ic|/home/}} folder. If it contains your data, it might be considerably larger than the system. Otherwise consider excluding unimportant subdirectories such as  {{ic|/home/*/.thumbnails/*}}, {{ic|/home/*/.cache/mozilla/*}}, {{ic|/home/*/.cache/chromium/*}}, {{ic|/home/*/.local/share/Trash/*}}, depending on software installed on the system. If [[GVFS]] is installed, {{ic|/home/*/.gvfs}} must be excluded to prevent rsync errors.
 
}}
 
  
You may want to include additional [[rsync]] options, such as the following (see {{ic|man rsync}} for the full list):
+
== Using tar ==
  
* If you are heavy user of hardlinks, you might consider adding the {{ic|-H}} option, which is turned off by default as memory expensive, but nowadays it should be no problem on most of modern machines. There are a lot of hard links below the {{ic|/usr/}} folder, which save disk space.
+
See [[Full system backup with tar]].
* You may want to add rsync's {{ic|--delete}} option if you are running this multiple times to the same backup folder.
 
* If you use any sparse files, such as virtual disks, [[Docker]] images and similar, you should add the {{ic|-S}} option.
 
* The {{ic|--numeric-ids}} option will disable mapping of user and group names, numeric group and user IDs will be transfered instead. This is useful when backing up over [[SSH]] or when using a live system to backup different system disk.
 
  
== Automated ==
+
== Using SquashFS ==
  
See [[Backup_programs#Rsync-type_backups]].
+
See [[Full system backup with SquashFS]].
  
== Boot requirements ==
+
== Bootable backup ==
  
Having a bootable backup can be useful in case the filesystem becomes corrupt or if an update breaks the system. The backup can also be used as a test bed for updates, with the [testing] repo enabled, etc. If you transferred the system to a different partition or drive and you want to boot it, the process is as simple as updating the backup's {{ic|/etc/fstab}} and your bootloader's configuration file.
+
Having a bootable backup can be useful in case the filesystem becomes corrupt or if an update breaks the system. The backup can also be used as a test bed for updates, with the ''testing'' repo enabled, etc. If you transferred the system to a different partition or drive and you want to boot it, the process is as simple as updating the backup's {{ic|/etc/fstab}} and your bootloader's configuration file.
 +
 
 +
This section assumes that you backed up the system to another drive or partition, that your current bootloader is working fine, and that you want to boot from the backup as well.
  
 
=== Update the fstab ===
 
=== Update the fstab ===
  
Without rebooting, edit the backup's [[fstab]] to reflect the changes:
+
Without rebooting, edit the backup's [[fstab]] by commenting out or removing any existing entries. Add one entry for the partition containing the backup like the example here:
{{hc|/path/to/backup/etc/fstab|2=
 
tmpfs        /tmp          tmpfs    nodev,nosuid            0  0
 
 
 
<font color=#888888><i>/dev/sda1    /boot        ext2      defaults                0  2
 
/dev/sda5    none          swap      defaults                0  0
 
/dev/sda6    /            ext4      defaults                0  1
 
/dev/sda7    /home        ext4      defaults                0  2</i></font>}}
 
  
Because rsync has performed a recursive copy of the ''entire'' root filesystem, all of the {{ic|sda}} mountpoints are problematic and booting the backup will fail. In this example, all of the offending entries are replaced with a single one:
+
/dev/sda''X''    /            ''ext4''     defaults                0  1
 
 
{{hc|/path/to/backup/etc/fstab|
 
tmpfs        /tmp          tmpfs    nodev,nosuid            0  0
 
 
 
/dev/'''sdb1'''    /            ext4      defaults                0  1}}
 
  
 
Remember to use the proper device name and filesystem type.
 
Remember to use the proper device name and filesystem type.
  
 
=== Update the bootloader's configuration file ===
 
=== Update the bootloader's configuration file ===
 
This section assumes that you backed up the system to another drive or partition, that your current bootloader is working fine, and that you want to boot from the backup as well.
 
  
 
For [[Syslinux]], all you need to do is duplicate the current entry, except pointing to a different drive or partition.
 
For [[Syslinux]], all you need to do is duplicate the current entry, except pointing to a different drive or partition.
Line 70: Line 56:
 
{{Tip|Instead of editing {{ic|syslinux.cfg}}, you can also temporarily edit the menu during boot. When the menu shows up, press the {{ic|Tab}} key and change the relevant entries. Partitions are counted from one, drives are counted from zero.}}
 
{{Tip|Instead of editing {{ic|syslinux.cfg}}, you can also temporarily edit the menu during boot. When the menu shows up, press the {{ic|Tab}} key and change the relevant entries. Partitions are counted from one, drives are counted from zero.}}
  
For [[GRUB]], it's recommended that you automatically [[GRUB#Generating_main_configuration_file|re-generate the main configuration file]].
+
For [[GRUB]], it is recommended that you automatically [[GRUB#Generate_the_main_configuration_file|re-generate the main configuration file]].  If you want to freshly install all GRUB files to somewhere other than {{ic|/boot}}, such as {{ic|/mnt/newroot/boot}}, use the {{ic|--boot-directory}} flag.
  
Also verify the new menu entry in {{ic|/boot/grub/grub.cfg}}. Make sure the UUID is matching the new partition, otherwise it could still boot the old system. Find the UUID of a partition as follows:
+
Also verify the new menu entry in {{ic|/boot/grub/grub.cfg}}. Make sure the UUID is matching the new partition, otherwise it could still boot the old system. Find the UUID of a partition with [[lsblk]]:
  
  # lsblk -no NAME,UUID /dev/sdb3
+
  $ lsblk -no NAME,UUID /dev/sd''XY''
  
where you substitute the desired partition for /dev/sdb3. To list the UUID's of partitions grub thinks it can boot, use grep:
+
where {{ic|/dev/sd''XY''}} is the desired partition (e.g. {{ic|/dev/sdb3}}). To list the UUIDs of partitions GRUB thinks it can boot, use ''grep'':
  
 
  # grep UUID= /boot/grub/grub.cfg
 
  # grep UUID= /boot/grub/grub.cfg
  
If the one you found from lsblk isn't found by grep, then grub-mkconfig didn't work. Most likely, you will have to [[Change root]] into the duplicate file system and then use [[mkinitcpio]]. For example, if you had used rsync to duplicate root on /dev/sdb3 then change root and use mkinitcpio as follows:
+
=== First boot ===
 
 
# mkdir /mnt/arch
 
# mount /dev/sdb3 /mnt/arch
 
# cd /mnt/arch
 
# mount -t proc proc proc/
 
# mount --rbind /sys sys/
 
# mount --rbind /dev dev/
 
# chroot /mnt/arch /bin/bash
 
# mkinitcpio -p linux
 
 
 
After exiting, generate grub.cfg again and verify the new UUID is included.
 
 
 
== First boot ==
 
  
 
Reboot the computer and select the right entry in the bootloader. This will load the system for the first time. All peripherals should be detected and the empty folders in {{ic|/}} will be populated.
 
Reboot the computer and select the right entry in the bootloader. This will load the system for the first time. All peripherals should be detected and the empty folders in {{ic|/}} will be populated.
  
 
Now you can re-edit {{ic|/etc/fstab}} to add the previously removed partitions and mount points.
 
Now you can re-edit {{ic|/etc/fstab}} to add the previously removed partitions and mount points.
 
If you transferred the data from HDD to SSD (solid state drive), don't forget to activate TRIM. Also consider using HDD and tmpfs mount points to reduce SSD wearing - see [[Maximizing performance#Relocate_files_to_tmpfs|Relocate files to tmpfs]] and [[Solid_State_Drives#Tips_for_Minimizing_SSD_Read.2FWrites|Tips for Minimizing SSD Read & Writes]].
 
 
{{Note|You may have to reboot again in order to get all services and daemons working correctly. Personally, pulseaudio would not initialise because of a module loading error. I restarted the dbus.service to make it work.}}
 
 
== See also ==
 
 
# [http://blog.pointsoftware.ch/index.php/howto-local-and-remote-snapshot-backup-using-rsync-with-hard-links/ Howto – local and remote snapshot backup using rsync with hard links] Includes file deduplication with hard-links, MD5 integrity signature, 'chattr' protection, filter rules, disk quota, retention policy with exponential distribution (backups rotation while saving more recent backups than older)
 

Latest revision as of 07:26, 19 August 2019

It is important to regularly backup system and user data stored for example in /etc, /home, /var and for server installations, also /srv.

Using Btrfs snapshots

See Btrfs#Snapshots and Snapper.

Using LVM snapshots

See LVM#Snapshots and Create root filesystem snapshots with LVM.

Using rsync

See rsync#As a backup utility.

Using tar

See Full system backup with tar.

Using SquashFS

See Full system backup with SquashFS.

Bootable backup

Having a bootable backup can be useful in case the filesystem becomes corrupt or if an update breaks the system. The backup can also be used as a test bed for updates, with the testing repo enabled, etc. If you transferred the system to a different partition or drive and you want to boot it, the process is as simple as updating the backup's /etc/fstab and your bootloader's configuration file.

This section assumes that you backed up the system to another drive or partition, that your current bootloader is working fine, and that you want to boot from the backup as well.

Update the fstab

Without rebooting, edit the backup's fstab by commenting out or removing any existing entries. Add one entry for the partition containing the backup like the example here:

/dev/sdaX    /             ext4      defaults                 0   1

Remember to use the proper device name and filesystem type.

Update the bootloader's configuration file

For Syslinux, all you need to do is duplicate the current entry, except pointing to a different drive or partition.

Tip: Instead of editing syslinux.cfg, you can also temporarily edit the menu during boot. When the menu shows up, press the Tab key and change the relevant entries. Partitions are counted from one, drives are counted from zero.

For GRUB, it is recommended that you automatically re-generate the main configuration file. If you want to freshly install all GRUB files to somewhere other than /boot, such as /mnt/newroot/boot, use the --boot-directory flag.

Also verify the new menu entry in /boot/grub/grub.cfg. Make sure the UUID is matching the new partition, otherwise it could still boot the old system. Find the UUID of a partition with lsblk:

$ lsblk -no NAME,UUID /dev/sdXY

where /dev/sdXY is the desired partition (e.g. /dev/sdb3). To list the UUIDs of partitions GRUB thinks it can boot, use grep:

# grep UUID= /boot/grub/grub.cfg

First boot

Reboot the computer and select the right entry in the bootloader. This will load the system for the first time. All peripherals should be detected and the empty folders in / will be populated.

Now you can re-edit /etc/fstab to add the previously removed partitions and mount points.