Operating System - Linux
1753383 Members
5909 Online
108792 Solutions
New Discussion юеВ

script to loop through directories and encrypt all files using openssl into another directory

 
Mark C. LaFevre
Advisor

script to loop through directories and encrypt all files using openssl into another directory

I'm looking for a script that I could use to select a directory, and the script would look through that directory and all sub directories and perform an openssl encrypt against each file into a target directory tree while maintaing the same structure/permissions.... I know the openssl syntax, but don't understand the scripting necessary to loop through all the dirs. This is a crude way to encrypt the data and allow to be stored in another location and restored when necessary.

Anyone for fishing?
8 REPLIES 8
Steven Schweda
Honored Contributor

Re: script to loop through directories and encrypt all files using openssl into another directory

The basic tool is "find" ("man find"). For
example, the following command will run
through "directory" and every file under it,
and echo each file name. ("{}" represents
the name of the current file as it is
processed.):

find directory -exec echo {} \;

You can substitute the command you wish to
use for "echo {}". "find" offers many file
selection options, too, such as "-type f",
which would eliminate the directories
themselves:

find gzip -type f -exec echo {} \;

Strictly speaking, that selects only the
regular files, which excludes directories and
some other things, too. A more precise way
to eliminate the directories would be to
exclude the directories, rather than to
accept only the regular files:

find gzip '!' -type d -exec echo {} \;

If you could better describe your actual
requirements, there's almost certainly a
better method than your apparent plan.
Sandman!
Honored Contributor

Re: script to loop through directories and encrypt all files using openssl into another directory

I don't know the openssl command/syntax but here's a command that will go thru your source dir (including subdirs) and copy them to a target dir while preserving the structure, permissions etc.

# cd && find . -depth -print | cpio -pdumv

~hope it helps
Mark C. LaFevre
Advisor

Re: script to loop through directories and encrypt all files using openssl into another directory

my sytax for the openssl is

openssl enc -aes-256-cbc -pass pass:mypass -e -salt -in myinfile -out myout.ssl

I have one dir with about 6 sub dir's and under that may be more sub dirs... I need a quick way to encrypt all data in the dir tree and move them into another location while hopefully maintaining the dir structure so those files could easily and quicky recovered to the original locations.
Anyone for fishing?
Steven Schweda
Honored Contributor

Re: script to loop through directories and encrypt all files using openssl into another directory

... -in {} -out another_location/{} ...

You'd probably need to make the destination directories first, but that could be
scripted, too. Crudely:

find directory -type d -exec mkdir another_location/{} \;

Oops. Earlier references to "gzip" were
caused by thoughtless copy-paste from an
example I was running. Please read them as
"directory".

I assume that "another location" is easily
accessible in the file system, not requiring
FTP or something.
James R. Ferguson
Acclaimed Contributor

Re: script to loop through directories and encrypt all files using openssl into another directory

Hi Mark:

One way would be to 'tar' (using *relative* paths) your source directory; encrypt the tarball; and un-tar it in your destination directory.

Regards!

...JRF...
Mark C. LaFevre
Advisor

Re: script to loop through directories and encrypt all files using openssl into another directory

We seem to be hitting an issue with openssl working on a file greater than 2gb, we tried the tar and then encrypt, but wouldn't work, hoping to encrypt all the smaller files, then tar.
Anyone for fishing?
Peter Nikitka
Honored Contributor

Re: script to loop through directories and encrypt all files using openssl into another directory

Hi,

use Sandmans/Stevens approach - slightly modified - to create the destination directory structure:

cd source
find . -type d | cpio -pd destination

In the second step take care for the files (untested!):

find . -type f -exec openssl enc -aes-256-cbc -pass pass:mypass -e -salt -in {} -out destination/{}.ssl \;

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Sandman!
Honored Contributor

Re: script to loop through directories and encrypt all files using openssl into another directory

slight variation on Peter's solution...

# cd && find . -type f | cpio -pdumv | xargs -i openssl enc -aes-256-cbc -pass pass:mypass -e -salt -in {} -out {}.ssl