How would I use tar for full backup and restore with system on SSD and home on HDD?

37,583

It is adviced to make TWO backups. 1 for / and 1 for /home if they are on different partition. If you want one you will have to add a lot of exceptions to that one command. Example:

sudo su
cd /
tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz --one-file-system / 
tar -cvpzf backuphome.tar.gz --one-file-system /home/

will backup your root and exclude ALL mounted partitions, like /media (you do not want to backup external devices (you really do not)) and you absolutely do NOT want to backup something like /dev or /proc. And /home goes to another backup.

In above method the backup are stored in /. It will be bettter to store it on an external media; you can then put a directory in front of the backup.tar.gz and drop the --exclude=... from the 1st command.

  • backup.tar.gz is the backup.
  • The --exclude will prevent the actual backup to be backupped.
  • cvpzf: create, verbose, preserve permissions, compress, use a file.

  • --one-file-system - Do not include files on a different filesystem. If you want other filesystems, such as a /home partition, or external media mounted in /media backed up, you either need to back them up separately, or omit this flag. If you do omit this flag, you will need to add several more --exclude= arguments to avoid filesystems you do not want. These would be /proc, /sys, /mnt, /media, /run and /dev directories in root. /proc and /sys are virtual filesystems that provide windows into variables of the running kernel, so you do not want to try and backup or restore them. /dev is a tmpfs whose contents are created and deleted dynamically by udev, so you also do not want to backup or restore it. Likewise, /run is a tmpfs that holds variables about the running system that do not need backed up (Source).


So ... if you really still want a "one-liner" it could look like this:

cd /
tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz --exclude=/proc 
--exclude=/tmp --exclude=/mnt --exclude=/dev --exclude=/sys / 

(I hope I got all of what needs to be excluded)


Restoring would be

sudo su
cd /
tar xvzf backup.tar.gz

I did not use sudo in front of the tar command since you will get error messages regarding permissions (because of files owned by root).

Share:
37,583

Related videos on Youtube

Buildit
Author by

Buildit

Updated on September 18, 2022

Comments

  • Buildit
    Buildit over 1 year

    How would I use tar for full backup and restore with system on SSD and home on HDD?

    The existing backup and restore answers don't seem to cover the case where root and home are on separate devices

    • David Foerster
      David Foerster over 9 years
      Welcome to AU! Are you looking for a one time thing or a continuous/periodic solution? Is the backup destination local or remote? tar doesn't really care about devices and mount points. Please edit your post to include the additional info.
    • Rinzwind
      Rinzwind over 9 years
      "The existing backup and restore answers don't seem to cover the case where root and home are on separate devices" the reason: it is a bad idea and making 2 backup files is not only smarter but also more efficient.
  • Buildit
    Buildit over 9 years
    Your two line suggestion worked very well except that when backing up the system I got one message. "/lib64/ld-linux86-64.so2 tar: /: file changed as we read it." Is this normal during a backup?
  • Rinzwind
    Rinzwind over 9 years
    Yes. tar will inform you of changes in the file during the backup action of that file itself (it checks status of file before and after backing it up). In general these messages are harmless. If you have your own software/data and get it on one of those files I would recreate the backup myself.
  • Alan Campbell
    Alan Campbell over 8 years
    Standard tar doesn't compress, it just joins everything to one file (TAR = Tape ARchive). Try gtar instead.
  • user4493605
    user4493605 about 7 years
    If the command will not backup certain directories, do I need to recreate them when I restore the system? Also what advantages does this method have to simply using Deja-Dup?
  • stephanmg
    stephanmg almost 3 years
    @Rinzwind so the tar command should be run with sudo?
  • Rinzwind
    Rinzwind almost 3 years
    @AlanCampbell that is what the "z" does.
  • Rinzwind
    Rinzwind almost 3 years
    @stephanmg depends on the contents. If it is personal files it might not be needed. If there are files you do not own it will require sudo. So backup of root will. Backup of /home will if you have more than 1 user :)
  • stephanmg
    stephanmg almost 3 years
    @Rinzwind: My apologies, I misread your instructions. :(
  • Goblinhack
    Goblinhack over 2 years
    would be good to add --exclude-vcs also for home dirs, to avoid .git as for me, tar complains about the file being too large