11#! /bin/bash
2+ #
3+ # Copyright (c) 2025 Ronan Le Meillat - SCTG Development
4+ #
5+ # Permission is hereby granted, free of charge, to anyone obtaining a copy
6+ # of this software and associated documentation files (the "Software"), to deal
7+ # in the Software without restriction, including without limitation the rights
8+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+ # copies of the Software, and to permit persons to whom the Software is
10+ # furnished to do so, subject to the following conditions:
11+ #
12+ # The above copyright notice and this permission notice shall be included in all
13+ # copies or substantial portions of the Software.
14+ #
15+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+ # SOFTWARE.
22+
23+ # =============================================================================
24+ # Secure Restore Script for Sensitive Data
25+ # =============================================================================
26+ #
27+ # This script decrypts and displays the contents of an encrypted backup archive
28+ # created by the store_sensitive_datas script. It provides a preview of what
29+ # will be restored without actually performing the restoration.
30+ #
31+ # The restore process:
32+ # 1. Validates PROJECT_ROOT environment variable
33+ # 2. Ensures .gitignore excludes the archive from version control
34+ # 3. Decrypts the AES-256-CBC encrypted archive
35+ # 4. Verifies archive integrity using SHA256 hash
36+ # 5. Lists the contents of the decrypted archive
37+ # 6. Provides manual restoration instructions
38+ #
39+ # Requirements:
40+ # - PROJECT_ROOT environment variable must be set
41+ # - CRYPTOKEN environment variable must be set for decryption (if encrypted)
42+ # - OpenSSL must be installed
43+ # - tar must be installed
44+ # - Encrypted backup file must exist: _sensitive_datas/_sensitive_datas.tar.xz.enc
45+ #
46+ # Usage:
47+ # export PROJECT_ROOT="/path/to/project"
48+ # export CRYPTOKEN="your-encryption-key" # Only if backup was encrypted
49+ # ./restore_sensitive_datas
50+ #
51+ # Note: This script only decrypts and shows contents. Manual restoration required.
52+ # =============================================================================
53+
54+ # Configuration Section
55+ # ====================
56+
57+ # Encryption configuration (must match store_sensitive_datas)
58+ # AES-256-CBC with PBKDF2 provides strong encryption with key derivation
59+ # Note: AES-GCM would be preferred for authenticated encryption, but may not be
60+ # supported in all OpenSSL versions. AES-CBC with PBKDF2 is widely compatible.
61+ CIPHER_ALGO=" aes-256-cbc"
62+
63+ # PBKDF2 iterations for key derivation (must match encryption settings)
64+ PBKDF2_ITERATIONS=100000
65+
66+ # =============================================================================
67+ # Main Script Logic
68+ # =============================================================================
69+
70+ # .env file is located one level from this script
71+ # load it
72+ SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
73+ if [ -f " $SCRIPT_DIR /../.env" ]; then
74+ set -a
75+ source " $SCRIPT_DIR /../.env"
76+ set +a
77+ fi
78+
79+ # Step 1: Validate PROJECT_ROOT
80+ # -----------------------------
281if [ ! -n " $PROJECT_ROOT " ]; then
3- echo " If you restore for a cloned repo you may declare a PROJECT_ROOT environment variable before running this"
82+ echo " INFO: PROJECT_ROOT environment variable not set"
83+ echo " For restoring in a cloned repository, set PROJECT_ROOT to the project root directory"
84+ echo " Example: export PROJECT_ROOT=\" /path/to/project\" "
485 exit 0
586fi
87+
688echo " PROJECT_ROOT is set to '$PROJECT_ROOT '"
89+
90+ # Interactive confirmation (can be disabled for automation)
91+ echo " This will decrypt and show the contents of the backup archive."
792read -p " Press any key to continue... " -n1 -s
93+ echo " " # New line after user input
94+
95+ # Save current directory and navigate to home (safety measure)
896PWD=` pwd`
997cd ~
10- # 1-decrypt
98+
99+ # Step 2: Ensure .gitignore excludes the archive
100+ # -----------------------------------------------
101+ GITIGNORE_FILE=" $PROJECT_ROOT /_sensitive_datas/.gitignore"
102+ if [ ! -f " $GITIGNORE_FILE " ]; then
103+ echo " Creating .gitignore file to exclude sensitive archive..."
104+ # Ensure the directory exists
105+ mkdir -p " $PROJECT_ROOT /_sensitive_datas"
106+ echo " _sensitive_datas.tar.xz" > " $GITIGNORE_FILE "
107+ echo " ✓ Created $GITIGNORE_FILE with archive exclusion rules"
108+ elif ! grep -q " ^_sensitive_datas\.tar\.xz$" " $GITIGNORE_FILE " ; then
109+ echo " Adding archive exclusion to existing .gitignore..."
110+ echo " _sensitive_datas.tar.xz" >> " $GITIGNORE_FILE "
111+ echo " ✓ Updated $GITIGNORE_FILE with archive exclusion rules"
112+ else
113+ echo " ✓ Archive exclusion already present in .gitignore"
114+ fi
115+
116+ # Step 3: Decrypt the archive
117+ # ---------------------------
118+ echo " Decrypting backup archive..."
119+
120+ # Build decryption command based on whether CRYPTOKEN is provided
11121DECRYPT=" "
12122if [ -n " $CRYPTOKEN " ]; then
13123 DECRYPT=" -pass pass:$CRYPTOKEN "
14- echo " Decrypting with 'openssl aes-256-cbc -a -d -md sha256 $DECRYPT -in $PROJECT_ROOT /_sensitive_datas/_sensitive_datas.tar.xz.enc -out $PROJECT_ROOT /_sensitive_datas/_sensitive_datas.tar.xz'"
124+ echo " Using encryption key for decryption..."
125+ echo " Command: openssl enc -${CIPHER_ALGO} -d -pbkdf2 -iter ${PBKDF2_ITERATIONS} $DECRYPT -in $PROJECT_ROOT /_sensitive_datas/_sensitive_datas.tar.xz.enc -out $PROJECT_ROOT /_sensitive_datas/_sensitive_datas.tar.xz"
126+ else
127+ echo " No encryption key provided - attempting unencrypted restore..."
128+ fi
129+
130+ # Perform the decryption
131+ openssl enc -${CIPHER_ALGO} -d -pbkdf2 -iter ${PBKDF2_ITERATIONS} $DECRYPT \
132+ -in $PROJECT_ROOT /_sensitive_datas/_sensitive_datas.tar.xz.enc \
133+ -out $PROJECT_ROOT /_sensitive_datas/_sensitive_datas.tar.xz
134+
135+ # Check if decryption was successful
136+ if [ $? -ne 0 ]; then
137+ echo " ERROR: Decryption failed!"
138+ echo " Possible causes:"
139+ echo " - Wrong encryption key (CRYPTOKEN)"
140+ echo " - Corrupted backup file"
141+ echo " - File not found: $PROJECT_ROOT /_sensitive_datas/_sensitive_datas.tar.xz.enc"
142+ exit 1
143+ fi
144+
145+ # Step 4: Verify archive integrity
146+ # ----------------------------------
147+ echo " Verifying archive integrity..."
148+ if [ -f " $PROJECT_ROOT /_sensitive_datas/_sensitive_datas.tar.xz.sha256" ]; then
149+ EXPECTED_HASH=$( cat " $PROJECT_ROOT /_sensitive_datas/_sensitive_datas.tar.xz.sha256" )
150+ ACTUAL_HASH=$( sha256sum " $PROJECT_ROOT /_sensitive_datas/_sensitive_datas.tar.xz" | cut -d' ' -f1)
151+
152+ if [ " $EXPECTED_HASH " != " $ACTUAL_HASH " ]; then
153+ echo " ⚠️ WARNING: Archive integrity check FAILED!"
154+ echo " Expected: $EXPECTED_HASH "
155+ echo " Actual: $ACTUAL_HASH "
156+ echo " The archive may be corrupted. Proceed with caution!"
157+ else
158+ echo " ✓ Archive integrity verified"
159+ fi
15160else
16- DECRYPT= " "
161+ echo " ⚠️ WARNING: No integrity hash file found. Cannot verify archive integrity. "
17162fi
18- openssl aes-256-cbc -a -d -md sha256 $DECRYPT -in $PROJECT_ROOT /_sensitive_datas/_sensitive_datas.tar.xz.enc -out $PROJECT_ROOT /_sensitive_datas/_sensitive_datas.tar.xz
19- # 2-show content
20- echo " ++++++++++++++++++++++++++++++++++++++++++++++++++"
21- echo " _sensitive_datas/_sensitive_datas.tar.xz contains:"
163+
164+ # Step 5: Display archive contents
165+ # --------------------------------
166+ echo " "
167+ echo " =================================================================================="
168+ echo " ✓ Decryption successful! Archive contents:"
169+ echo " =================================================================================="
22170tar -tvJf $PROJECT_ROOT /_sensitive_datas/_sensitive_datas.tar.xz
23- # 3 show help
24- echo " if you want to restore enter:"
25- echo " cd $PROJECT_ROOT && tar -xvJf $PROJECT_ROOT /_sensitive_datas/_sensitive_datas.tar.xz && rm $PROJECT_ROOT /_sensitive_datas/_sensitive_datas.tar.xz"
171+
172+ # Step 6: Provide restoration instructions
173+ # ----------------------------------------
174+ echo " "
175+ echo " =================================================================================="
176+ echo " RESTORATION INSTRUCTIONS"
177+ echo " =================================================================================="
178+ echo " To complete the restoration, run these commands manually:"
179+ echo " "
180+ echo " cd $PROJECT_ROOT "
181+ echo " tar -xvJf $PROJECT_ROOT /_sensitive_datas/_sensitive_datas.tar.xz"
182+ echo " rm $PROJECT_ROOT /_sensitive_datas/_sensitive_datas.tar.xz"
183+ echo " "
184+ echo " WARNING: This will overwrite existing files with the same names!"
185+ echo " =================================================================================="
0 commit comments