09-15-2018, 05:42 PM
(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.
This was actually a lot of fun for me. This is why I like Linux, and the Linux community.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
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.