feat: Add PID file

This commit is contained in:
ge 2022-06-07 23:26:46 +03:00
parent f18b9627d5
commit 9eb7f31130
2 changed files with 26 additions and 5 deletions

View File

@ -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. -c, --config config file.
-v, --verbose verbose output. -v, --verbose verbose output.
-l, --log-file log file. -l, --log-file log file.
-p, --pid-file PID file.
-h, --help print this help messagea and exit. -h, --help print this help messagea and exit.
-V, --version print version and exit. -V, --version print version and exit.

View File

@ -21,10 +21,12 @@ __config=
__verbose= __verbose=
__log_file='./log.txt' __log_file='./log.txt'
__log_date_fmt='%d/%b/%Y:%H:%M:%S %z' __log_date_fmt='%d/%b/%Y:%H:%M:%S %z'
__pid_file='/tmp/boring-backup.pid'
__tar_options='-acf' __tar_options='-acf'
__tar_exclude= __tar_exclude=
__name_date_fmt='_%Y%m%d-%H%M' __name_date_fmt='_%Y%m%d-%H%M'
__compression='gzip' __compression='gzip'
LIBRARY="${LIBRARY:-./lib}" LIBRARY="${LIBRARY:-./lib}"
# Source library # Source library
@ -39,12 +41,13 @@ print_help() {
cat <<- EOF cat <<- EOF
Backup files and databases. Backup files and databases.
Usage: $0 [-cvlhV] FILES.. Usage: $0 [-cvlphV] FILES..
Options: Options:
-c, --config config file. -c, --config config file.
-v, --verbose verbose output. -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. -h, --help print this help messagea and exit.
-V, --version print version and exit. -V, --version print version and exit.
@ -101,6 +104,7 @@ while (( "$#" )); do
-c|--config|--config=*) optval "$1" "$2"; __config="$val"; shift "$sft";; -c|--config|--config=*) optval "$1" "$2"; __config="$val"; shift "$sft";;
-v|--verbose) __verbose=1; shift;; -v|--verbose) __verbose=1; shift;;
-l|--log-file|--log-file=*) optval "$1" "$2"; __log_file="$val"; shift "$sft";; -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;; -h|--help) print_help; exit 1;;
-V|--version) echo "$__version"; exit 0;; -V|--version) echo "$__version"; exit 0;;
-*) echo "Error: Unknown option: $1" >&2; exit 1;; -*) echo "Error: Unknown option: $1" >&2; exit 1;;
@ -112,19 +116,32 @@ done
# * Do backups # # * 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. # Scripts counter.
__count=${#__args[@]} # count __count=${#__args[@]} # count
__iter=1 # iterator __iter=1 # iterator
# Startup log. # Startup log.
date +'Start: %d %b %Y %T %z' date +'Start: %d %b %Y %T %z'
log "Backup STARTED"
log -p "Library path: $LIBRARY" log -p "Library path: $LIBRARY"
log -p "Log file: $__log_file" log -p "Log file: $__log_file"
log -p "Configuration file: $([ "$__config" ] || echo not specified && echo "$__config")" log -p "Configuration file: $([ "$__config" ] || echo not specified && echo "$__config")"
log "Scripts to process (${__count}): ${__args[@]}" log "Scripts to process (${__count}): ${__args[@]}"
for script in "${__args[@]}"; do for script in "${__args[@]}"; do
# Initialise variables # Initialise variables
__user_script="$script" __user_script="$script"
@ -176,3 +193,6 @@ done
log "Backup FINISHED" log "Backup FINISHED"
echo -e "\nBackup [Done]" echo -e "\nBackup [Done]"
# Remove PID file
rm $__pid_file