瀏覽代碼

Added cryptdir.sh

George Jones 7 月之前
父節點
當前提交
a737c66372
共有 1 個文件被更改,包括 38 次插入0 次删除
  1. 38 0
      bin/cryptdir.sh

+ 38 - 0
bin/cryptdir.sh

@@ -0,0 +1,38 @@
+#!/bin/bash
+# Use zip to encrypt all single files CRYPTDIR with PASSWORD
+
+# this is bash; be safe
+set -e
+set -u
+
+# Set up logging functions (die, warn, info ...)
+source $HOME/lib/bash/bashutils.sh
+
+# Get the encryption password from the CRYPTDIR_PASS environment variable
+password=${CRYPTDIR_PASS:-"NONE"}
+
+# Check if password is defined
+if [ $password == "NONE" ]; then
+  die "ERROR: CRYPTDIR_PASS environment variable not defined"
+fi
+
+cryptdir=${CRYPTDIR:-"NONE"}
+
+# Check if password is defined
+if [ $cryptdir == "NONE" ]; then
+  die "ERROR: CRYPTDIR environment variable not defined"
+fi
+
+cd $cryptdir || die "Can't cd to $cryptdir"
+info "encrypting files in $cryptdir"
+
+# Loop through all files in the current directory
+for file in *; do
+  # Check if the file is not a directory and is not already a zip file
+  if [ ! -d "$file" ] && [[ "$file" != *.zip ]]; then
+      # Create a zip file with the same name as the original file with .zip extension
+      info "Encrypting $file ..."
+      zip --encrypt --password "$password" "$file.zip" "$file"
+  fi
+done
+info done