-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sh
executable file
·120 lines (97 loc) · 3.78 KB
/
main.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
#!/bin/bash
# Declare global environment variable for gpg recipient
# Change to the desired recipient email or key ID or override it during invocation
export GPG_RECIPIENT="[email protected]"
# Function to create a tar archive, compress with zstd, and encrypt with gpg
do_tar_zstd_gpg() {
if [ $# -ne 2 ]; then
echo "Usage: $0 source_dir dest_file"
return 1
fi
local source_dir="$1"
local dest_file="$2"
# Check that source_dir is directory
if [ ! -d "$source_dir" ]; then
echo "Source directory does not exist: $source_dir"
return 1
fi
# Check that source_dir is accessible (readable) to the Bash user
if [ ! -r "$source_dir" ]; then
echo "Source directory is not accessible: $source_dir"
return 1
fi
# Create the destination directory if it doesn't exist
mkdir -p "$(dirname "$dest_file")"
# Create the tar archive, compress with zstd, and encrypt with gpg
tar cf - -C "$source_dir" . | zstd - | gpg --encrypt --recipient "$GPG_RECIPIENT" --output "$dest_file" -
}
# Function to decrypt, decompress, and extract files from an encrypted compressed archive
undo_tar_zstd_gpg() {
if [ $# -ne 2 ]; then
echo "Usage: $0 source_file dest_dir"
return 1
fi
local source_file="$1"
local dest_dir="$2"
# Check that source_file exists
if [ ! -f "$source_file" ]; then
echo "Source file does not exist: $source_file"
return 1
fi
# Check that source_file is accessible (readable) to the Bash user
if [ ! -r "$source_file" ]; then
echo "Source file is not accessible: $source_file"
return 1
fi
# Create the destination directory if it doesn't exist
mkdir -p "$dest_dir"
# Step 1: Decrypt with PGP (gpg), decompress with zstd, and extract with tar
gpg --decrypt --recipient "$GPG_RECIPIENT" --output - "$source_file" | zstd -d | tar xf - -C "$dest_dir"
}
# Show help message
show_help() {
usage=$(cat << EOF
Usage: main.sh do_tar_zstd_gpg source_dir dest_file
main.sh undo_tar_zstd_gpg source_file dest_dir
do_tar_zstd_gpg: Create a tar archive, compress with zstd, and encrypt with gpg
undo_tar_zstd_gpg: Decrypt, decompress, and extract files from an encrypted compressed archive
source_dir: The source directory to be archived
source_file: The source archive file to be extracted
dest_file: The destination archive file to be created
dest_dir: The destination directory for the extracted files
Examples:
Put all files and dirs which are inside ~/.local/share/TelegramDesktop directory
into encrypted compressed archive ~/tg.tar.zst.gpg:
./main.sh do_tar_zstd_gpg ~/.local/share/TelegramDesktop ~/tg.tar.zst.gpg
Put all files and dirs which are inside encrypted compressed archive ~/tg.tar.zst.gpg
into directory ~/.local/share/TelegramDesktop:
rm -rf ~/.local/share/TelegramDesktop; ./main.sh undo_tar_zstd_gpg ~/tg.tar.zst.gpg ~/.local/share/TelegramDesktop
Notes:
Env
The GPG recipient email or key ID can be set using the GPG_RECIPIENT environment variable. For example:
GPG_RECIPIENT="[email protected]" main.sh do_tar_zstd_gpg /path/to/source_dir /path/to/dest_file
Archiving
Script does not check if the destination file exists, so it is recommended
that you manually delete the old archive before creating a new one.
Script does not add any extensions, so it is recommended
that you manually add .tar.zst.gpg to the archive file
Extracting
Script does not handle conflicts when extracting files, so it is recommended
that you manually delete the content of destination directory before extracting.
EOF
)
echo "$usage"
}
# Check the first argument and call the appropriate function
case "$1" in
do_tar_zstd_gpg)
do_tar_zstd_gpg "$2" "$3"
;;
undo_tar_zstd_gpg)
undo_tar_zstd_gpg "$2" "$3"
;;
*)
show_help
exit 1
;;
esac