LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-09-2014, 12:26 PM   #1
postcd
Member
 
Registered: Oct 2013
Posts: 527

Rep: Reputation: Disabled
Lightbulb How to Tar several files and paths and how to restore it?


Hello,

im making an bash script

and my aim is to backup:

Quote:
f /usr/local/ddos/ddos.conf
f /etc/fail2ban/jail.conf
f /nodewatch/scripts/nodewatch_config.php
d /etc/yum.repos.d
d /etc/pki/rpm-gpg/
i know i can do it using:
Code:
tar czf archive.tar.gz filepath1 filepath2 folderpath3
BUT i dont know how can i extract it easilly using one command into its pathes back?

Code:
tar xzf archive.tar.gz -C /destination/path/
should extract it? but it puts wrongly all files into one directory, setting path to / i would quess wont work?
 
Old 03-09-2014, 12:33 PM   #2
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
The simple no questions pending way.

$ mkdir destination
$ cd destination
$ tar xzf ../archive.tar.gz
$ cd ../

Where .. is the parent direction, versus . which is the current directory. Otherwise man tar and tar --help might help. A lot of times questions like these are answered faster by trying them out in a test setup. Then using what you've learned.

Edit: The problem with using paths is that they're saved along with the files. And an old bug with tar is that /etc/passwd could be a file in the tar which is why you should never run tar as root.

Alternatively: You could copy the files to a directory and tar that directory. But there may be a tar way directly. It's still better to have a less prone to undesired results method. Like cd to / and use relative paths (./) and the created file specified with a usable path.

Last edited by Shadow_7; 03-09-2014 at 12:43 PM.
 
Old 03-09-2014, 01:11 PM   #3
postcd
Member
 
Registered: Oct 2013
Posts: 527

Original Poster
Rep: Reputation: Disabled
and after the commands you written executed i have directories like yum.repos.d, rpm-gpg in directory "destination", but i need to have them in its proper directories not there.. while i sitll want to keep existing files and dont overwrite them by these i extract.. so i dont much understand how you solve my initial question. i already known i can extract it to other directory, but i want to keep pathes
 
Old 03-09-2014, 04:21 PM   #4
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
$ cd /
$ tar -czf /home/user/archive.tar.gz \
./etc/ \
./usr/local/ddos/ddos.conf \
./etc/fail2ban/jail.conf \
./nodewatch/scripts/nodewatch_config.php \
./etc/yum.repos.d* \
./etc/pki/rpm-gpg*

Something like that, the \ are optional if done as a one liner. They are saved "relative" to the current directory(which would be / if you cd to it first). To extract them to original locations, be located at / when extracted. Otherwise they extract under whatever directory you're in. This is basically how I clone linux installs that are < 4GB in size. Mod the fstab and the bootloader and off to the races. Extract to whatever filesystem is supported by the kernel of the install.
 
1 members found this post helpful.
Old 03-10-2014, 02:47 PM   #5
postcd
Member
 
Registered: Oct 2013
Posts: 527

Original Poster
Rep: Reputation: Disabled
Thanks, so the difference makes that dot added before full path?
 
Old 03-10-2014, 08:22 PM   #6
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
And from where you perform the tar is important aka cd to / first. Otherwise the relative path would be ../../ and things could get complicated.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] compress with tar without saving full paths razor7 Linux - Newbie 6 11-14-2016 04:57 AM
tar and absolute paths wjtaylor Linux - Server 4 03-28-2011 08:09 AM
tar files from multiple locations into a tarball preserving location paths Siljrath Linux - Newbie 2 07-10-2010 01:23 AM
BackUp & Restore with TAR (.tar / .tar.gz / .tar.bz2 / tar.Z) asgarcymed Linux - General 5 12-31-2006 02:53 AM
Can tar restore only files newer than already in target directory? lhouk Linux - General 4 08-31-2006 01:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration