2014-11-16 37 views
0

我有幾個目錄,如下所示:壓縮和單獨加密多個文件同時保留文件結構

dir1/ 
    |_foo.txt 
    |_bar.txt 
dir2/ 
    |_qux.txt 
    |_bar.txt 

對於每一個在這個目錄我想它裏面的文件壓縮成一些加密格式 然後複製的結構到一個新的位置(某處在線)。所以最後我們希望在新的位置得到類似這樣的東西:

dir1/ 
    |_foo.rar 
    |_bar.rar 
dir2/ 
    |_qux.rar 
    |_bar.rar 

有沒有一種簡單的Unix方式來做到這一點?

P.S.我正在尋找一個加密和壓縮文件的rar,但如果有什麼更好的讓我知道。

編輯:如果我不清楚,這是爲了備份的目的。

+0

在線? Dropbox的?你有ssh訪問權限的另一臺計算機?從文件所在的機器上運行的Web服務器上爲自己提供文件是否可以? – Enselic

+0

沒有這是爲了備份的目的,對不起,我應該已經清楚。 – RansuDoragon

回答

0

這是我該怎麼做的。

首先,創建這個幫手腳本並將其放在某處。我把它放在/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,像這樣爲每個文件運行腳本:

find . -type f -exec /Users/martin/1temp/stackoverflow/26960080/encrypt-and-compress.sh {} \; 

這將反映輸入DIR樹造成-DIR。要解密文件,請使用:

gpg --decrypt /Users/martin/1temp/stackoverflow/26960080/result-dir/./dir1/foo.txt 

要鏡像生成的加密目錄結構,我建議您使用rsync。具體細節取決於你想要的設置,但對谷歌來說很簡單。

祝你好運!

相關問題