Introduction
This article is dedicated to documenting methods of performing drive imaging (also called bare metal backups, or disk cloning). Drive imaging is a complete copy of all information on a drive, necessary to restore all of the data or entire operating system on a drive to the same state it was when the image was created. This is different from imaging a partition, where one is making a copy of an individual partition that resides on a drive, or backing up individual files and folders.
Please ensure you are comfortable with the information discussed before proceeding. Improperly executing a command may result in partial or complete data loss. Please double- or even triple-check your target device to avoid such catastrophic loss.
Preparations
- Exclusive access to the drive being imaged (i.e. the drive being imaged shouldn’t be mounted). Live operating system imaging methods, for example, physical-to-virtual (P2V), virtual-to-virtual (V2V), etc. are not covered here.
- The location (remote file share, external USB drive, internal drive, etc.) where the drive image is being backed up to should have the same or more free space then that of the drive being imaging. For example, if you are imaging a 160GB drive, you should have 160GB or more free space to back up to.
- The filesystem of the backup location needs to support the filesize necessary to backup the image as one file.
- An environment to perform the drive imaging. This can be a live environment where one images the data of the computers internal drive.
dd is a universal command line program used for low level copying of data. It will copy the entire drive, even if the used data is only consuming 10% of the beginning of the drive. For example, dd’ing a 100GB drive, where all the data is at the beginning, and is only 10GB is being consumed, the resulting file will be 100GB in size.
In order to find out which drive to clone, open a terminal and execute:
The output of the command will list each hard drive (ex. /dev/sda).
Backup with dd
The following example will create a drive image of /dev/sda, the image will be backed up to an external drive, and compressed. For example, one may use bzip2 for maximum compression:
sudo dd if=/dev/sda | bzip2 > /media/usb/image.bz2
However, one may use this same concept to change the compression type (gzip, zip, etc.) to one that best suites your needs (higher compression speed, preferred compression format, etc.).
Restoring a drive image
To restore a drive image, one will want to boot into a live environment. Restoration is quite simple, and really just involves reversing the if and of values. This will tell dd to overwrite the drive with the data that is stored in the file. Ensure the image file isn’t stored on the drive you’re restoring to. If you do this, eventually during the operation dd will overwrite the image file, corrupting it and your drive.
To restore the drive above:
bzcat /media/usb/image.bz2 | dd of=/dev/sda
When restoring the whole drive, the system will not automatically create the devices (/dev/sda1, /dev/sda2, etc.). Reboot to ensure automatic detection.
If you restored Ubuntu to a new drive, and the UUIDs (see UsingUUID for more) changed, then you must change the bootloader and the mount points. One will want to edit the following via a terminal:
sudo nano /boot/grub/menu.lst sudo nano /etc/fstab
To know what the new UUIDs for your drives are, use the following command:
From this list, you can cross-reference the information with that of fdisk to know which drive is which. Then simply update the UUIDs in both GRUB and fstab files.
Clone Drive
Clone the contents of a whole hard drive onto another completely different drive
Clonezilla
https://launchpad.net/ubuntu/+source/clonezilla is another modern imaging solution. For more on it, please see the Clonezilla homepage.
DriveImaging (последним исправлял пользователь qiii 2015-09-28 17:55:23)
The material on this wiki is available under a free license, see Copyright / License for details
You can contribute to this wiki, see Wiki Guide for details
Full system backup with tar
Reason: Uses a half-baked script instead of explaining basic options and referring to the manual, duplication with articles such as Help:Reading, numerous Help:Style issues (Discuss in Talk:Full system backup with tar)
This article will show you how to do a full system backup with tar.
Backing up with tar has the advantages of using compression that can help save disk space, and simplicity. The process only requires several steps, they are:
- Boot from a LiveCD
- Change root to the Linux install
- Mount additional (if any) partitions/drives
- Add exclusions
- Use the backup script to backup
To minimize downtime the backup can alternatively be performed on a running system using LVM snapshots, if all filesystems reside on LVM volumes.
Boot with LiveCD
Many Linux bootable CDs, USBs. have the ability to let you change root to your install. While changing root is not necessary to do a backup, it provides the ability to just run the script without need to transfer it to a temporary drive or having to locate it on the filesystem. The Live medium must be of the same architecture that your Linux install currently is (i.e. i686 or x86_64).
Changing root
First you should have a scripting environment set up on your current Linux install. If you do not know what that is, it means that you are able to execute any scripts that you may have as if they are regular programs. If you do not, see this article on how to do that. What you will need to do next is change root, to learn more about what changing root is, read this. When you change root, you do not need to mount any temporary file systems ( /proc , /sys , and /dev ). These temporary file systems get populated on boot and you actually do not want to backup them up because they can interfere with the normal (and necessary) population process which can change on any upgrade. To change root, you will need to mount your current Linux installs root partition. For example:
# mkdir /mnt/arch # mount /dev/your-partition-or-drive
Use fdisk -l to discover you partitions and drives. Now chroot:
# cd /mnt/arch # chroot . /bin/bash
Warning: Do not use arch-chroot to chroot into the target system — the backup process will fail as it will try to back up temporary file systems, all system memory and other interesting things. Use plain chroot instead.
This example obviously uses bash but you can use other shells if available. Now you will be in your scripted environment (this is provided that you have your ~/.bashrc sourced on entry):
# If using bash, source the local .bashrc source ~/.bashrc
Mount other partitions
Other partitions that you use (if any) will need to be mounted in their proper places (e.g. if you have a separate /home partition).
Exclude file
tar has the ability to ignore specified files and directories. The syntax is one definition per line. tar also has the capability to understand regular expressions (regexps). For example:
# Not old backups /opt/backup/arch-full* # Not temporary files /tmp/* # Not the cache for pacman /var/cache/pacman/pkg/ .
Backup script
Backing up with bsdtar is straight-forward process. Here is a basic script that can do it and provides a couple checks. You will need to modify this script to define your backup location, and exclude file (if you have one), and then just run this command after you have chroot ed and mounted all your partitions. Note that GNU tar with —xattrs will not preserve extended attributes.
#!/bin/bash # full system backup # Backup destination backdest=/opt/backup # Labels for backup name #PC=$ pc=pavilion distro=arch type=full date=$(date "+%F") backupfile="$backdest/$distro-$type-$date.tar.gz" # Exclude file location prog=$ # Program name from filename excdir="/home//.bin/root/backup" exclude_file="$excdir/$prog-exc.txt" # Check if chrooted prompt. echo -n "First chroot from a LiveCD. Are you ready to backup? (y/n): " read executeback # Check if exclude file exists if [ ! -f $exclude_file ]; then echo -n "No exclude file exists, continue? (y/n): " read continue if [ $continue == "n" ]; then exit; fi fi if [ $executeback = "y" ]; then # -p, --acls and --xattrs store all permissions, ACLs and extended attributes. # Without both of these, many programs will stop working! # It is safe to remove the verbose (-v) flag. If you are using a # slow terminal, this can greatly speed up the backup process. # Use bsdtar because GNU tar will not preserve extended attributes. bsdtar --exclude-from=$exclude_file --acls --xattrs -cpvzf $backupfile / fi
Restoring
To restore from a previous backup, mount all relevant partitions, change the current working directory to the root directory, and execute
$ bsdtar --acls --xattrs -xpzf backupfile
replacing backupfile with the backup archive. Removing all files that had been added since the backup was made must be done manually. Recreating the filesystem(s) is an easy way to do this.
Backup with parallel compression
To back up using parallel compression (SMP), use pbzip2 (Parallel bzip2):
# bsdtar -cvf /path/to/chosen/directory/etc-backup.tar.bz2 -I pbzip2 /etc
Store etc-backup.tar.bz2 on one or more offline media, such as a USB stick, external hard drive, or CD-R. Occasionally verify the integrity of the backup process by comparing original files and directories with their backups. Possibly maintain a list of hashes of the backed up files to make the comparison quicker.
Restore corrupted /etc files by extracting the etc-backup.tar.bz2 file in a temporary working directory, and copying over individual files and directories as needed. To restore the entire /etc directory with all its contents execute the following command as root:
# bsdtar -xvf etc-backup.tar.bz2 -C /