Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #set -x # uncomment/comment to enable/disable debug mode
- # name: ddwrt-ovpn-client-backup.sh
- # version: 1.0.0b (beta), 18-aug-2020, by eibgrad
- # purpose: backup/restore openvpn client configuration
- #
- # usage: ddwrt-ovpn-client-backup.sh name [options]
- # options:
- # --backup (optional/default) backup openvpn client config by name
- # --restore restore named openvpn client config
- # --nocommit do NOT commit restored openvpn client config to nvram
- #
- # example usage:
- # ddwrt-ovpn-client-backup.sh expressvpn
- # ddwrt-ovpn-client-backup.sh expressvpn --backup
- # ddwrt-ovpn-client-backup.sh expressvpn --restore
- # ddwrt-ovpn-client-backup.sh expressvpn --restore --nocommit
- # ------------------------------ BEGIN OPTIONS ------------------------------- #
- # configuration directory (optional: use $name to embed backup name)
- CONFIG_DIR='/jffs/openvpn/client/$name/config'
- # ------------------------------- END OPTIONS -------------------------------- #
- # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
- backup() {
- _query() {
- local reply
- read -p "$1 " reply
- [ "$reply" ] && echo "$reply" || echo "$2"
- }
- # obtain permission to overwrite existing files
- if [ -d $CONFIG_DIR ] && [ "$(ls -A $CONFIG_DIR)" ]; then
- while :; do
- case $(_query 'Overwrite existing files (yes/[no])?' no) in
- yes) break;;
- no) { echo 'Aborted.'; exit 0; };;
- esac
- done
- fi
- # create/cleanup store
- [ -d $CONFIG_DIR ] && rm -f $CONFIG_DIR/* || mkdir -p $CONFIG_DIR
- # backup openvpn client configuration
- nvram show 2>/dev/null | grep -Eo '^openvpncl[^=]*' | \
- while read var; do
- nvram get $var | tr -d '\r' > $CONFIG_DIR/$var
- done
- }
- restore() {
- [ -d $CONFIG_DIR ] || \
- { echo "ERROR: Directory <$CONFIG_DIR> not found."; exit 1; }
- [ "$(ls -A $CONFIG_DIR)" ] || \
- { echo "ERROR: Directory <$CONFIG_DIR> is empty."; exit 1; }
- # restore openvpn client configuration
- find $CONFIG_DIR -type f -exec sh -c 'nvram set $(basename {})="$(cat {})"' \;
- # commit changes (optional)
- [ ${NOCOMMIT+x} ] || nvram commit
- }
- [ "$1" ] || { echo 'Nothing to do.'; exit 0; }
- # assume backup mode (the default) and name argument
- mode='backup'; name="${1//[[:space:]]/}"; shift
- # process command line options/arguments
- while [ $# -gt 0 ]; do
- case $1 in
- --backup ) mode='backup';;
- --restore ) mode='restore';;
- --nocommit) NOCOMMIT=;;
- *) { echo "ERROR: Invalid option/argument: $1"; exit 1; }
- esac
- shift
- done
- # validate input
- [ "$name" ] || { echo 'ERROR: Missing name argument.'; exit 1; }
- [[ "$mode" == 'backup' && "${NOCOMMIT+x}" ]] && \
- echo 'WARNING: --nocommit ignored; only valid with --restore option.'
- # backup name may be embedded in configuration directory; resolve it
- eval CONFIG_DIR="${CONFIG_DIR//[[:space:]]/}"
- # perform the requested operation
- $mode
- echo 'Done.'
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement