8

I want to copy everything in root / to my external hard disk before I upgrade my OS so I'll have everything just in case something goes wrong. Is the following the most appropriate command for it?

username@machine:/$ sudo cp -r . /media/username/TOSHIBA\ EXT/root_copy/

5 Answers 5

13

To add to what P.P has said, you don't really want to copy every directory. I use tar to backup everything that I need, and "exclude" the directories I don't need. The command basically goes tar (options) BACKUP-FILENAME TARGET. If you run the following command it should do the trick.

sudo tar -cvpzf /media/username/TOSHIBA\ EXT/root_copy/root-copy.tar.gz --exclude='/mnt' --exclude='/run' --exclude='/proc' --exclude='/sys' --exclude='/media' --exclude='/dev' --exclude='/lost+found' --exclude='/tmp' --exclude='/boot' --exclude=".*lost+found" / 2> /root/tar-errors.txt

I know it looks like a lot, but tar is going to create a backup at /media/username/TOSHIBA\ /EXT/root_copy/. The file name is root-copy.tar.gz. It will exclude each of the directories that I have mentioned with --exclude, and lastly it will dump any errors that occur in a file named /root/tar-errors.txt.

Another benefit to using tar this way, is that the backup file will be a "zipped tarball". It's essentially a compressed archive so you will save space on your external drive.

I am sure there are more simple ways of doing this but I have always used this method to backup my systems.

10
  • 5
    I also exclude /home/*/.cache, that is also several gigabytes you don't need. Jul 9, 2020 at 11:49
  • @SimonRichter mind explaining a little why could those be excluded?
    – scribe
    Jul 9, 2020 at 22:09
  • @Gordster, when I ran this, I am getting bash: /root/tar-errors.txt: Permission denied. I did run it with sudo but it did not prompt for the password.
    – scribe
    Jul 9, 2020 at 22:23
  • That will be because the redirection is running as your regular user, not with sudo. You'll need to use ... 2> sudo tee /root/tar-errors.txt (and install tee if it's not there already) Jul 9, 2020 at 22:43
  • I was actually able to run sudo -i and log in as a sudoer and then cd /. After that I ran the command and it is running just fine.
    – scribe
    Jul 9, 2020 at 23:05
9

Consider imaging the disk instead of making just a copy of files. There's more than just files that can get messed up during an upgrade: EFI system partition, boot sectors etc.

I'd recommend Clonezilla. It's user-friendly and reliable. They have their dedicated boot disk. Some rescue disks provide it too (eg. GRML) and you can install it on Ubuntu Live CD.

Clonezilla images aren't browseable, but can be used to easily restore entire disk to previous condition.

Some people could suggest using dd to create a raw image. It's also a valid approach and resulting files would be browseable, but they would take as much space as your entire partition size. Meanwhile Clonezilla uses partclone under the hood, which only copies actually used disk space and ignores free space unlike dd. Clonezilla will also compress images by default.

2
  • 1
    +1 for suggesting Clonezilla, but I wouldn't go so far as to say it is user-friendly. An experienced computer user can handle it, but I imagine a less experienced one would find it quite challenging. Jul 9, 2020 at 9:14
  • @JonBentley You're right, it's a stretch to say it's user-friendly in general, but it definitely is compared to tar ;)
    – gronostaj
    Jul 9, 2020 at 12:25
8

Yes, if you want to copy everything under /, then you can do:

sudo cp -r / media/username/TOSHIBA\ EXT/root_copy/

But, before proceeding... you need to be aware that you don't really want to copy everything under / :)

You typically only need to copy your data; not all the OS binaries, installed applications, configuration files, etc. Which is what the about command would do.

If OS upgrade goes bad, it's not going to be straightforward to "copy over" everything. If you had your data, you can simply reinstall the OS and copy back the data from the external disk/medium.

Also, if you have set up other partitions than the one you have the OS installed and keep the data there, then you may not need to copy externally at all. You can keep your data in a separate partition. If the OS reinstall goes bad, you can just wipe that partition and reinstall the OS without touching the other partition where you kept the data.

10
  • I don't exactly know where all my data might be, so I am machine gunning everything in root to an external hdd just in case. Maybe you'll be interested in answering this.
    – scribe
    Jul 8, 2020 at 23:14
  • 3
    Don't copy /proc or /dev. You'll regret trying to restore those. If you don't know where your data is...then find out. This answer is very similar to the answer to your previous question.
    – user535733
    Jul 8, 2020 at 23:18
  • I am not looking to restore anything with this, I know where most of my data is and I have backed it up. Now I am just copying because why not, if shit hits the fan at least I would have somewhere to look back. I have used my system for about 5 years now. It is not possible to find out exactly what all I did and where.
    – scribe
    Jul 8, 2020 at 23:25
  • 1
    @scribe But please don't copy back everything onto / even if something goes wrong. Because an OS upgrade would inevitably change a number of files/configurations. So if you "restore" everything from the external medium/USB for whatever reason, you might end up breaking it even more.
    – P.P
    Jul 8, 2020 at 23:29
  • 2
    To be fair to OP, I think there is a lot of missing the point here. I personally clone my hard drive before re-installing the OS. Doing so means I can simply restore the clone and I will be exactly where I started from with no need to reinstall applications and configuration files etc. which this answer rules out. If you've only copied the data, you will need to reinstall the originall OS, reinstall all the applications, re-configure everything, and copy back all the data. That's much more labour and time intensive. I agree that things like /proc are not useful here, but OP understands that. Jul 9, 2020 at 9:05
1

rsync is better IMO that tar or cp

if you have your sshkeys setup, and are going to another system: nohup rsync-r /* user@destination:/ARCHIVE_DESTINATION

else rsync -r /* user@destination:/ARCHIVE_DESTINATION

If local rsync -r /* /mnt/ARCHIVE_DESTINATION

0

I find it much easier to mount the root device on another path and then use cp (or even better rsync). This way I do not need to worry about excluding /dev/disk, /run, /proc etc.

For example:

    eval $(findmnt --pairs --target /)
    sudo mount -t ${FSTYPE} --options ${OPTIONS} --source ${SOURCE} --target /mnt/
    sudo cp -r /mnt/ /media/username/TOSHIBA\ EXT/root_copy/

The first line creates the following environment variables in my case:

    TARGET="/" SOURCE="/dev/sda8" FSTYPE="ext4" OPTIONS="rw,noatime,errors=remount-ro"

These are used to mount the root device again in /mnt. Alternatively, you can use sudo mount --bind / /mnt (I am not sure if there is a difference between the two solutions).

As others have noted, there are things in / that you may not need to backup so it would make sense to exclude them anyway. My solution is useful when you want to copy everything in the partition so you could in principle even boot from the external device (assuming efi, boot, etc are also copied / found somewhere upon boot).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .