-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbatch-cryptor.sh
More file actions
executable file
·100 lines (76 loc) · 2.36 KB
/
Copy pathbatch-cryptor.sh
File metadata and controls
executable file
·100 lines (76 loc) · 2.36 KB
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
#!/bin/bash
# Author: Ray Winkelman, raywinkelman@gmail.com
# Date: July 2017
# This program has 2 modes.
# These modes dictate the general flow of control.
# They are encrypt mode, and decrypt mode.
# Encrypt mode is the default mode, unless decrypt is specified by the -d flag argument.
### GLOBALS
source_dir=""
target_dir=""
mode="E"
passphrase=""
### FUNCTIONS
# Prints a formatted error to the console.
error() {
echo -e "\e[31mERROR\e[0m: batch-cryptor.sh: $1"
exit 1
}
while getopts s:t:d option
do
case "${option}"
in
s) source_dir=${OPTARG};;
t) target_dir=${OPTARG};;
d) mode="D";;
\?) error "Invalid option: -$OPTARG";;
esac
done
# Get the passphrase.
read -s -p "Enter a Passphrase: " passphrase
# Not null checks.
if [[ -z $source_dir ]] ; then error "A Source Directory (-s) is required." ; fi
if [[ -z $target_dir ]] ; then error "A Target Directory (-t) is required." ; fi
if [[ -z $passphrase ]] ; then error "A Passphrase (-p) can't be blank." ; fi
# Filesystem checks.
if [[ ! -d $source_dir ]] ; then error "Source Dir: $source_dir doesn't exist." ; fi
if [[ ! -d $target_dir ]] ; then error "Target Dir: $target_dir doesn't exist." ; fi
if [[ ! -w $target_dir ]] ; then error "Can't write to $target_dir" ; fi
echo -e "This will delete all files in: $target_dir \nDo you wish to proceed?"
select YN in "Yes" "No"; do
case $YN in
Yes ) break;;
No ) exit 1;;
esac
done
find "$target_dir" -mindepth 1 -delete
chmod 755 "$target_dir"
# Recurse into the target and encrypt/decrypt the files.
function crypt()
{
for item in "${1}"/*
do
suffix=${item#$source_dir}
suffix=${suffix%$item}
full_path="$target_dir$suffix"
# If it's a folder, recurse.
if [[ -d $item ]] ; then
mkdir -p "$full_path"
crypt "$item"
# Empty directory.
elif [[ $item == *"/*"* ]] ; then
continue
else
# Encrypt mode
if [[ $mode == "E" ]] ; then
$(dirname $0)/lib/cryptor.sh -f "$item" -t "$full_path" -p "$passphrase" || { exit 1; }
# Decrypt mode
else
$(dirname $0)/lib/cryptor.sh -f "$item" -t "$full_path" -p "$passphrase" -d || { exit 1; }
fi
fi
done
}
crypt "$source_dir"
echo "Done."
exit 0