Reputation: 14856
My goal:
having a shellscript for a cronjob (on MacOSX Snow Leopard) that connects to a Debian machine with ssh (public/private key login), executes a tar
command and downloads the tarred file afterwards.
My problem:
The login works, also the execution of some commands. But how can I download a file back to the local machine?
This is what I have so far:
This is the content of the shell script so far:
#!/bin/bash
ssh user@remotehost << 'ENDSSH'
tar -C / -czf /home/user/stuff.tar.gz /home/user/stuff
ENDSSH
Upvotes: 2
Views: 5295
Reputation: 1104
I. How to compress files or folders via SSH
For different compressed formats, you need to use different command lines:
To compress a file or folder to a zip file:
zip -r file.zip file
To compress a file (ONLY) to a bz2 file:
Bzip2 -zk file
To compress a file (ONLY) to a gz file:
gzip -c file > file.gz
By the way, you need to change the above "file" to the file name with extension (if any) you want to compress, while you can replace the following "xxx" with any keywords:
To compress one file or folder to a tar file:
tar -cvf xxx.tar file
To compress multiple files and/or folders to a tar file:
tar -cvf xxx.tar file1 file2 folder1 folder2 ...
To compress one file or folder to a tar.bz2 file:
tar -cvjf xxx.tar.bz2 file
To compress multiple files and/or folders to a tar.bz2 file:
tar -cvjf xxx.tar.bz2 file1 file2 folder1 folder2 ...
To compress one file or folder to a tar.gz file:
tar -cvzf xxx.tar.gz file
To compress multiple files and/or folders to a tar.gz file:
tar -cvzf xxx.tar.gz file1 file2 folder1 folder2 ...
II. How to extract file via SSH
To extract a file will be easier, since you don't need to worry about folders:
To extract a zip file:
unzip file.zip
To extract a bz2 file:
bunzip2 file.bz2
To extract a gz file:
gzip -d file.gz
To extract a tar file:
tar -xvf file.tar
To extract a tar.bz2 file:
tar -xvjf file.tar.bz2
To extract a tar.gz file:
tar -xvzf file.tar.gz
By the way, you need to replace the above "file"s of the compressed files with the real file names.
Bonus:
Besides remote servers, the above command lines are also available for a Mac OS computer with the Terminal application.
Upvotes: 0
Reputation: 121820
Why downloading the tar file and not create the tar content on stdout?
Ie:
ssh user@machine '(' cd /the/dir '&&' tar cf - list of files ')' >archive.tar
Upvotes: 1
Reputation: 141928
Simply rsync
the file once it's created:
#!/bin/bash
ssh user@remotehost tar -C / -czf /home/user/stuff.tar.gz /home/user/stuff
rsync -chavP --stats user@remotehost:/home/user/stuff.tar.gz .
This does initiate a second connection to remotehost
but will save you copying data across the network when the file has not changed (much) since the last time it was archived.
Upvotes: 1
Reputation: 10579
Short and simple, no heredoc needed.
ssh -Te none user@remotehost "tar -C / -cz /home/user/stuff" >stuff.tar.gz
Upvotes: 2
Reputation: 112
this might be want you want.
scp stuff.tar.gz user@remotehost:/"directory to place this file"/
Upvotes: 1
Reputation: 799320
Stream it back.
#!/bin/bash
ssh user@remotehost << 'ENDSSH' > stuff.tar.gz
tar -C / -czf - /home/user/stuff
ENDSSH
Upvotes: 1