Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cronjob issues with 18.04, it's not working, or i'm not.
#21
(09-15-2018, 05:51 AM)eliasw4u Wrote: last night i was going threw some very old files from 2000 - 2005 and found a script template for my old cron settup. though mostly useless it had some interesting consepts in it. it tried them and now have a single self contained script that sets niceness, checks for battery, gives one chance to cancel, runs the backup, gives a brief notification upon compleation, send the full log to temp, greps for all the usful info, sends that back to my scripts folder. then quietly goes away intill the next run. and with very minor changes can be switch from manual to auto, as well as user, path, and IP


i still need to do some cleanup,  but this is basically done. from here the only changes should be cosmetic. unless  I... well, we find another dumb entry of mine. lol
but here it is.
Code:
#!/bin/bash
#
### MAKE SURE this is the SAME as $LONG_OUTPUT ! ! line #30
exec &>  >(tee -i /tmp/rsync-long.log)
echo ""
echo "##### start of log #####"
echo ""
### setts niceness
renice -n 10 $$
### the next line is for when ran by crontab. it allows zenity to work
export DISPLAY=":0"
# Checks if your on the battery
if which on_ac_power >/dev/null 2>&1; then
   on_ac_power >/dev/null 2>&1
   ON_BATTERY=$?
   [ "$ON_BATTERY" -eq 1 ] && echo "It is not safe to run rsync when on the laptop battery. this script is now closed"&&echo "" && exit 0
fi
## Remote User Name Here
USR1='remote_user'
## Remote IP here
IP1='123.456.7.89'
# other useful variables
MnAt='AUTO'     # is it ran automatically or manually?
mnat='Auto'
MYHOME="/home/$USR1"
DEST="/home/$USR1/FLAT-BAK"
HMdesk="/home/$USR1/Desktop"
HMscr="/home/$USR1/Scripts"
GREP_OUTPUT="/home/$USR1/Scripts/rsync-bkup.log"
LONG_OUTPUT="/tmp/rsync-long.log"
RsYnC="$MYHOME $IP1:$DEST"
TODAY="$(date)"

## one chance to cancel.
zenity \
--question \
--width=200 \
--title="$MnAt Rsync Backup" \
--timeout=15 \
--cancel-label="cancel" \
--ok-label="continue" \
--text="$MnAt Rsync has Started\nYou Have 10 Seconds To cancel."
     if [ "$?" = "1" ]
        then echo "" ; echo "You canceled the rcync backup" ; echo "" ; exit 86
     fi

echo -e "\n#### Starting New $MnAt Rsync Job ####" >> $GREP_OUTPUT

### With The delete-after Option Added and exclude option
### deletes files on remote that are no longer on source.
rsync -avvzp -e 'ssh -p 22' \
--exclude='VirtualBox VMs/' \
--exclude=".local/" \
--exclude="Git-Stuff/" \
--exclude=".bash_history" \
--exclude=".zhistory" \
--exclude=".ssh/" \
--exclude=".git/" \
--exclude="Code/" \
--exclude='BOX/' \
--exclude="htop/" \
--exclude=".mozilla/" \
--exclude=".cache/" \
--ignore-errors \
${RsYnC} \
--delete-after

# fail popup doesn't go away until clicked.
  if [ "$?" = "0" ]
    then  echo "" >> "$GREP_OUTPUT"
          echo -e "${MnAt} BACKUP Success! -${TODAY} \n\n#### Here Is The Output ####" >> "${GREP_OUTPUT}"
#           zenity --notification --width=200 --title="Rsync Backup" --text "          $mnat Rsync \nHas finnshed Successfully."
#           zenity --info --width=200 --title="Rsync Backup" --text "          $mnat Rsync \nHas finnshed Successfully." --timeout=10
     else echo "" >> "${GREP_OUTPUT}"
          echo -e "${MnAt} BACKUP FAILED! -${TODAY} \n\n#### Here Is The Output ####" >> "${GREP_OUTPUT}"
          zenity --info --width=300 --title="Rsync Backup" --text "Rsync Backup Has Failed! \nA Log File Was Made Here. \n$GREP_OUTPUT \n\nOr The Full File Here\n$LONG_OUTPUT"
  fi
  exec 2>&1
###
# Grepit
grep -v "uptodate\|sender\|Rsync is finished\|END OF OUTPUT\|generator\|mapped without a transient parent\|Gtk-WARNING" $LONG_OUTPUT >> $GREP_OUTPUT

echo "##################################################################################################"  >> $GREP_OUTPUT
echo "########################################### END OF OUTPUT ########################################"  >> $GREP_OUTPUT
echo "##################################################################################################"  >> $GREP_OUTPUT
echo "" >&2

exit
This was actually a lot of fun for me. This is why I like Linux, and the Linux community.

I have another script project I am working on for unknown IP addresses. but I will post that under its own thread. Probably  tomorrow.

leon.p, and  deck_luck, you guys are great. thanks for hanging in there with me.

kudos

When checking for the return value of rsync, you use '[ "$?" = "0" ]', but I would advice using '[ "$?" == "0" ]'.
I don't exactly recall how bash parses that, but most programming languages I know would interpret the single
'=' as setting the value of '$?' to '0', a process which also has a return value, which than gets checked by the 'if' statement.

This means that you don't check if the variable is zero, but set if to zero and then check if that worked.
Reply
#22
The if [[ "$?" = "0" ]is the acceptable syntax for checking the "${?}" exit code.   The = is equivalent to == in bash.  Also, you can use an integer evaluation if [[ "${?}" -eq "0" ]] to be more technically precise.
Idea Give a person a fish, and you feed them for a day. Teach a person how to fish, and you feed them for a lifetime. ✝️ Proverbs 4:7 Wisdom is the principal thing; therefore get wisdom: and with all thy getting get understanding.  (Linux Mint 19 XFCE)
Reply
#23
Quote:The table below shows the bash comparison operators for numbers and strings. bash uses textual operators for numbers and symbolic operators for strings.

String         Numeric                                        True if

x = y          x -eq y                                     x is equal to y
x != y        x -ne y                                      x is not equal to y
x < y         x -lt y                                        x is less than y
x <= y      x -le y                                       x is less than or equal to y
x > y        x -gt y                                       x is greater than y
x >= y     x -ge y                                       x is greater than or equal to y
-n x              -                                            x is not null
-z x              -                                            x is null


bash shines in its options for evaluating the properties of files. The table below shows a few of bash’s many file testing and file comparison operators.

Operator                                        True if
-d file                                             file exists and is a directory
-e file                                             file exists
-f file                                              file exists and is a regular file
-r file                                             You have read permission on file
-s file                                             file exists and is not empty
-w file                                           You have write permission on file
file1 -nt file 2                                file 1 is newer than file 2
file1 -ot file 2                                file 1 is older than file 2
https://www.cs.clemson.edu/course/cpsc42...ipting.pdf
A computer without Microsoft is like a piece of chocolate cake without ketchup and mustard.


Telegram @eliasw4u
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)