Encryption 分别压缩和加密多个文件,同时保留文件结构

Encryption 分别压缩和加密多个文件,同时保留文件结构,encryption,compression,backup,Encryption,Compression,Backup,我有几个目录如下所示: dir1/ |_foo.txt |_bar.txt dir2/ |_qux.txt |_bar.txt find . -type f -exec /Users/martin/1temp/stackoverflow/26960080/encrypt-and-compress.sh {} \; 对于每个目录,我想将其中的文件压缩成某种加密格式 然后将结构复制到新位置(在线的某个位置)。所以最后我们希望在新的地点能得到这样的东西: dir1/ |_f

我有几个目录如下所示:

dir1/
  |_foo.txt
  |_bar.txt
dir2/
  |_qux.txt
  |_bar.txt
find . -type f -exec /Users/martin/1temp/stackoverflow/26960080/encrypt-and-compress.sh {} \;
对于每个目录,我想将其中的文件压缩成某种加密格式 然后将结构复制到新位置(在线的某个位置)。所以最后我们希望在新的地点能得到这样的东西:

 dir1/
   |_foo.rar
   |_bar.rar
 dir2/
   |_qux.rar
   |_bar.rar
有没有一种简单的Unix方法可以做到这一点

另外,我正在寻找加密和压缩文件的rar,但如果有更好的,请告诉我


编辑:如果我不清楚,这是出于备份目的。

下面是我将如何做的

首先,创建这个助手脚本并将其放在某个地方。我把我的文件放在
/Users/martin/1temp/stackoverflow/26960080/encrypt and compress.sh
中。别忘了使用chmod+x使其可执行,也别忘了更新路径以使其与您的系统匹配。请注意文件
temp password for batch encryption.txt
,它是一个简单的文本文件,其中一行带有用于加密的密码。如果没有此文件,您必须手动输入每个加密文件的密码,这很快就会成为一个麻烦。但要注意谁有权读取该文件

#!/bin/bash

# The relative path of the file to encrypt, passed as a parameter by find
file_to_encrypt="$1"

# The directory where the mirrored, encrypted directory structure shall end up
dest_dir="$HOME/1temp/stackoverflow/26960080/result-dir"

# Relative path to the dir of the file to encrypt, used to create the
# same directory structure elsewhere
dir_of_file_to_encrypt="$(dirname ${file_to_encrypt})"

# In what dir to put the result file, used to be able to create that
# dir before we encrypt the file
dest_dir_of_file_to_encrypt="${dest_dir}/${dir_of_file_to_encrypt}"

# Path to where the encrypted file shall be put
dest_file="${dest_dir}/${file_to_encrypt}"

# To not have to input the password manually each time, put it in this
# file temporarily (make sure to now allow anywone else to access this
# file...)
password_file="$HOME/1temp/stackoverflow/26960080/temp-password-for-batch-encryption.txt"

# Make sure the dest dir exists
mkdir -p "${dest_dir_of_file_to_encrypt}"

echo "About to encrypt ${file_to_encrypt} and putting it in ${dest_file} using password in ${password_file}"
# Encrypt the file and put it in the dest dir
# --symetric: Use simple so called symmetric encryption
# --cipher-algo: Select encryption algorithm
# --passphrase-fd 0: make "password from a file" work
# --yes: Overwrite any existing files
cat "${password_file}" | gpg --symmetric --cipher-algo AES256 --passphrase-fd 0 --yes --output "${dest_file}" "${file_to_encrypt}"
然后,将cd放入要加密的目录结构的根目录中

cd /Users/martin/1temp/stackoverflow/26960080/input-dir/
然后使用find为每个文件运行脚本,如下所示:

dir1/
  |_foo.txt
  |_bar.txt
dir2/
  |_qux.txt
  |_bar.txt
find . -type f -exec /Users/martin/1temp/stackoverflow/26960080/encrypt-and-compress.sh {} \;
这将把输入目录树镜像到结果目录。要解密文件,请使用:

gpg --decrypt /Users/martin/1temp/stackoverflow/26960080/result-dir/./dir1/foo.txt
要镜像生成的加密目录结构,我建议您使用rsync。具体细节在很大程度上取决于你拥有/想要的设置,但谷歌很容易做到


祝你好运

哪里在线?投递箱?您有ssh访问权限的另一台计算机?您可以自己从运行在文件所在机器上的web服务器上提供文件吗?不,这是出于备份目的,对不起,我应该清楚这一点。