Advertisement
Eeems

backup.sh

Feb 17th, 2020
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.21 KB | None | 0 0
  1. readonly ROOT="/home/eeems"
  2. readonly PIDFILE="$ROOT/var/backup.pid";
  3. log(){
  4.   echo "[$(date --iso-8601=seconds)] $1";
  5. }
  6. checkpid(){
  7.   if [ -f "$PIDFILE" ]; then
  8.     local PID=$(cat "$PIDFILE");
  9.     ps -p $PID > /dev/null 2>&1;
  10.     if [ $? -eq 0 ]; then
  11.       log "Process already running";
  12.       return 1;
  13.     else
  14.       log "Found old PID file";
  15.     fi;
  16.   fi;
  17.   echo $$ > "$PIDFILE";
  18.   if [ $? -ne 0 ]; then
  19.     log "Could not create PID file";
  20.     return 1;
  21.   fi;
  22.   trap "rm -f $PIDFILE" EXIT
  23.   return 0;
  24. }
  25. backup(){
  26.   if [ ! -f $ROOT/etc/backup.d/$1.sh ];then
  27.     log "Backup script for $1 doesn't exist";
  28.     return 1;
  29.   fi;
  30.   log "Starting backup of $1...";
  31.   if ! $ROOT/etc/backup.d/$1.sh > $ROOT/var/log/backup.d/$1.log 2>&1;then
  32.     log "Backup of $1 failed!";
  33.     return 1;
  34.   fi;
  35.   log "Finished backup of $1";
  36.   return 0;
  37. }
  38. cleanup(){
  39.   unset -f log;
  40.   unset -f checkpid;
  41.   unset -f backup;
  42.   unset -f cleanup;
  43.   unset -f main;
  44. }
  45. main(){
  46.   if ! checkpid;then
  47.     cleanup;
  48.     exit 1;
  49.   fi;
  50.   log "Starting backups";
  51.   backup eeems &
  52.   backup omnimaga &
  53.   log "Waiting for jobs...";
  54.   wait;
  55.   log "Finished waiting";
  56.   backup whiterabbit;
  57.   log "Done";
  58.   cleanup;
  59. }
  60. main;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement