1

I read somewhere that after using rsync to copy a partition from one device to other one, it is important to edit the UUID, since devices cannot coexist with the same UUID.

Is it true? How do I do it?

BlueSkies
  • 2,145
  • 2
    rsync works on files located on a file-system, the UUID is related to the file-system itself and is a lower level than the files that can then be stored on it. Different levels of the 'stack' – guiverc Nov 22 '19 at 23:48

2 Answers2

5

rsync does not copy partitions

rsync is a file and folder copying/syncing tool. It is great for syncing a local folder with a folder in a remote computer or backing up folders and files in one computer to another computer.

One thing it does not do is clone a partition. Therefore there is no need to worry about UUID of the partitions while using rsync.

Use gparted (or dd) to clone a partition

If you want to copy a partition, use GUI gparted or commandline dd. It will make an exact copy of the original partition with the exact size and free space as in the original. The copy will also have the same UUID.

This is a problem when a system tries to mount a partition by its UUID, such as using the /etc/fstab file and finds two partition with the same UUID. If the original and the copy are in two different computers, or if the copy replaces the original then the same UUID should not be a problem.

To give the copied partition a new UUID from within gparted:

  1. Select the copied partition. Make sure it is not mounted
  2. Go to Partition in the top menu or right click on the partition in the diagram
  3. Click on New UUID
  4. Click Apply to make the change

To assign a new UUID using the command line (for ext4 partitions only)

$ tune2fs -U $(uuidgen) /dev/sdXN

or

$ tune2fs -U random /dev/sdXN

where X is a letter, and N is a number specific to the partition of interest.

See answers to this question in Stackexchange for more on changing UUID using the commandline.

Hope this helps

user68186
  • 33,360
  • 1
    Note that copying a partition or disk with dd and dc3dd for that matter will also copy the UUID(s) which may need to be changed – Elder Geek Nov 23 '19 at 00:29
  • Thanks @EldeeGeek! This is a good point. I will add a section for command line based partition copying and UUID changing section based on your link. – user68186 Nov 23 '19 at 03:46
  • 1
    If newer gpt partitioning best not to copy just one partition. You also have GUID in the partition that must match primary & backup partition table. If you do copy it then you also have to update/change GUIDs. – oldfred Nov 23 '19 at 03:47
  • Thanks @oldfred! I have to admit that I have not worked much with gpt partitions. Does gparted handle the copying of the GUID when I copy from a gpt based system to another? – user68186 Nov 23 '19 at 03:52
  • I can't clone a partition because the size of the destination partition is larger than the size of the source partition and I want to preserve that. That's why I turned to rsync – BlueSkies Nov 23 '19 at 14:11
  • @BlueSkies see my answer to your other question about cloning/copying partition. When you use rsync you are not cloning partition, you are copying folders. – user68186 Nov 23 '19 at 14:15
  • @user68186: Not sure I understand how rsync works. Is it similar to copy/paste in Windows? Copy/paste in Windows adds new files, replaces old ones and retains the rest. Or the rsync first removes all the files from the destination partition and then copies all files from the source partition? – BlueSkies Nov 23 '19 at 14:43
  • @BlueSkies Yes, rsync is like copy and paste. Type man rsync in a terminal to read the "manual". It has many options that modifies how it works. For example it can overwrite existing files with the same name, or skip them, or only overwrite if the file in the destination is older than the one at the source, etc. – user68186 Nov 23 '19 at 14:51
  • 1
    @user68186: Do not know about gparted and copying GUIDs. I think it was a answer by Rod Smith who posts here and is author of gdisk on copying partitions. You can copy entire drive, but safer with partition to create it and copy data. Rsync has many, many parameters see man rsync. My first bash file with rsync commands, I included the description of parameters so I could remember what I was doing. – oldfred Nov 23 '19 at 14:53
  • @oldfred Thanks! I agree about rsync. It can do many things and it is always good to check the man. – user68186 Nov 23 '19 at 15:08
  • @user68186: The problem is that with all the millions of options, modes and terms used to explain a command, sometimes, it's hard to find the right answer or solution to a specific problem. For instance, to copy partition files and folders I used the -Havn option (because someone recommended it) but I don't understand what is the -H supposed to do... – BlueSkies Nov 23 '19 at 15:15
  • @BlueSkies see my comments to the other answer. – user68186 Nov 23 '19 at 15:25
  • I posted a comment regarding the rsync options you are recommending to use. – BlueSkies Nov 23 '19 at 16:07
  • 1
    If not sure what parameter does always check. -H preserve hard links, not seen that normally used and n is dry run or no changes. Your -a excludes -H so it do not know which is is really using. https://askubuntu.com/questions/545655/backup-your-home-directory-with-rsync-and-skip-useless-folders – oldfred Nov 23 '19 at 16:42
3

rsync can clone your entire /

But with both rsync and dd or another cloning method you must change /etc/fstab and /etc/default/grub and by extension /boot/grub/grub.cfg for Ubuntu to work properly.

From this script: Bash script to backkup/clone Ubuntu to another partition

rsync is called like this:

rsync -haxAX --stats --delete --info=progress2 --info=name0 --inplace  \
      /* "$TargetMnt"                                                   \
      --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found}

Notice the many directories that you do not want to copy specified with the --exclude directive. These directories are either recreated at boot time (virtual file system) or are pointers to other partitions (like /mnt and /media). You might also want to exclude the Trash folder from copying too.

After rsync completes you need to change the aforementioned files like this:

sudo sed -i "s/$SourceUUID/$TargetUUID/g" "$TargetMnt"/etc/fstab
sudo sed -i "s/$SourceUUID/$TargetUUID/g" "$TargetMnt"/boot/grub/grub.cfg
sudo sed -i "s/quiet splash/nosplash/g" "$TargetMnt"/boot/grub/grub.cfg

Where:

  • SourceUUID= The UUID of your current partition
  • TargetUUID= The UUID of your target / clone partition
  • TargetMnt= The mount point of your clone partition
  • s/quiet splash/nosplash/g line is optional so that when you boot your clone you see a difference with system messages displayed and no splash screen.

Finally to add a GRUB menu option pointing to your new cloned Ubuntu use:

sudo update grub
  • 1
    This is a valid point. Even though rsync does not copy partitions, it can copy all the contents of a partition. If that copied partition happens to be a root files system (/) partition. One would need to change reference to the UUID of the source partition to the new destination partition's UUID within the copied files so that the system files can work properly and use the new partition as the system partition. – user68186 Nov 24 '19 at 18:46