-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash.tool.gpg.encrypt.workflow.sh
136 lines (93 loc) · 2.95 KB
/
bash.tool.gpg.encrypt.workflow.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/sh
# created: 08-22-2017
# updated: 08-27-2017
# name: ssh_helper.sh
# about: encryption backup workflow
# version: 0.1.0
function ui_get_local_folder() {
read -r -d '' applescript_get_folder_path << EOF
set var_folder to POSIX path of (choose folder with prompt "Choose Folder To Sync")
return POSIX path of var_folder
EOF
local folder_path=$( osascript -e "$applescript_get_folder_path" )
echo "${folder_path}"
}
function ui_get_password() {
read -r -d '' applescript_get_password << EOF
set var_password to the text returned of (display dialog "Enter GPG Password" default answer "")
return var_password
EOF
local user_password=$( osascript -e "$applescript_get_password" )
echo "${user_password}"
}
function current_timestamp (){
echo $( date "+%Y.%m.%d.%H%M%S" )
}
function get_random_alphanum() {
# python script: indentation and whitespace matters, spaces over tabs.
local python_get_random_alphanum=$(
python - <<EOF
from random import choice;
print ''.join([
choice(
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'abcdefghijklmnopqrstuvwxyz'
'0123456789')
for i in range(10000)])
EOF
)
# trim length to thirty character file name or user input amount
local alphanum_length=${1}
local alphanum_filename=$( \
echo "${python_get_random_alphanum}" | \
head -c "${alphanum_length}" \
)
echo "${alphanum_filename}"
}
function create_temp_file () {
local temp_file=$( \
mktemp /tmp/bash.script.temp.gpg.workflow.XXXXXXXXXX || \
exit 1 )
echo "${temp_file}"
}
function encrypt_files_recursively () {
# set working directory
local work_directory=$( ui_get_local_folder )
cd "${work_directory}"
pwd
# prompt for user password | will be applied to all files
local user_password=$( ui_get_password )
# create csvfile and headers
local csv_file=$( create_temp_file )
local timestamp=$( current_timestamp )
local csv_filename="file.list.${timestamp}.csv"
echo "file,id" >> ${csv_file}
# create tar and gpg output folder
for i in *; do
local original_file=${i}
local file_id=$( get_random_alphanum 30 )
local current_file=${i}
local tar_file="archive.${file_id}.tar"
echo "${original_file},${file_id}" >> ${csv_file}
echo ${tar_file}
tar -cf "${tar_file}" "${original_file}"
echo "${user_password}" | \
gpg --passphrase-fd 0 \
-c "${tar_file}"
done
# create output directory for archive.tar.gpg files.
mkdir output
local output_folder="$(pwd)/output/"
# remove tar files and move gpg files into output folder.
rm -rf *.tar
mv *.gpg "${output_folder}"
# remove temp csv file and place into user selected directory.
mv ${csv_file} "$(pwd)/${csv_filename}"
rm -rf "${csv_file}"
}
# entry to main function
function main() {
echo $( encrypt_files_recursively )
}
# call main function and pass all paramaters to functions
echo $( main "${@}" )