20

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

Buildit
  • 201
  • 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. – David Foerster Sep 15 '14 at 18:58
  • "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. – Rinzwind Sep 15 '14 at 19:20

1 Answers1

26

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).

Rinzwind
  • 299,756
  • 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? – Buildit Sep 17 '14 at 23:24
  • 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. – Rinzwind Sep 18 '14 at 07:15
  • Standard tar doesn't compress, it just joins everything to one file (TAR = Tape ARchive). Try gtar instead. – Alan Campbell Feb 15 '16 at 03:03
  • 1
    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? – user4493605 Apr 25 '17 at 09:25
  • @Rinzwind so the tar command should be run with sudo? – stephanmg Jun 07 '21 at 08:09
  • @AlanCampbell that is what the "z" does. – Rinzwind Jun 07 '21 at 12:24
  • 1
    @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 :) – Rinzwind Jun 07 '21 at 12:25
  • @Rinzwind: My apologies, I misread your instructions. :( – stephanmg Jun 07 '21 at 15:53
  • 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 – Goblinhack Oct 29 '21 at 09:26