Erralt

Embedded archives in your shell scripts

Posted in Administration, LF, Linux by erralt on 16 août 2010

My problem was how to make an install of my servers with different files to create/modify with only one script, just using a copy/paste into my SSH console ?

It’s pretty easy to do, good news.

The principle is to have a variable which contains all the files we need in the script.
It’s done with base64 command which ensure me to have a string that does not interfere with the rest of my script.

How to use ? What to put in the variable ?

For an entire archive :
ARCHIVE = `tar -c /path/to/files | gzip | base64`
or just one file :
ARCHIVE = `cat /path/to/file | gzip | base64`
If you don’t want to compress, remove gzip step.

The variable looks like a simple hexadecimal string. Put it in one line (it could work when it’s truncated but i met some problems).
ARCHIVE = "PD9YTUwgdmVyc2lvbj0iMS4xIiBlbmNvZGluZz0iVVRGLTgiPz4NPCFET0NUWVBFIGtleWJvYXJkIFBVQkxJQyAiIiAiZmlsZTovL2xvY2FsaG9zdC9TeXN0ZW0vTGlicmFyeS9EVERzL0tleWJvYXJkTGF5b3V0LmR0ZCI+DTwhLS1MYXN0IGVkaXRlZCBieSBVa2VsZWxlIHZlcnNpb24gZW1hIiBvdXRwdXQ9IsKoIi8+DSAgICA8L3Rlcm1pbmF0b3JzPg08L2tleWJvYXJkPg0="

And finally just do the reverted actions in order to restore files contained in $ARCHIVE.
For an archive :
echo $ARCHIVE | base64 -d | gzip -d | tar -x
(use -t option instead of -x of tar to control what you are doing)
or just one file :
echo $ARCHIVE | base64 -d | gzip -d | cat > destination-file
If you don’t want to compress, remove gzip -d step.

Tagged with: , , , , ,