Operating System - Linux
1827399 Members
6084 Online
109965 Solutions
New Discussion

backup & restore user home directory include hidden files

 
SOLVED
Go to solution
'chris'
Super Advisor

backup & restore user home directory include hidden files

hi

I'm looking for simple bash scripts running from the desktop:

1.) backup the user home directory include all hidden files:

tar cvzf /save/user.tar.gz /home/user

2.) restore from the backup /save/user.tar.gz

cd /home/user
tar xvzf /save/user.tar.gz; cd -
26 REPLIES 26
Steven Schweda
Honored Contributor
Solution

Re: backup & restore user home directory include hidden files

Is there a question in there?

> [...] running from the desktop:

Define "the desktop".

> tar c[...] /home/user

You may save yourself some work later if you
avoid specifying an absolute path when you
create your archive. For example:

cd /home
tar c[...] user

cd /home
tar x[...]

GNU "tar" has enough options to let you
recover from many kinds of unwise usage, but
why make more work for yourself than you need
to?
'chris'
Super Advisor

Re: backup & restore user home directory include hidden files

Thx I have created 2 scripts and these scripts should be launched as a normal user directly from a Desktop.

1.) Backup script:

#! /bin/bash
echo 'Backup user home dir'
cd /home
tar cvzf /home/user/save/backup.tar.bz2 $HOME

2.) Restore script:

#! /bin/bash
echo 'Restore user home dir'
cd /home
tar xvzf /home/user/save/backup.tar.bz2

Backup seems to work well, but if I try to restore I get this problem:

# sh restore
Restore user home dir
tar (child): /home/user/save/backup.tar.bz2: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now





Steven Schweda
Honored Contributor

Re: backup & restore user home directory include hidden files

> tar xvzf /home/user/save/backup.tar.bz2

I normally write for old "tar" programs, so I
normally make my own compression pipelines,
so I know nothing, but doesn't "-z" imply the
use of gzip, not bzip2? I don't know what
all these programs will do is you give them
inappropriate file-name suffixes.

http://www.gnu.org/software/tar/manual/

As I read it, "-a" ("--auto-compress") might
be more flexible.

> Backup seems to work well, [...]

Evidence?

What's in "/home/user/save/"? With my weak
psychic powers, I can't see it from here.
Viktor Balogh
Honored Contributor

Re: backup & restore user home directory include hidden files

Hi Chris,

I think it would be a better idea to save the backup outside of the home dir (which is also the subject of the backup) By this you would avoid backing up the already-created backups. What do you think?

Regards,
Viktor
****
Unix operates with beer.
Viktor Balogh
Honored Contributor

Re: backup & restore user home directory include hidden files

I meant this line in your latest script:

tar cvzf /home/user/save/backup.tar.bz2 $HOME

****
Unix operates with beer.
'chris'
Super Advisor

Re: backup & restore user home directory include hidden files

These scrips should run by normal non-root user.
I've setup permissons in /save directory for a normal non-root user.

I changed to tar.gz and it should backup outside of the home dir:

backup:

tar cvzf /save/backup.tar.gz $HOME

restore:

tar xvzf /save/backup.tar.gz


Now, if I try to restore using:

tar xvzf /save/backup.tar.gz

