Rsync is a widely used file level synchronisation utility. When upgrading or replacing a failing disk drive it is important to make sure no data or even metadata loss occurs. Rsync can provide near mirror copies of files and directories. Only block-level copying can create a perfect mirror but it is not needed or wanted in all but some cases such as when a hardware defect is present in which case block level duplication can reduce data-loss.
After a disk drive has been physically connected it’s time to partition & format the disk drive then mount the partition on a temporary mount point. The files can now be transferred from the old or failing storage device to the temporarily mounted partition on the new replacement storage device. If a disk is being upgraded rsync is the best option as it can preserve all data and nearly all metadata. In case of a damaged storage device it may be advisable to backup at block-level with DD.
Synchronise data with Rsync
Run a test synchronisation.
sudo rsync -avihXAPH --info=progress2 --stats --dry-run /mnt/disk?/ /mnt/temp/
A test command is enabled with
--dry-run which will execute the command but will not perform any writes to the storage device.
Perform the initial synchronisation of files between the 2 mount points.
sudo rsync -avihXP --info=progress2 --stats --timeout=5 /mnt/disk?/ /mnt/temp/
Archive mode is enabled with
--archive options which is equivalent to
-rlptgoD options combined.
Recurse into directories with
-r option. Copy symbolic links with
--links option. Permissions can be preserved with
--perms option, modification times with
--times option and owner with
-D option is a combination of
--specials options. Device and special files are preserved with
--specials options respectively.
Verbosity can be increased with
--verbose option. List a summary of all changes with
--itemize-changes option. The storage units can be displayed in human readable format with
--human-readable option. Extended attributes are preserved with
--xattrs option while ACLs are preserved with
--acls option. The
-P option is the same as
--progress options. Hard links are preserved with
-H option. Files that have not finished transferring can be kept with
--partial option otherwise these files are deleted. The progress of the current file being transferred can be shown with
--progress option while total progress can be show with
--info=progress2 option. Additional file transfer statistics can be displayed with
--stats option. By default Rsync has no timeout meaning it will never exit and partial files will never get renamed which means they cannot be resumed. Setting a
timeout value will make Rsync exit when no data is received after a specified number of seconds. It should prevent temporary files from being saved and never removed. This The last 2 options are the
destination paths respectively. The trailing slashes of the source and destination paths will sync contents of the directory but not directory itself.
Preserving extended attributes with
-Xcan result in the errors below related to SELinux. This persists even when SELinux is in permissive mode. To get a round these errors simply remove the
rsync: rsync_xal_clear: lremovexattr("*****","security.selinux") failed: Permission denied (13) rsync: rsync_xal_set: lremovexattr("*****","security.selinux") failed: Permission denied (13)
Perform an additional synchronization and remove synced files from the source path.
sudo rsync -avihXP --info=progress2 --stats --timeout=5 --remove-source-files /mnt/disk?/ /mnt/temp/
To remove files from the source path that have been synced to the destination path with
Verify Transferred Data
Once the synchronisation has completed its worth manually inspecting the files on the new storage device to verify the general directory and file structure. A quick way to verify that all files have transferred is to compare the total space used on the old and the new partitions. If the size matches it’s reasonable to assume all files have been transferred though it’s still possible that files could be corrupt.
Show total directory sizes of all mount points.
du -sh /mnt/* | sort -h
Once the files have been transferred and verified the old storage device can be unmounted and disconnected. If it is to be sold or disposed it should be securely wiped first to prevent data theft.