Shell – Compose and pipe a gzipped tar archive in one go

filesshell-scripttar

I'm trying to come up with an effective way to create a gzipped archive of various system partitions on Android, piping everything out to stdout so as to be able to encrypt it as it comes across via gpg.

The workflow looks like this:

adb shell "/data/dobackup.sh" | gpg --output suchbackup.tar.gz.gpg --encrypt --sign --recipient me@myemail.com

The shell script needs to basically do this:

cat /dev/block/platform/msm_sdcc.1/by-name/recovery > recovery.img
cat /dev/block/platform/msm_sdcc.1/by-name/system > system.img
...

However, as seen, I'd like to do this whole thing over an adb shell execution so that everything is transferred over USB, into memory, and quickly is encrypted then written to disk, ie leaving no unencrypted trace.

The problem I'm having is that I'm not sure how to manufacture the internals of the script into a tar.gz file composed of multiple files/sources. Basically, I'm looking to output a tar.gz file from stdout from this script which includes the following files inside:

/recovery.img
/system.img
/userdata.img
/cache.img
/...

Is there any way I can do this? I haven't encountered a problem like this in my travels so far.

Best Answer

Assuming you mean that you want to use tar to create an archive and send it to standard out, that's what happens by default if you don't use the f option to send the output to a file:

tar cz /{recovery,system,userdata,cache}.img | ...

If the problem is archiving "files" which don't actually exist on the filesystem, that's a bit trickier because of the format of tar files: each file is preceded by a header which includes the filesize. The filesize in the header must be correct because that's the only way that tar has to know where the next header entry starts.

So it's only feasible to do this if you can predict precisely the size of the generated file object. If you can do that,you should be able to construct a tar file using, for example, the standard python library tarfile module.

Related Question