How do I take complete backup of my Linux partition? I am planning to perform some upgrade related activity and I want to take backup or clone Linux hard drive so for case of any failure I can restore the content.
PauseNext
Mute
Current Time 0:00
/
Duration 0:00
Fullscreen
Backward Skip 10sPlay VideoForward Skip 10s
It is very important in real time production environments to have a backup solution. You are always recommended to take a complete backup of you Linux partitions before you plan for any maintenance activity. These days we have many third party solutions which takes timely backup of your entire hard drive. But if your requirement is only to modify data of single or some of the Linux partitions then you can just use fsarchiver which is an excellent tool to take Linux partition's backup and then restore.
In this tutorial guide I will share examples to take backup of Linux partition, create image of partition, hard drive and logical volume using fsarchiver and restore using the backup archive content.
Topics we will cover hide
Download and Install fsarchiver
How to backup or clone Linux root partition?
How to backup or clone multiple Linux partitions
Encrypt the cloned backup archive
Exclude files and directories while cloning
Overview on fsarchiver
- fsarchiver is a system tool that allows you to save the contents of a filesystem to a compressed archive file.
- The filesystem contents can be restored on a device which has a different size and it can be restored on a different filesystem.
- Unlike tar or cpio, fsarchiver retains extended file attributes and can be restored to a disk with no current filesystem so you can clone your Linux hard drive or partitions.
- Everything is checksummed in the archive in order to protect the data.
- If the archive is corrupt, you just lose the current file, not the whole archive.
- The fsarchiver application recognizes and retains Windows file attributes as well as Linux attributes, making it suitable for migrating Samba-mounted partitions.
NOTE:
I will be using Oracle VirtualBox installed on Linux server, now since I have logical volumes so I will share all the steps of backup and restore using logical volumes but the same steps will work to backup and restore normal partition using the same command syntax. For logical volumes alternatively you can also use LVM snapshot to perform backup and restore.
Create Stratis Snapshot, Restore Stratis FileSystem & more (CentOS/RHEL 8)
Download and Install fsarchiver
fsarchiver is available for download in various formats from their official page
I will install fsarchiver using rpm version. At the time of writing this article 8.5 was the latest available release.
bash
# rpm -Uvh fsarchiver-0.8.5-1.el7.x86_64.rpm
On my RHEL/CentOS 7 node an additional dependency rpm was required which I installed separately using yum (This is only required if installation of fsarchiver fails with dependency for libzstd.so.1 library
bash
# yum -y install libzstd.x86_64
HINT:
For the sake of this article I am using an external USB to store the backup. My USB drive is NTFS format and is mounted on /usb.
ADVERTISEMENT
Backup Linux Partition
Create clone of your Linux partition
Here we have around 4 logical volume on my Linux node. As told earlier we will backup LVM instead of partition but the steps should be the same to clone a partition.
bash
[root@server1 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert data centos -wi-ao---- 5.00g home centos -wi-ao---- 2.00g root centos -wi-ao---- <18.00g swap centos -wi-ao---- 1.00g
Let's take create image of partition /dev/mapper/centos-data
I have created some dummy files and directories under /data folder
bash
[root@server1 data]# ls -l total 44 drwxr-xr-x. 2 root root 20480 Jan 10 11:12 binaries drwxr-xr-x. 75 root root 4096 Jan 10 11:12 config -rw-r--r--. 1 root root 10 Jan 10 11:12 file1 drwx------. 2 root root 16384 Jan 10 11:03 lost+found
It is important to un-mount the data partition before we attempt to backup partition
bash
[root@server1 ~]# umount /dev/mapper/centos-data
Below is the command to clone the linux partition using fsarchiver. Here our backup archive name is backup_data
bash
[root@server1 ~]# fsarchiver savefs /usb/backup_data /dev/mapper/centos-data Statistics for filesystem 0 * files successfully processed:....regfiles=2432, directories=602, symlinks=93, hardlinks=0, specials=0 * files with errors:...............regfiles=0, directories=0, symlinks=0, hardlinks=0, specials=0
So the backup partition succeeded with zero errors.
HINT:
To enable verbose you can additionally use -v. Increasing the count -v will increase the level of verbosity.
DRBD Tutorial | Setup Linux Disk Replication | CentOS 8
Verify the cloned backup archive content
Next to verify the cloned backup content let us delete some files and folders from our data partition
ADVERTISEMENT
bash
[root@server1 ~]# mount /dev/mapper/centos-data /data/ [root@server1 ~]# cd /data/
We will delete file1 and config directory
bash
[root@server1 data]# rm -rf config file1 [root@server1 data]# ls -l total 44 drwxr-xr-x. 2 root root 20480 Jan 10 11:12 binaries drwxr-xr-x. 75 root root 4096 Jan 10 11:12 config drwx------. 2 root root 16384 Jan 10 11:03 lost+found
Restore partition using the cloned backup archive
Now let us restore the content of our data partition. First un-mount the data partition before initiating restore
bash
[root@server1 ~]# umount /data/
Execute fsarchiver to initiate the restore. Here id=0 means first partition = number 0 followed by the destination file system. We will explain this in coming chapter.
bash
[root@server1 ~]# fsarchiver restfs /usb/backup_data.fsa id=0,dest=/dev/mapper/centos-data Statistics for filesystem 0 * files successfully processed:....regfiles=2432, directories=602, symlinks=93, hardlinks=0, specials=0 * files with errors:...............regfiles=0, directories=0, symlinks=0, hardlinks=0, specials=0
The restore partition step was successful as no errors are reported. Next verify your data
bash
[root@server1 ~]# mount /dev/mapper/centos-data /data/
So as you see our data which we had deleted is restored successfully.
bash
[root@server1 ~]# ls -l /data/ total 44 drwxr-xr-x. 2 root root 20480 Jan 10 11:12 binaries drwxr-xr-x. 75 root root 4096 Jan 10 11:12 config -rw-r--r--. 1 root root 10 Jan 10 11:12 file1 drwx------. 2 root root 16384 Jan 10 11:03 lost+found
How to backup or clone Linux root partition?
As we saw above that it is important to un-mount the partition before you can clone it ti a backup archive
Now we can not unmount root partition in multi-user environment so either you can go to single user mode
OR you can use -A argument with fsarchiver which will allow partition cloning to continue in read-write mode.
This has it's own risk which means any file which will be in use may not get properly backed up
bash
# fsarchiver savefs /usb/backup_sda.fsa /dev/mapper/centos-root -A Statistics for filesystem 0 * files successfully processed:....regfiles=29684, directories=5417, symlinks=1191, hardlinks=4253, specials=23 * files with errors:...............regfiles=0, directories=0, symlinks=0, hardlinks=0, specials=0
Linux lvm snapshot backup and restore tutorial RHEL/CentOS 7/8
How to backup or clone multiple Linux partitions
We can also backup or clone multiple partitions using single command. Here we will backup two partitions from my Linux node. I have already un-mounted both the partitions.
IMPORTANT NOTE:
It is important that you know the order in which the backup was created i.e. here in this example centos-data was the first partition in our backup archive while centos-home was the second partition. This will be used to restore the data later.
bash
# fsarchiver savefs /usb/backup_data /dev/mapper/centos-data /dev/mapper/centos-home Statistics for filesystem 0 * files successfully processed:....regfiles=2432, directories=602, symlinks=93, hardlinks=0, specials=0 * files with errors:...............regfiles=0, directories=0, symlinks=0, hardlinks=0, specials=0 Statistics for filesystem 1 * files successfully processed:....regfiles=1296, directories=4, symlinks=121, hardlinks=11, specials=0 * files with errors:...............regfiles=0, directories=0, symlinks=0, hardlinks=0, specials=0
So our backup has completed successfully for both partitions.
Next to restore partition you must use id directive in the same order in which backup was taken. Since we took backup of centos-data first it's id will be 0 and for our second partition centos-home the id will be 1 as shown below
ADVERTISEMENT
bash
# fsarchiver restfs /usb/backup_data.fsa id=0,dest=/dev/mapper/centos-data id=1,dest=/dev/mapper/centos-home Statistics for filesystem 0 * files successfully processed:....regfiles=2432, directories=602, symlinks=93, hardlinks=0, specials=0 * files with errors:...............regfiles=0, directories=0, symlinks=0, hardlinks=0, specials=0 Statistics for filesystem 1 * files successfully processed:....regfiles=1296, directories=4, symlinks=121, hardlinks=11, specials=0 * files with errors:...............regfiles=0, directories=0, symlinks=0, hardlinks=0, specials=0
So the restore is successful for both logical volumes. You can mount both the partition and verify the content now.
Encrypt the cloned backup archive
We can also increase the security and encrypt the cloned backup archive with a password using -c. You can either provide the password while taking the backup using "-c mypassword" or else use "-c -" to prompt for password. Here we will make fsarchiver prompt for password of our backup archive.
bash
[root@server1 ~]# umount /data [root@server1 ~]# fsarchiver savefs -c - /usb/backup_data /dev/mapper/centos-data Enter password: Confirm password: Statistics for filesystem 0 * files successfully processed:....regfiles=2432, directories=602, symlinks=93, hardlinks=0, specials=0 * files with errors:...............regfiles=0, directories=0, symlinks=0, hardlinks=0, specials=0
Next again to restore the backup archive you must provide the password of the archive using -c. You can again either provide the password in plain text format with "-c mypassword" or use "-c -" so fsarchiver will prompt for the password before starting restore activity.
bash
[root@server1 ~]# fsarchiver restfs /usb/backup_data.fsa id=0,dest=/dev/mapper/centos-data -c - Enter password: Statistics for filesystem 0 * files successfully processed:....regfiles=2432, directories=602, symlinks=93, hardlinks=0, specials=0 * files with errors:...............regfiles=0, directories=0, symlinks=0, hardlinks=0, specials=0
Beginners guide to how LVM works in Linux (architecture)
Exclude files and directories while cloning
You can also exclude certain files and directories based on regex while you backup partition using --exclude. For example I wish to exclude file1 from the backup of my data partition
bash
[root@server1 data]# ls -l total 44 drwxr-xr-x. 2 root root 20480 Jan 10 11:12 binaries drwxr-xr-x. 75 root root 4096 Jan 10 11:12 config -rw-r--r--. 1 root root 10 Jan 10 11:12 file1 drwx------. 2 root root 16384 Jan 10 11:03 lost+found
Un-mount the data partition
bash
[root@server1 ~]# umount /data/
Execute the fsarchiver backup command with --exclude=file1 to create image of partition.
HINT:
You can use --exclude multiple times to exclude multiple files and directories
bash
[root@server1 ~]# fsarchiver savefs /usb/backup_data /dev/mapper/centos-data --exclude=file1 Statistics for filesystem 0 * files successfully processed:....regfiles=2431, directories=602, symlinks=93, hardlinks=0, specials=0 * files with errors:...............regfiles=0, directories=0, symlinks=0, hardlinks=0, specials=0
Next I will remove file1 from the data partition to check if this is restored
bash
[root@server1 ~]# mount /dev/mapper/centos-data /data/ [root@server1 ~]# rm -f /data/file1 [root@server1 ~]# ls -l /data/ total 40 drwxr-xr-x. 2 root root 20480 Jan 10 11:12 binaries drwxr-xr-x. 75 root root 4096 Jan 10 11:12 config drwx------. 2 root root 16384 Jan 10 11:03 lost+found
Let us now un-mount the /dev/mapper/centos-data and restore partition from the backup archive
bash
[root@server1 ~]# umount /data/ [root@server1 ~]# fsarchiver restfs /usb/backup_data.fsa id=0,dest=/dev/mapper/centos-data Statistics for filesystem 0 * files successfully processed:....regfiles=2431, directories=602, symlinks=93, hardlinks=0, specials=0 * files with errors:...............regfiles=0, directories=0, symlinks=0, hardlinks=0, specials=0
Since the restore partition was successful, mount the data partition and check if file1 was restored
bash
[root@server1 ~]# mount /dev/mapper/centos-data /data/
As expected file1 was not backed up and hence is not restored since we intentionally excluded the file from backup.
ADVERTISEMENT
bash
[root@server1 ~]# ls -l /data/ total 40 drwxr-xr-x. 2 root root 20480 Jan 10 11:12 binaries drwxr-xr-x. 75 root root 4096 Jan 10 11:12 config drwx------. 2 root root 16384 Jan 10 11:03 lost+found
How to fix "pvs shows unknown device" in RHEL/CentOS 7/8
Conclusion
In this tutorial we learned about how we can take backup of entire Linux partition or hard drive. This process is similar to cloning process, infact better I would say as we get additional options such as excluding certain files and directories while cloing the hard drive. Or you can also take backup of root partition
Lastly I hope the steps from the article to backup and restore partition using fsarchiver and create image of partition on Linux was helpful. So, let me know your suggestions and feedback using the comment section.
References
I have used below external references for this tutorial guide
man page of fsarchiver

被折叠的 条评论
为什么被折叠?



