-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt.sh
More file actions
executable file
·76 lines (60 loc) · 2.07 KB
/
decrypt.sh
File metadata and controls
executable file
·76 lines (60 loc) · 2.07 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
#!/bin/bash
# Load configuration file
CONFIG_FILE="${1:-config.sh}"
source "$CONFIG_FILE"
# Check location on decrypt.sh for decrypt.log
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# Location for decrypt.log
DECRYPT_LOG_FILE="${SCRIPT_DIR}/decrypt.log"
exec >> "$DECRYPT_LOG_FILE" 2>&1
echo "========== START DECRYPT: $(date) =========="
ENCRYPTED_FILE="$2"
DECRYPTED_DIR="$3"
# Function to check if correct number of arguments is provided
check_arguments() {
if [ "$#" -ne 3 ]; then
echo "Usage: $0 config.sh /path/to/backup_file.tar.enc /destination/dir"
exit 1
fi
}
# Function to check if encrypted file and destination directory exist
check_files() {
if [ ! -f "$ENCRYPTED_FILE" ]; then
echo "[ERROR] Encrypted file '$ENCRYPTED_FILE' does not exist or is not accessible."
exit 1
fi
if [ ! -d "$DECRYPTED_DIR" ]; then
echo "[ERROR] Destination directory '$DECRYPTED_DIR' does not exist."
exit 1
fi
}
# Function to decrypt and extract the backup file
decrypt_file() {
echo "[INFO] Starting decryption of $ENCRYPTED_FILE ..."
openssl enc -d -aes-256-cbc -pbkdf2 \
-in "$ENCRYPTED_FILE" \
-pass pass:"$ENCRYPTION_PASSWORD" \
| tar -xzvf - -C "$DECRYPTED_DIR"
if [ $? -eq 0 ]; then
echo "[INFO] Decryption and extraction completed successfully."
else
echo "[ERROR] Decryption or extraction failed."
exit 1
fi
}
# Function to check the size of backup.log. If it is greater than 10 MB, create a new log file and rename the old one with the current date plus .old.
check_size_file_log(){
if [ -f "$DECRYPT_LOG_FILE" ] && [ "$(stat -c%s "$DECRYPT_LOG_FILE")" -gt 10000000 ]; then
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
ROTATED_FILE="${DECRYPT_LOG_FILE}_${TIMESTAMP}.old"
mv "$DECRYPT_LOG_FILE" "$ROTATED_FILE"
touch "$DECRYPT_LOG_FILE"
echo "[INFO] Log file rotated: $ROTATED_FILE"
fi
}
# Run all functions
check_arguments "$@"
check_files
decrypt_file
check_size_file_log
echo "========== DECRYPT COMPLETED: Sun Jul 27 17:19:02 UTC 2025 =========="