eibgrad

ddwrt-ovpn-client-backup.sh

Aug 18th, 2020 (edited)
453
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. #set -x # uncomment/comment to enable/disable debug mode
  3.  
  4. #     name: ddwrt-ovpn-client-backup.sh
  5. #  version: 1.0.0b (beta), 18-aug-2020, by eibgrad
  6. #  purpose: backup/restore openvpn client configuration
  7. #
  8. #  usage: ddwrt-ovpn-client-backup.sh name [options]
  9. #    options:
  10. #      --backup    (optional/default) backup openvpn client config by name
  11. #      --restore   restore named openvpn client config
  12. #      --nocommit  do NOT commit restored openvpn client config to nvram
  13. #
  14. #  example usage:
  15. #    ddwrt-ovpn-client-backup.sh expressvpn
  16. #    ddwrt-ovpn-client-backup.sh expressvpn --backup
  17. #    ddwrt-ovpn-client-backup.sh expressvpn --restore
  18. #    ddwrt-ovpn-client-backup.sh expressvpn --restore --nocommit
  19.  
  20. # ------------------------------ BEGIN OPTIONS ------------------------------- #
  21.  
  22. # configuration directory (optional: use $name to embed backup name)
  23. CONFIG_DIR='/jffs/openvpn/client/$name/config'
  24.  
  25. # ------------------------------- END OPTIONS -------------------------------- #
  26.  
  27. # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
  28.  
  29. backup() {
  30.     _query() {
  31.         local reply
  32.  
  33.         read -p "$1 " reply
  34.         [ "$reply" ] && echo "$reply" || echo "$2"
  35.     }
  36.  
  37.     # obtain permission to overwrite existing files
  38.     if [ -d $CONFIG_DIR ] && [ "$(ls -A $CONFIG_DIR)" ]; then
  39.         while :; do
  40.             case $(_query 'Overwrite existing files (yes/[no])?' no) in
  41.                 yes) break;;
  42.                  no) { echo 'Aborted.'; exit 0; };;
  43.             esac
  44.         done
  45.     fi
  46.  
  47.     # create/cleanup store
  48.     [ -d $CONFIG_DIR ] && rm -f $CONFIG_DIR/* || mkdir -p $CONFIG_DIR
  49.  
  50.     # backup openvpn client configuration
  51.     nvram show 2>/dev/null | grep -Eo '^openvpncl[^=]*' | \
  52.         while read var; do
  53.             nvram get $var | tr -d '\r' > $CONFIG_DIR/$var
  54.         done
  55. }
  56.  
  57. restore() {
  58.     [ -d $CONFIG_DIR ] || \
  59.         { echo "ERROR: Directory <$CONFIG_DIR> not found."; exit 1; }
  60.  
  61.     [ "$(ls -A $CONFIG_DIR)" ] || \
  62.         { echo "ERROR: Directory <$CONFIG_DIR> is empty."; exit 1; }
  63.  
  64.     # restore openvpn client configuration
  65.     find $CONFIG_DIR -type f -exec sh -c 'nvram set $(basename {})="$(cat {})"' \;
  66.  
  67.     # commit changes (optional)
  68.     [ ${NOCOMMIT+x} ] || nvram commit
  69. }
  70.  
  71. [ "$1" ] || { echo 'Nothing to do.'; exit 0; }
  72.  
  73. # assume backup mode (the default) and name argument
  74. mode='backup'; name="${1//[[:space:]]/}"; shift
  75.  
  76. # process command line options/arguments
  77. while [ $# -gt 0 ]; do
  78.     case $1 in
  79.         --backup  ) mode='backup';;
  80.         --restore ) mode='restore';;
  81.         --nocommit) NOCOMMIT=;;
  82.         *) { echo "ERROR: Invalid option/argument: $1"; exit 1; }
  83.     esac
  84.     shift
  85. done
  86.  
  87. # validate input
  88. [ "$name" ] || { echo 'ERROR: Missing name argument.'; exit 1; }
  89. [[ "$mode" == 'backup' && "${NOCOMMIT+x}" ]] && \
  90.     echo 'WARNING: --nocommit ignored; only valid with --restore option.'
  91.  
  92. # backup name may be embedded in configuration directory; resolve it
  93. eval CONFIG_DIR="${CONFIG_DIR//[[:space:]]/}"
  94.  
  95. # perform the requested operation
  96. $mode
  97.  
  98. echo 'Done.'
  99. exit 0
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×