Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

how do I create a compressed tar archive with gzip

If none of the specific sub-forums seem right for your thread, ask here.
Post Reply
Message
Author
jaytelford
Posts: 36
Joined: 2018-05-09 10:56
Location: United KIngdom

how do I create a compressed tar archive with gzip

#1 Post by jaytelford »

Hi,

I need to create a backup of two directories on my server.

I have searched around for ways to do this using gzip to create a compressed tar archive and then scp it through an ssh tunnel, to my local machine from my remote server. I am a bit confused though because the sites I have found on Google don't really explain it properly or give examples - they just blast off a load of flags and then other people or other sites contradict each other; telling you to use different flags or to use the pipe, where others say not to.

Can somebody please explain to me how I can do this:

1: Create a compressed tar archive using gzip of my httpdocs directory
2: Connect to my local machine from my remote server using ssh
3: scp the compressed tar archive created in step 1 to my local machine from my remote server through an ssh tunnel

I know how to use ssh and I know how to scp files/directories via terminal but I don't really understand how to create the compressed tar archive and I don't really understand how to put all the steps together into a terminal command.

Thanks
Jay

NB
Just to be clear, I want to use these archives as backups and store them on my local machine

Bulkley
Posts: 6383
Joined: 2006-02-11 18:35
Has thanked: 2 times
Been thanked: 39 times

Re: how do I create a compressed tar archive with gzip

#2 Post by Bulkley »

There are on the Net so many instructions for the various tar operations that I can't be bothered remembering any of it. It will be an easy search.

peter_irich
Posts: 1403
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation
Been thanked: 11 times

Re: how do I create a compressed tar archive with gzip

#3 Post by peter_irich »

scp transmits files only, not directories.
And what Net is need for tar? "man tar" is quite enough.

Code: Select all

tar zcvf name.tar.gz directory
Peter.
Last edited by peter_irich on 2018-05-26 19:26, edited 1 time in total.

p.H
Global Moderator
Global Moderator
Posts: 3049
Joined: 2017-09-17 07:12
Has thanked: 5 times
Been thanked: 132 times

Re: how do I create a compressed tar archive with gzip

#4 Post by p.H »

jaytelford wrote:Create a compressed tar archive using gzip
gzip does not create an archive (a structured container for files and directory). It just compresses a single file or a stream.
tar creates a tar archive, and optionnally uses gzip to compress it.
peter_irich wrote:scp transmits files only, not directories.
What about the -r option ?

Code: Select all

     -r      Recursively copy entire directories.  Note that scp follows sym‐
             bolic links encountered in the tree traversal.

peter_irich
Posts: 1403
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation
Been thanked: 11 times

Re: how do I create a compressed tar archive with gzip

#5 Post by peter_irich »

peter_irich wrote:scp transmits files only, not directories.
What about the -r option ?

Code: Select all

     -r      Recursively copy entire directories.  Note that scp follows sym‐
             bolic links encountered in the tree traversal.
Excuse me, I forgot this option. I prefer to send compressed directories.

Peter.
Last edited by peter_irich on 2018-05-26 19:24, edited 1 time in total.

jaytelford
Posts: 36
Joined: 2018-05-09 10:56
Location: United KIngdom

Re: how do I create a compressed tar archive with gzip

#6 Post by jaytelford »

gzip does not create an archive (a structured container for files and directory).
I had realised this and indeed that is what I was referring to. I wanted to create the tar archive and then compress it using gzip. After than I wanted to transfer it to my local machine. I kind of understood how to do this with multiple steps

create the tar archive
compress it using gzip
transfer it from remote to local
rm -rf the compressed archive from the remote

What I had actually wanted to do however - was create the tar archive, compress it with gzip and transfer it to my local machine in a single step, without needing to go back and then rm -rf it from the remote.

So basically I wanted to transfer a compressed tar archive of the directories that I need to backup to my local machine, without having to physically create the archive on the remote and then having to go back and delete it.

Sorry, it seems I wasn't clear enough in my initial question but I was tired at the time I wrote it, so what made sense to my sleepy brain probably didn't make sense to anyone else's.

Cheers
Jay

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: how do I create a compressed tar archive with gzip

#7 Post by arzgi »

jaytelford wrote: I I kind of understood how to do this with multiple steps

create the tar archive
compress it using gzip
transfer it from remote to local
rm -rf the compressed archive from the remote

What I had actually wanted to do however - was create the tar archive, compress it with gzip and transfer it to my local machine in a single step, without needing to go back and then rm -rf it from the remote.
First two tasks can be done in one command:

Code: Select all

tar -czvf name-of-archive.tar.gz /path/to/directory-or-file
You can write a bash script, one command on each line. To make sure it works, test each command separately. Then make script executable (chdmod u+x [script]). After that you only have run that script.

steve_v
df -h | grep > 20TiB
df -h | grep > 20TiB
Posts: 1400
Joined: 2012-10-06 05:31
Location: /dev/chair
Has thanked: 79 times
Been thanked: 175 times

Re: how do I create a compressed tar archive with gzip

#8 Post by steve_v »

jaytelford wrote:I wanted to transfer a compressed tar archive of the directories that I need to backup to my local machine, without having to physically create the archive on the remote and then having to go back and delete it.
The Unix way is with pipes, e.g.:

Code: Select all

$ tar zcf - [somefiles] | ssh user@destination 'cat - > archive.tar.gz'
Tars STDOUT is passed over SSH to cat on the receiving end, which writes it to a file. More.
Once is happenstance. Twice is coincidence. Three times is enemy action. Four times is Official GNOME Policy.

User avatar
stevepusser
Posts: 12930
Joined: 2009-10-06 05:53
Has thanked: 41 times
Been thanked: 71 times

Re: how do I create a compressed tar archive with gzip

#9 Post by stevepusser »

Obligatory xkcd reference:

Image
MX Linux packager and developer

Post Reply