Last active
September 10, 2017 22:55
-
-
Save mlagerberg/390a29f101f6ce025d15 to your computer and use it in GitHub Desktop.
[My Pi scripts] #rpi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ___ CREDITS | |
# This script is a modification of | |
# https://github.com/aweijnitz/pi_backup/blob/master/backup.sh | |
# which started from | |
# http://raspberrypi.stackexchange.com/questions/5427/can-a-raspberry-pi-be-used-to-create-a-backup-of-itself | |
# which in turn started from | |
# http://www.raspberrypi.org/phpBB3/viewtopic.php?p=136912 | |
# | |
# Setting up directories | |
SUBDIR=pibackups | |
DIR=/media/hdd/$SUBDIR | |
function log { | |
echo "$1" | |
echo "$(date +%Y/%m/%d_%H:%M:%S) $1" >> $DIR/backup.log | |
} | |
# Check if backup directory exists | |
if [ ! -d "$DIR" ]; | |
then | |
log "Backup directory $DIR doesn't exist, creating it..." | |
mkdir -p $DIR | |
fi | |
log "-----" | |
log "RaspberryPI backup process started." | |
# First check if pv package is installed, if not, install it first | |
if `dpkg -s pv | grep -q Status;` | |
then | |
log "Package 'pv' is installed." | |
else | |
log "Package 'pv' is NOT installed. Installing..." | |
apt-get -y install pv | |
fi | |
# Create a filename with datestamp for our current backup (without .img suffix) | |
OFILE="$DIR/pibackup-$(date +%Y.%m.%d)" | |
# Create final filename, with suffix | |
OFILEFINAL=$OFILE.img | |
# First sync disks | |
sync; sync | |
# Shut down some services before starting backup process | |
#log "Stopping services before backup..." | |
#service motion stop | |
#service node stop | |
#service btsync stop | |
# Begin the backup process, should take about 2.5 hours from 4Gb SD card to HDD | |
log "Backing up SD card to external HDD." | |
echo "This will take some time depending on your SD card size and read performance. Please wait..." | |
SDSIZE=`sudo blockdev --getsize64 /dev/mmcblk0`; | |
sudo pv -tpreb /dev/mmcblk0 -s $SDSIZE | dd of=$OFILE.temp bs=1M conv=sync,noerror iflag=fullblock | |
# Wait for DD to finish and catch result | |
RESULT=$? | |
# Start services again that where shutdown before backup process | |
#log "Starting the stopped services again..." | |
#service motion start | |
#service node start | |
#service btsync start | |
# If command has completed successfully, delete previous backups and exit | |
if [ $RESULT = 0 ]; | |
then | |
log "Successful backup" | |
#log "NOT deleting old backup images (disabled)" | |
# Delete everything but the 3 newest: | |
cd $DIR | |
# +4 because +3 keeps the 3 newest files, but we don't count the backup.log | |
ls -t $DIR | tail -n +4 | xargs -d '\n' rm | |
mv $OFILE.temp $OFILEFINAL | |
log "Backup process completed. Output: $OFILEFINAL" | |
exit 0 | |
# Else remove attempted backup file | |
else | |
log "Backup failed! Previous backup files untouched." | |
log "Please check there is sufficient space on the HDD." | |
rm -f $OFILE | |
log "Intermediate file $OFILE removed." | |
exit 1 | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
from __future__ import print_function, unicode_literals | |
import requests, json, csv, StringIO, os | |
HISTORY_URL = 'https://api.bitcoinaverage.com/history/USD/per_minute_24h_global_average_sliding_window.csv' | |
SPARK = '/home/pi/scripts/spark/spark' | |
SPARK_LENGTH = 32 | |
def spark(currency='EUR'): | |
r = requests.get(HISTORY_URL) | |
f = StringIO.StringIO(r.text) | |
try: | |
reader = csv.reader(f) | |
args = "" | |
rows = list(reader) | |
totalrows = len(rows) | |
chunk = totalrows / SPARK_LENGTH | |
for i, row in enumerate(rows): | |
# skip first row, it's only column headers | |
#if i < totalrows - SPARK_LENGTH: | |
if i == 0 or i % chunk != 0: | |
continue | |
# Get USD value | |
val = row[2] # 0=datetime, 1=USD volume, 2=USD average | |
args = args + " " + val | |
os.system("/home/pi/scripts/spark/spark " + args) | |
usd = float(rows[totalrows - 1][2]) | |
eur = float(rows[totalrows - 1][5]) | |
output = '' | |
if currency == 'USD' or currency == 'both': | |
output = output + "{0:.2f} (USD) ".format(usd) | |
if currency == 'EUR' or currency == 'both': | |
output = output + "{0:.2f} (EUR) ".format(eur) | |
print(output) | |
finally: | |
f.close() | |
def btc_value(currency='EUR'): | |
response = requests.get('http://blockchain.info/ticker') | |
jsonResponse = None | |
try: | |
jsonResponse = response.json() | |
except ValueError: | |
print("Parse error:") | |
print(response) | |
return | |
output = '' | |
if currency == 'EUR' or currency == 'both': | |
row = jsonResponse['EUR'] | |
output = output + "{0:.2f} (EUR) ".format(row['last']) | |
if currency == 'USD' or currency == 'both': | |
row = jsonResponse['USD'] | |
output = output + "{0:.2f} (USD) ".format(row['last']) | |
print(output) | |
return response | |
spark('both') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Collection of methods used in the other scripts | |
function pause(){ | |
read -p "$*" | |
} | |
# Output pretty colors | |
function blue { | |
echo -e "\e[1;34m$1\e[0m" | |
} | |
function green { | |
echo -e "\e[0;32m$1\e[0m" | |
} | |
function yellow { | |
echo -e "\e[1;33m$1\e[0m" | |
} | |
function lred { | |
echo -e "\e[1;31m$1\e[0m" | |
} | |
function red { | |
echo -e "\e[0;31m$1\e[0m" | |
} | |
# Usage: echo "One ${fcyellow}Two${fcreset} Three" | |
fcreset=$(tput sgr0) # reset foreground color | |
fcdark=$(tput bold; tput setaf 0) | |
fcred=$(tput setaf 1) | |
fcgreen=$(tput setaf 2) | |
fcyellow=$(tput setaf 3) | |
fcdblue=$(tput setaf 4) | |
fcblue=$(tput bold; tput setaf 4) | |
fcpink=$(tput setaf 5) | |
fclblue=$(tput setaf 6) | |
bold=$(tput bold) | |
dim=$(tput dim) | |
underline=$(tput smul) | |
emph=$(tput smso) # Stand-out mode (bold) | |
xemph=$(tput rmso) # Exit stand-out mode | |
# Pretend the terminal is like Android LogCat | |
function debug { | |
blue "$1" | |
} | |
function error { | |
red "$1" | |
} | |
function info { | |
green "$1" | |
} | |
function warn { | |
yellow "$1" | |
} | |
### | |
# Usage: | |
# is_running "display name" "processname" | |
function is_running { | |
ps cax | grep $2 > /dev/null | |
if [ $? -eq 0 ]; then | |
# echo -e "$1: \e[1;33mOK\e[0m" | |
echo "$1: ${fcgreen}OK${fcreset}." | |
else | |
# echo -e "$1: \e[0;31mNot running\e[0m;" | |
echo "$1: ${fcred}Not running${fcreset}." | |
fi | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## | |
# Display the status of various system elements. | |
# Requires ./functions.sh to be present. | |
# Failed login attempts are highlighted in red, | |
# if -keeplog is used they're shown again on next run. | |
# If -btc is used then it'll also show the current bitcoin | |
# price (slow). | |
source /home/pi/scripts/functions.sh | |
log_ts_file="/home/pi/scripts/status_log.timestamp" | |
function header_line { | |
echo | |
echo "${fclblue}$1:${fcreset} ${fcdark}$2${fcreset} $3" | |
} | |
function header { | |
echo | |
echo "${fclblue}$1: ${fcdark}$2${fcreset}" | |
} | |
## | |
# Checks the log files for failed login | |
# attempts since the last this script was run. | |
function failed_logins { | |
fails=`cat /var/log/auth.log | grep -cim1 failure` | |
if [ $fails ]; then | |
header 'Failed login attempts' '' | |
tput bold; tput setaf 1 | |
count=0 | |
# get modified date: | |
ts=0 | |
if [[ -f "$log_ts_file" ]]; then | |
ts=$(stat -c %Y "$log_ts_file") | |
fi | |
ts=$((ts - 1000)) | |
#cat /var/log/auth.log | grep failure | head | |
while read line; do | |
# format: Jan 19 06:25:21 <log message> | |
timestamp=$(date +%s -d "${line:0:15}") | |
# if log line more recent that $ts | |
if (($timestamp > $ts)); then | |
error "$line" | |
count=$((count + 1)) | |
fi | |
done < <(cat /var/log/auth.log | grep 'failure') | |
if (($count == 0)); then | |
echo "${fcdark}None.${fcreset}" | |
fi | |
tput sgr0 | |
else | |
header 'Failed login attempts' 'None.' | |
fi | |
} | |
########################################### | |
clear | |
#### Show current sessions | |
header 'Currently logged in' '`w`' | |
w | |
### prints the same as the first line of 'w' | |
#header "Uptime" '`uptime`' | |
#uptime | |
### Memory usage statistics | |
#header "Memory usage" '`free`' | |
#free | |
### List of relevant disk space statistics | |
header 'Disk space' '`df -h`' | |
#Limit to table header, important partitions and external hd: | |
df -h -l --total | grep -E "/dev/sda|rootfs|Filesystem" | |
### Security: check currently open ports (TCP and UDP) | |
header 'Open ports' '`netstat -vautnp`' | |
# using sudo otherwise -p doesn't work | |
sudo netstat -vautnp | |
### Checking for failed login attempts | |
failed_logins | |
# Check if any of the arguments is clearlog: | |
if [[ $@ != **-keeplog** ]]; then | |
warn "Login attempts prior to this point will no longer be shown." | |
touch "$log_ts_file" | |
fi | |
# Running deamons | |
#header 'Running services' '' | |
#is_running "BitTorrent Sync" "btsync" | |
#is_running "Syncthing " "pulse" | |
#is_running "Motion " "motion" | |
#is_running "Node " "node" | |
# Check if clock is on time | |
header_line 'Time and date' '' "`date`" | |
# Motion timelapse status | |
#header 'Last snapshot' '' | |
#datestamp=`ls -l /media/hdd/motion/lastsnap.jpg | cut -c74-83` | |
#timestamp=`ls -l /media/hdd/motion/lastsnap.jpg | cut -c97-104` | |
#header_line 'Last snapshot' '' "$datestamp at $timestamp" | |
# Bitcoin; show EUR or USD value unless argument 'fast' is given | |
if [[ $@ == **-btc** ]]; then | |
value=$(python /home/pi/scripts/btc.py) | |
header 'Bitcoin' '' | |
echo "${fcyellow}$value" | |
fi | |
echo | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment