I had a command that was used to see if. It used some arithmetic operators to subtract 1 from the current day. That would give us yesterdays day which we could then use to check if a backup was created then.
day=$(date +%d) ; date=$(($day - 1)) ; echo "yesterday date is $date"
-bash: 08: value too great for base (error token is "08")
The better way
Fortunately there is an easier and more reliable way to do this. Using the date command, you can specify yesterday and it will print out yesterdays date.
There may be a different and easier way to do this, but the main thing to learn here is the ^, $, and [[:digit:]] options.
^ refers to the first part of a line & which is our searched for pattern $ refers to an end part of the line [[:digit:]] searches for, you guessed it. Digits!
The following command reads the incoming 10 digit number form echo and does the following.
the ^ tells it that the pattern needs to match at the beginning of the line [[:digit:]] repeated tells it to search for three consecutive digits (&) tells it to put brackets around the & which is our searched for pattern in the first part. We then pipe that to another sed command which searches for 4 consecutive digits the $ tells it that it needs to be at the end of the line.
echo "1234567890" | sed -e 's/^[[:digit:]][[:digit:]][[:digit:]]/(&) /g' | sed -e 's/[[:digit:]][[:digit:]][[:digit:]][[:digit:]]$/-&/g'
Resulting output is
(123) 456-7890
The following link was helpful while searching what the ^ and $ options do.
You should be able to copy and paste the following in a backup.sh file and then execute from cron. Should work out of the box, but you can change the backup directory and the teams.sh path if needed/wanted.
#!/bin/bash
# LibreNMS backup script
# Jan 1, 2019
lDate=`date +%Y%m%d-%H%M` # local date + hour minute
dDate=`date +%Y%m%d` # todays date
# If you have the teams.sh script, you can trigger a backup notification
ALERT="/home/admin/teams.sh -b"
# Directory to backup to
bDir="/backup"
bName="librenms_backup"
# MySQL settings for tar and sqldump
sqlDir="/var/lib/mysql"
sqlDB="librenms"
sqlUN="root"
sqlPW=""
LOG="${bDir}/${lDate}-${bName}.log"
# Directory that contains data
dDir="/opt/librenms"
# tar LibreNMS dir
# tar SQL dir "the whole thing with the innode files
# sql dump of the db for extra redundancy
if [ -d ${bDir} ]; then
echo "backup dir exist, starting to backup"
else
echo "backup dir not available. Quiting"
exit 1
fi
${ALERT} "Starting backup for ${bName} - `date`"
systemctl stop mariadb httpd
# LibreNMS data backup
tar -zcvf ${bDir}/${lDate}-${bName}.tgz ${dDir}
if [ $? -eq 0 ]; then
echo "Tar succesfully backed up ${bDir}"
else
echo "Tar failed while trying to backup ${dDir}"
echo " ${lDate} - Tar failed while trying to backup ${dDir}" >> ${LOG}
${ALERT} "${lDate} - Tar failed while trying to backup ${dDir}"
fi
# MySQL data backup
tar -zcvf ${bDir}/${lDate}-${bName}-mysql.tgz ${sqlDir}
if [ $? -eq 0 ]; then
echo "Tar succesfully backed up ${sqlDir}"
else
echo "Tar failed while trying to backup ${sqlDir}"
echo " ${lDate} - Tar failed while trying to backup ${sqlDir}" >> ${LOG}
${ALERT} "${lDate} - Tar failed while trying to backup ${sqlDir}"
fi
systemctl start mariadb httpd
sleep 5
# SQL dump
mysqldump -u ${sqlUN} -p'4rfvBHU8!' ${sqlDB} > ${bDir}/${lDate}-${bName}.sql
if [ $? -eq 0 ]; then
echo "MySQL DB dumped"
else
echo "Ran into error while doing sql dump"
echo "${lDate} - Ran into error while doing sql dump" >> ${LOG}
${ALERT} "${lDate} - Ran into error while doing sql dump"
fi
echo "Removing old backups"
if ( ls ${bDir} | grep -q ${dDate} );then
find ${bDir}/* -prune -mtime +31 -exec rm {} \;
else
echo "Looks like there are no backup files! Aborting!!!"
${ALERT} "${lDate} - Error: find failed to find any backup files in backup dir. Aborting!!!"
fi
${ALERT} "Finished backup for ${bName} - `date`"
#!/bin/bash
array=(one two three)
echo "Printing first object in array." #Replace 0 with the place number of the array item
echo ${array[0]}
echo ""
echo "Whole array"
echo ${array[*]}
echo ""
echo "Array indexes"
echo ${!array[*]}
Output
Printing first object in array.
one
Whole array
one two three
Array indexes
0 1 2
The following script let you turn your screen brightness up/down, but also adjust the color for night time.
Copy and paste code below in a nightlight.sh file
chmod +x nightlight.sh
and run
./nightlight.sh on .5
Code:
#!/bin/sh
export DISPLAY=$(w $(id -un) | awk 'NF > 7 && $2 ~ /tty[0-9]+/ {print $3; exit}')
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
display=`xrandr | grep "\ connected" | cut -d" " -f1`
brightness="$2"
# Check if brightness was specified. If not, set screen to 50% brightness
if (echo $2 | grep [0-9]);then
brightness="$2"
elif (echo $1 | grep -q help);then
echo "############"
else
brightness=".5"
echo "Brightness variable not set, setting to fallback of 50%"
fi
night_mode() {
for disp in ${display}; do
xrandr --output $disp --gamma $1 --brightness ${brightness}
done }
# auto is for future development
# auto() {
# The idea behind auto is to setup something that can pull the actual sunrise/sunset times then automatically adjust the display.
# Ideally there would be an algorithm so it does it slowly over a period of time, say slightly change the color over 30 minutes.
# until the desired color limit is reached
#
# curl sunrise-sunset.com/timezone/time
# if (time > sunset && colorTemp != colorTempMin); then
# set color to current temp-1
# elif (time > sunrise && colorTemp != colorTempMax); then);
# set to full brightness and temp
# else
# unable to parse, skipping.
# fi
#}
help() {
echo " Help for nightmode script.
How to run script
./nightmode.sh on/off brightness
Examples:
Turn nightmode on and set screen brightness to 75%
./nightmode.sh on .75
Turn night mode off and set screen brightness to 100%
./nightmode.sh off 1
"
}
case $1 in
off) night_mode 1:1:1 1.0 ;;
help) help ;;
auto) auto ;;
*) night_mode 1:1:0.5 ;;
esac
Setup in crontab to automatically trigger when it gets night or morning
* 21 * * * ~/nightlight.sh on .5 # Turn on at night
* 7 * * * ~/nightlight.sh off 1 # Turn off in the morning