then the extra /home/user/home/user will be created, instead of extracting directly into /home/user/*.

Howto solve this problem?


Steven Schweda
Honored Contributor

Re: backup & restore user home directory include hidden files

> tar cvzf /save/backup.tar.gz $HOME

Should we non-psychics know what "$HOME" is
in this situation?

> Now, if I try to restore using:

Starting where, exactly?

pwd

> Howto solve this problem?

Read previous replies? Read the "tar"
documentation?

Also potentially useful:

man basename
'chris'
Super Advisor

Re: backup & restore user home directory include hidden files

Sorry for misunderstanding and pls let explain:

$HOME is home directory, it's dynamic.

It does the same like:

tar cvzf /save/backup.tar.gz /home/user

on my debian squeeze.

I try to restore, inside from /home/user, because I'd like to do it as a normal non-root user.

I think, running from / as a root user shouldn't be a problem.


Perhaps I'll try completely other solution:

copy ALL from /home/user/* into /save/*

and runnig this script outside of home dir:

#! /bin/bash
echo 'restore user settings'
cd /save
find . -print | cpio -dumpv /home/user








Steven Schweda
Honored Contributor

Re: backup & restore user home directory include hidden files

> $HOME is home directory, it's dynamic.

Thanks for the explanation, but I think that
I understood that much. However, you demand
that I assume/guess that its value is
"/home/user/" in things like:

> then the extra /home/user/home/user will be
> created [...]

You're the one who wants help. I think that
you should be clear about what you want and
what you're doing. It's not my job to guess
what you really mean.


> Mar 2, 2011 05:50:04 GMT 10 pts
> [...]
> cd /home
> tar c[...] user
>
> cd /home
> tar x[...]
> [...]

What, exactly, was wrong with that plan? Did
you try it (or anything like it)?

> man basename

echo basename "$HOME"

Just a thought.

You might also consider something like:

cd /home/user
tar c[...] .

cd /home/user
tar x[...]

Perhaps:

cd "$HOME"
or:
cd


In general, "tar" stores paths in the archive
which look like the paths you specify when
you create the archive. Then, when you
extract the files from an archive, it tries
to reproduce those stored paths. If you
don't want to see "/home/user" when you
restore the stuff, then don't say
"/home/user" to "tar" when you create the
archive. Often informative:

tar t[...]

> http://www.gnu.org/software/tar/manual/

Seriously, you _really_ should look at the
documentation. GNU "tar" provides many
options which let you play with the paths
when you create an archive, and when you
extract the files from it. Even if you
insist on creating the archive using
(inappropriate, unhelpful) absolute paths,
GNU "tar" allows you to extract the files to
pretty much any location you might choose.
But wise use of "cd" and appropriate,
relative path specifications can make these
things pretty easy in many cases, even
without using any fancy GNU-"tar"-specific
command options.
Steven Schweda
Honored Contributor

Re: backup & restore user home directory include hidden files

> copy ALL from /home/user/* into /save/*

And how do you propose to do that if you
still want to "include hidden files"?

If you figure out how to use "tar", then you
shouldn't need to do any of this "copy"
nonsense.


By the way, do you _really_ want to overwrite
_everything_ in this "/home/user" directory?
(Why?)
'chris'
Super Advisor

Re: backup & restore user home directory include hidden files

""And how do you propose to do that if you
still want to "include hidden files"?""

for example:

cp -a /home/user /save

should copy all files include hidden files into /save/user


""If you figure out how to use "tar", then you
shouldn't need to do any of this "copy""
nonsense.

Sorry, I need a quick solution and using tar didn't work as a normal non-root user to run inside a home dir.
Perhaps that was a mistake.
It should work as a tar x script to run it outside of the home dir.
Anyway if I have more time, I'll look at the tar documentation.

""By the way, do you _really_ want to overwrite
_everything_ in this "/home/user" directory?
(Why?)""

because i try to prepare a client and it should be possible to reset all user settings.

thx a lot and greetings



Steven Schweda
Honored Contributor

Re: backup & restore user home directory include hidden files

> [...] using tar didn't work [...]

What, exactly, didn't work? The one thing
which you said you tried, or one of the other
things which I suggested?
'chris'
Super Advisor

Re: backup & restore user home directory include hidden files

""What, exactly, didn't work?""

tar x exctract in the wrong path, it creates extra directory inside home dir.
I should find a solution to generate exact destination path using tar.
Steven Schweda
Honored Contributor

Re: backup & restore user home directory include hidden files

> ""What, exactly, didn't work?""
>
> tar x exctract in the wrong path, [...]

WHEN YOU DID _WHAT_? Which part of "exactly"
was unclear? What "tar x" does depends on
what's in the archive, and my psychic powers
are too weak to tell me what you're doing.

As usual, showing actual commands with their
actual output can be more helpful than vague
descriptions or interpretations.


> You might also consider something like:
>
> cd /home/user
> tar c[...] .
> [...]

Do you ever read this stuff? Do you ever try
these things?
Viktor Balogh
Honored Contributor

Re: backup & restore user home directory include hidden files

hi chris,

as Stewen suggests, before making the tar archive cd to the home directory and issue the tar command with relative path. (that is, with a dot at the end). This way you store the data with relative path, and no additional dirs will be created. Note that you also need to cd to the home before untaring.

****
Unix operates with beer.
Viktor Balogh
Honored Contributor

Re: backup & restore user home directory include hidden files

*SteVen
****
Unix operates with beer.
'chris'
Super Advisor

Re: backup & restore user home directory include hidden files

Thx and sorry, it was my mistake, I've forgotten dot at the end.

Running these scripts outside of home dir works really well:

backup:

#! /bin/bash
echo 'Backup user home dir'
cd /home
tar cvzf /save/user.tar.gz /home/user .

and resore:

#! /bin/bash
echo 'Restore user home dir'
cd /home
tar xvzf /save/user.tar.gz




Steven Schweda
Honored Contributor

Re: backup & restore user home directory include hidden files

> cd /home
> tar cvzf /save/user.tar.gz /home/user .

You seem to have missed the point about
specifying relative versus absolute paths to
"tar".

> [...] /home/user .

Why both "/home/user" _and_ "."? You might
try them one at a time, and see which one
does what you want. (If either of them
does.)

> cd /home
> tar xvzf /save/user.tar.gz

For a "tar" archive with absolute paths, does
that "cd" command actually do anything for
you?

What's actually in your archive?

tar tvzf /save/user.tar.gz


> [...] works really well:

If you think so, then you should probably
look again.


> [...] I need a quick solution [...]

Sometimes it's helpful to slow down and take
enough time to understand what you're doing,
rather that simply trying to throw guesses at
the problem until something works (or _seems_
to work). This really is simpler than you're
making it.
'chris'
Super Advisor

Re: backup & restore user home directory include hidden files

Thx again, you're completely right, don't need both "/home/user" _and_ "."

> [...] What's actually in your archive?

I have unpacked the archive using:

tar xzvf user.tar.gz

and it seems to have the same contents 1:1 like in /home/user

Before, I've tried to delete something from /home/user dir, for example a Desktop icon and it was succesfully recovered.
Steven Schweda
Honored Contributor

Re: backup & restore user home directory include hidden files

> What's actually in your archive?
>
> tar tvzf /save/user.tar.gz

> I have unpacked the archive using:
>
> tar xzvf user.tar.gz

It's like pulling teeth. The idea was to use
"tar t[...]" to see what the _names_ in the
archive were.


> and it seems to have [...]

> As usual, showing actual commands with their
> actual output can be more helpful than vague
> descriptions or interpretations.

And it's like talking to a wall.
Viktor Balogh
Honored Contributor

Re: backup & restore user home directory include hidden files

hi chris,

use "tar tzvf" instead of "tar xzvf". t is for testing the archive, which means nothing will be written to the disk, only the contents will be listed. It should list the files with _relative_ paths. If unsure, post the out here.
****
Unix operates with beer.
'chris'
Super Advisor

Re: backup & restore user home directory include hidden files

Sorry for troubles, my idea was tu use tar xzvf to extract somewhere and use diff to compare contents of 2 directories.
Yesterday, was really late I had a lot of other work and didn't have time to use diff.
I just looked into 2 directories, deleted and succesfully recovered some files from the home dir.

Here ist the output of diff:

# diff /save/user /home/user
Nur in /home/user: .adobe.
Nur in /home/user: .bash_history.
Nur in /home/user: .bash_logout.
Nur in /home/user: .bashrc.
Nur in /home/user: Bilder.
Nur in /home/user: .cache.
Gemeinsame Unterverzeichnisse: user/.config und /home/user/.config.
Nur in /home/user: .dbus.
Gemeinsame Unterverzeichnisse: user/Desktop und /home/user/Desktop.
Nur in /home/user: Dokumente.
Nur in /home/user: Downloads.
Nur in /home/user: .esd_auth.
Nur in /home/user: .fontconfig.
Nur in /home/user: .gconf.
Nur in /home/user: .gconfd.
Nur in /home/user: .gnome2.
Nur in /home/user: .gstreamer-0.10.
Nur in /home/user: .gtk-bookmarks.
Nur in /home/user: .gvfs.
Gemeinsame Unterverzeichnisse: user/.ICAClient und /home/user/.ICAClient.
Nur in /home/user: .ICEauthority.
Nur in /home/user: .kde.
Gemeinsame Unterverzeichnisse: user/.local und /home/user/.local.
Nur in /home/user: .macromedia.
Gemeinsame Unterverzeichnisse: user/.mozilla und /home/user/.mozilla.
Nur in /home/user: Musik.
Nur in /home/user: Ã ffentlich.
Nur in /home/user: .su-to-rootrc.
Nur in /home/user: .thumbnails.
Nur in /home/user: Videos.
Gemeinsame Unterverzeichnisse: user/.vmware und /home/user/.vmware.
Gemeinsame Unterverzeichnisse: user/.vnc und /home/user/.vnc.
Nur in /home/user: Vorlagen.
Nur in /home/user: .xsession-errors.
Nur in /home/user: .xsession-errors.old.

They are not 100% identical.

and here is the output tar tzvf:

tar tzvf backup.tar.gz > output.txt



Steven Schweda
Honored Contributor

Re: backup & restore user home directory include hidden files

> They are not 100% identical.

Would it be asking too much to see what,
exactly, all the commands were that you used
to get this stuff?

> As usual, showing actual commands with their
> actual output can be more helpful than vague
> descriptions or interpretations.

Which part of that was unclear?

This is very tiring.
'chris'
Super Advisor

Re: backup & restore user home directory include hidden files

backup /home/user dir using this bash script:
-----------------------------------
#! /bin/bash
echo 'Backup user home dir'
cd /home
tar cvzf /save/user.tar.gz .
-----------------------------------

extract into /save/user:

# cd /save
# tar xzvf user.tar.gz

compare:

# diff /save/user /home/user