From 9eb7f31130639bb1ab74680f0791fbf97542f973 Mon Sep 17 00:00:00 2001 From: ge Date: Tue, 7 Jun 2022 23:26:46 +0300 Subject: [PATCH] feat: Add PID file --- docs/boring-backup.1.rst | 3 ++- src/boring-backup | 28 ++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/docs/boring-backup.1.rst b/docs/boring-backup.1.rst index 5acc92f..83697e5 100644 --- a/docs/boring-backup.1.rst +++ b/docs/boring-backup.1.rst @@ -15,7 +15,7 @@ backup files and databases. Синопсис -------- -``boring-backup`` [-cvlhV] FILES.. +``boring-backup`` [-cvlphV] FILES.. Описание -------- @@ -28,6 +28,7 @@ boring-backup - это расширяемая утилита для резерв -c, --config config file. -v, --verbose verbose output. -l, --log-file log file. +-p, --pid-file PID file. -h, --help print this help messagea and exit. -V, --version print version and exit. diff --git a/src/boring-backup b/src/boring-backup index d0d7f63..8283a72 100755 --- a/src/boring-backup +++ b/src/boring-backup @@ -21,10 +21,12 @@ __config= __verbose= __log_file='./log.txt' __log_date_fmt='%d/%b/%Y:%H:%M:%S %z' +__pid_file='/tmp/boring-backup.pid' __tar_options='-acf' __tar_exclude= __name_date_fmt='_%Y%m%d-%H%M' __compression='gzip' + LIBRARY="${LIBRARY:-./lib}" # Source library @@ -39,12 +41,13 @@ print_help() { cat <<- EOF Backup files and databases. -Usage: $0 [-cvlhV] FILES.. +Usage: $0 [-cvlphV] FILES.. Options: -c, --config config file. -v, --verbose verbose output. - -l, --log-file log file. + -l, --log-file log file [default: $__log_file] + -p, --pid-file PID file [default: $__pid_file] -h, --help print this help messagea and exit. -V, --version print version and exit. @@ -101,6 +104,7 @@ while (( "$#" )); do -c|--config|--config=*) optval "$1" "$2"; __config="$val"; shift "$sft";; -v|--verbose) __verbose=1; shift;; -l|--log-file|--log-file=*) optval "$1" "$2"; __log_file="$val"; shift "$sft";; + -p|--pid-file|--pid-file=*) optval "$1" "$2"; __pid_file="$val"; shift "$sft";; -h|--help) print_help; exit 1;; -V|--version) echo "$__version"; exit 0;; -*) echo "Error: Unknown option: $1" >&2; exit 1;; @@ -112,19 +116,32 @@ done # * Do backups # # ---------------------------------------------------------- # +log "Backup STARTED" + +# Check PID file +if [ -e $__pid_file ]; then + if [ -z "$(ps ax -o pid | grep "$(cat $__pid_file)")" ]; then + err "Process $(cat $__pid_file) died." + rm $__pid_file + else + err -e "Process $(cat $__pid_file) still running."; + fi +fi + +# Touch PID file +echo $$ > $__pid_file + # Scripts counter. __count=${#__args[@]} # count __iter=1 # iterator # Startup log. date +'Start: %d %b %Y %T %z' -log "Backup STARTED" log -p "Library path: $LIBRARY" log -p "Log file: $__log_file" log -p "Configuration file: $([ "$__config" ] || echo not specified && echo "$__config")" log "Scripts to process (${__count}): ${__args[@]}" - for script in "${__args[@]}"; do # Initialise variables __user_script="$script" @@ -176,3 +193,6 @@ done log "Backup FINISHED" echo -e "\nBackup [Done]" + +# Remove PID file +rm $__pid_file