Skip to content

Commit e369048

Browse files
allow GPG encryption
1 parent f541425 commit e369048

1 file changed

Lines changed: 105 additions & 5 deletions

File tree

mirror-s3.sh

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,91 @@ if [ -n "$RCLONE_TMPL" ]; then
77
fi
88
cat $TMPL | envsubst > /etc/rclone.conf
99

10+
# GPG encryption setup
11+
if [ -n "$GPG_KEY_ID" ]; then
12+
echo "== GPG ENCRYPTION ENABLED (Key ID: $GPG_KEY_ID) =="
13+
14+
# Import GPG key from environment if provided
15+
if [ -n "$GPG_PRIVATE_KEY" ]; then
16+
echo "=> Importing GPG private key..."
17+
echo "$GPG_PRIVATE_KEY" | gpg --batch --import
18+
if [ $? -eq 0 ]; then
19+
echo "=> GPG private key imported successfully"
20+
else
21+
echo "=> ERROR: Failed to import GPG private key"
22+
exit 1
23+
fi
24+
fi
25+
26+
if [ -n "$GPG_PUBLIC_KEY" ]; then
27+
echo "=> Importing GPG public key..."
28+
echo "$GPG_PUBLIC_KEY" | gpg --batch --import
29+
if [ $? -eq 0 ]; then
30+
echo "=> GPG public key imported successfully"
31+
else
32+
echo "=> ERROR: Failed to import GPG public key"
33+
exit 1
34+
fi
35+
fi
36+
37+
# Verify the key exists
38+
if ! gpg --list-keys "$GPG_KEY_ID" > /dev/null 2>&1; then
39+
echo "=> ERROR: GPG key $GPG_KEY_ID not found in keyring"
40+
echo "=> Please import the key or check GPG_KEY_ID"
41+
exit 1
42+
fi
43+
44+
# Set up GPG trust (needed for encryption)
45+
echo "=> Setting up GPG trust for key $GPG_KEY_ID"
46+
echo "$GPG_KEY_ID:6:" | gpg --batch --import-ownertrust
47+
48+
# Create temporary directory for GPG operations
49+
GPG_TEMP_DIR="/tmp/gpg_sync"
50+
mkdir -p "$GPG_TEMP_DIR"
51+
52+
GPG_ENABLED=true
53+
else
54+
echo "== GPG ENCRYPTION DISABLED (no GPG_KEY_ID set) =="
55+
GPG_ENABLED=false
56+
fi
57+
58+
# GPG encryption functions
59+
encrypt_and_sync() {
60+
local source="$1"
61+
local destination="$2"
62+
local compare_dest="$3"
63+
64+
echo "=> Syncing with GPG encryption..."
65+
66+
# Create a temporary local directory for encrypted files
67+
local encrypted_dir="$GPG_TEMP_DIR/encrypted"
68+
mkdir -p "$encrypted_dir"
69+
70+
# Download files from source
71+
echo "=> Downloading files from source..."
72+
rclone --progress $BW_LIMIT --config=/etc/rclone.conf sync "$source" "$GPG_TEMP_DIR/plain"
73+
74+
# Encrypt all files
75+
echo "=> Encrypting files with GPG..."
76+
find "$GPG_TEMP_DIR/plain" -type f | while read file; do
77+
relative_path="${file#$GPG_TEMP_DIR/plain/}"
78+
encrypted_file="$encrypted_dir/$relative_path.gpg"
79+
mkdir -p "$(dirname "$encrypted_file")"
80+
gpg --batch --trust-model always --encrypt --recipient "$GPG_KEY_ID" --output "$encrypted_file" "$file"
81+
done
82+
83+
# Upload encrypted files to destination
84+
echo "=> Uploading encrypted files to destination..."
85+
if [ -n "$compare_dest" ]; then
86+
rclone --progress $BW_LIMIT --config=/etc/rclone.conf sync "$encrypted_dir" "$destination" --compare-dest="$compare_dest"
87+
else
88+
rclone --progress $BW_LIMIT --config=/etc/rclone.conf sync "$encrypted_dir" "$destination"
89+
fi
90+
91+
# Clean up temporary files
92+
rm -rf "$GPG_TEMP_DIR/plain" "$encrypted_dir"
93+
}
94+
1095
echo "Running rclone sync..."
1196
DEBUG=""
1297

@@ -29,12 +114,27 @@ fi
29114

30115
if [ -n "$DO_ATOMIC" ]; then
31116
echo "== ATOMIC MODE ENABLED =="
32-
echo "=> Syncing from source..."
33-
rclone --progress $BW_LIMIT --config=/etc/rclone.conf sync sync_src:${SOURCE_BUCKET} sync_dst:${DESTINATION_TMP_BUCKET} --compare-dest=sync_dst:${DESTINATION_BUCKET}
34-
echo "=> Moving..."
35-
rclone --config=/etc/rclone.conf move sync_dst:${DESTINATION_TMP_BUCKET} sync_dst:${DESTINATION_BUCKET}
117+
if [ "$GPG_ENABLED" = true ]; then
118+
encrypt_and_sync "sync_src:${SOURCE_BUCKET}" "sync_dst:${DESTINATION_TMP_BUCKET}" "sync_dst:${DESTINATION_BUCKET}"
119+
echo "=> Moving encrypted files..."
120+
rclone --config=/etc/rclone.conf move sync_dst:${DESTINATION_TMP_BUCKET} sync_dst:${DESTINATION_BUCKET}
121+
else
122+
echo "=> Syncing from source..."
123+
rclone --progress $BW_LIMIT --config=/etc/rclone.conf sync sync_src:${SOURCE_BUCKET} sync_dst:${DESTINATION_TMP_BUCKET} --compare-dest=sync_dst:${DESTINATION_BUCKET}
124+
echo "=> Moving..."
125+
rclone --config=/etc/rclone.conf move sync_dst:${DESTINATION_TMP_BUCKET} sync_dst:${DESTINATION_BUCKET}
126+
fi
36127
else
37128
echo "=> Syncing from source..."
38-
rclone --progress $BW_LIMIT --config=/etc/rclone.conf sync sync_src:${SOURCE_BUCKET} sync_dst:${DESTINATION_BUCKET}
129+
if [ "$GPG_ENABLED" = true ]; then
130+
encrypt_and_sync "sync_src:${SOURCE_BUCKET}" "sync_dst:${DESTINATION_BUCKET}"
131+
else
132+
rclone --progress $BW_LIMIT --config=/etc/rclone.conf sync sync_src:${SOURCE_BUCKET} sync_dst:${DESTINATION_BUCKET}
133+
fi
134+
fi
135+
136+
# Clean up GPG temp directory if it exists
137+
if [ "$GPG_ENABLED" = true ]; then
138+
rm -rf "$GPG_TEMP_DIR"
39139
fi
40140
curl ${HEALTHCHECK_URL}

0 commit comments

Comments
 (0)