Merge branch 'master' of github.com:stv0g/snippets

This commit is contained in:
Steffen Vogel 2014-07-13 12:40:35 +02:00
commit e079b8135c
36 changed files with 614 additions and 86 deletions

2
.gitignore vendored
View file

@ -3,3 +3,5 @@
.project
.classpath
gc_spider/cookie.txt
php/campus/cocal.db

9
README.md Normal file
View file

@ -0,0 +1,9 @@
snippets
========
Meine Werkzeugssammlung
I'm using this repository to gather various scripts and utities I'm using all day.
It's growing continously and hopefully other can profit for it.
Everything ins this repo is licenced under GPLv3.

93
bash/backup-btrfs.sh Normal file
View file

@ -0,0 +1,93 @@
#!/bin/bash
##
# Incremental backups with Btrfs snapshots
#
# This script does incremental backups of Btrfs subvolumes
# across filesystem boundaries as proposed in the Btrfs wiki:
# https://btrfs.wiki.kernel.org/index.php/Incremental_Backup
#
# It uses the 'btrfs send' and 'btrfs receive' commands.
# Its not intended for simple snapshots in a single filesystem enviroment.
#
# @copyright 2013 Steffen Vogel
# @license http://www.gnu.org/licenses/gpl.txt GNU Public License
# @author Steffen Vogel <info@steffenvogel.de>
# @link http://www.steffenvogel.de
##
##
# This script is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script. If not, see <http://www.gnu.org/licenses/>.
##
# TODO: delete old snapshots in destination fs
# TODO: print statistics of send | receive pipe (speed & size)
function usage {
echo "Usage: $(basename $0) SOURCE [DEST]"
echo
echo " SOURCE a path to the subvolume to backup"
echo " DEST an optional path to the backup destination"
echo " only required for initialization"
exit 1
}
set -e
if [ $# -lt 1 ]; then
echo -e "missing source"
echo
usage
fi
SRC=$(readlink -f "$1")
if [ -h "$SRC/.backup/destination" ]; then
DEST=$(readlink -f "$SRC/.backup/destination")
elif [ $# -ne 2 ] ; then
echo -e "missing destination"
echo
usage
else
DEST=$(readlink -f $2)
mkdir -p "$SRC/.backup/"
mkdir -p "$DEST"
ln -sf "$DEST" "$SRC/.backup/destination"
ln -sf "$SRC" "$DEST/source"
fi
# name for the new snapshot
SNAPSHOT=$(date +%F_%H-%M-%S)
LATEST="$SRC/.backup/$SNAPSHOT"
# snapshot the current state
btrfs subvolume snapshot -r "$SRC" "$LATEST"
# send changes
if [ -h "$DEST/latest-source" ]; then
PREVIOUS=$(readlink -f "$DEST/latest-source")
btrfs send -p "$PREVIOUS" "$LATEST" | btrfs receive "$DEST"
else
btrfs send "$LATEST" | btrfs receive "$DEST"
fi
# delete old snapshot in source fs
if [ -n "$PREVIOUS" ]; then
btrfs subvolume delete "$PREVIOUS"
fi
# update links to last backup
ln -rsfT "$DEST/$SNAPSHOT" "$DEST/latest"
ln -sfT "$LATEST" "$DEST/latest-source"

112
bash/backup-mysql.sh Normal file
View file

@ -0,0 +1,112 @@
#!/bin/bash
##
# Backup mySQL databases in separate sql dumps
#
# @copyright 2013 Steffen Vogel
# @license http://www.gnu.org/licenses/gpl.txt GNU Public License
# @author Steffen Vogel <info@steffenvogel.de>
# @link http://www.steffenvogel.de
##
##
# This script is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script. If not, see <http://www.gnu.org/licenses/>.
##
set -e
function usage {
cat <<-EOF
Usage: $(basename $0) [-u USER] [-p PASSWORD] DIR
DIR is the directory for the backups (defaults to cwd)
Options:
-u mysql username
-p mysql password
-h show this help
-d enable verbose output
written by Steffen Vogel <post@steffenvogel.de>
EOF
}
function deps() {
FAILED=0
for DEP in $*; do
if ! which ${DEP} &>/dev/null; then
echo -e "This script requires ${DEP} to run but it is not installed."
((FAILED++))
fi
done
return ${FAILED}
}
if ! deps mysql mysqldump; then
echo -e "mysql tools missing!"
echo
usage
exit 1
fi
# parse arguments
while getopts "u:p:hd" OPT; do
case ${OPT} in
p) MYSQL_PASS=${OPTARG} ;;
u) MYSQL_USER=${OPTARG} ;;
d) V=1 ;;
h)
usage
exit 0 ;;
*)
usage
exit 1
esac
done
# clear all options and reset the command line
shift $((OPTIND-1))
# parsing backup directory
if [ -n "$1" ]; then
DIR=$(readlink -f $1)
else
DIR=$(pwd)
fi
# mySQL options
OPTS=""
if [ -n "$MYSQL_USER" ]; then
OPTS+=" -u$MYSQL_USER"
fi
if [ -n "$MYSQL_PASS" ]; then
OPTS+=" -p$MYSQL_PASS"
fi
# get all databases
DATABASES=`mysql $OPTS -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`
DATE=$(date +%F_%H-%M-%S)
mkdir -p $DIR/$DATE
ln -rsfT $DIR/$DATE/ $DIR/latest
[ -z "$V" ] || echo "Starting mySQL backup: $(date)"
[ -z "$V" ] || echo "$(echo '$DATABASES' | wc -l) databases"
[ -z "$V" ] || echo "Backup directory: $DIR/$DATE"
for db in $DATABASES; do
[ -z "$V" ] || echo -n "Dumping $db ..."
mysqldump $OPTS --force --opt --events --databases $db | gzip > "$DIR/$DATE/$db.sql.gz"
[ -z "$V" ] || echo -e "\b\b\binto $DIR/$DATE/$db.sql.gz ($(du -h $DIR/$DATE/$db.sql.gz | cut -f1))"
done
[ -z "$V" ] || echo "Finished mySQL backup: $(date) ($(du -hs $DIR/$DATE/ | cut -f1))"

91
bash/backup-remote.sh Normal file
View file

@ -0,0 +1,91 @@
#!/bin/bash
##
# Sync with remote server and create Btrfs snapshots
#
# This scripts uses rsync to sync remote directories with a local copy
# After every successful sync a readonly Btrfs snapshot of this copy is
# created
#
# This script requires root privileges for creating Btrfs snapshots.
# Consider using public key authentification with SSH to allow root
# logins on remote machines:
#
# On remote side:
# echo "PermitRootLogin without-password" >> /etc/ssh/sshd_config:
#
# On local side:
# sudo ssh-keygen
# sudo cat /root/.ssh/id_dsa.pub | ssh user@remote 'cat >> /root/.ssh/authorized_keys'
#
# @copyright 2013 Steffen Vogel
# @license http://www.gnu.org/licenses/gpl.txt GNU Public License
# @author Steffen Vogel <info@steffenvogel.de>
# @link http://www.steffenvogel.de
##
##
# This script is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script. If not, see <http://www.gnu.org/licenses/>.
##
function usage {
echo "Usage: $(basename $0) SOURCE DEST"
echo
echo " SOURCE a path to the subvolume to backup"
echo " DEST a path to the backup destination"
exit 1
}
set -e
if [ $# -ne 2 ]; then
echo -e "invalid args!"
echo
usage
fi
DATE=$(date +%F_%H-%M-%S)
SRC=$1
DEST=$(readlink -f $2)
if ! btrfs sub show $DEST/.current &> /dev/null; then
if [ -d $DEST/.current ]; then
echo -e "destination directory exists and is not a valid btrfs subvolume!"
echo
usage
else
btrfs sub create $DEST/.current
fi
fi
# rsync options
OPTS="--archive --acls --xattrs"
OPTS+=" --progress --human-readable"
OPTS+=" --delete --delete-excluded"
OPTS+=" --exclude /dev/"
OPTS+=" --exclude /proc/"
OPTS+=" --exclude /sys/"
OPTS+=" --exclude /tmp/"
OPTS+=" --exclude /run/"
OPTS+=" --exclude /mnt/"
OPTS+=" --exclude /media/"
OPTS+=" --exclude /lost+found/"
# sync with remote
rsync $OPTS $SRC $DEST/.current/
# create new readonly snapshot
btrfs subvolume snapshot -r $DEST/.current $DEST/$DATE
# create symlink to latest snapshot
ln -rsfT $DATE latest

69
bash/mount.luks.sh Executable file
View file

@ -0,0 +1,69 @@
#!/bin/bash
##
# [u]mount(8) helper for luks encrypted disks
#
# Both mount and umount offer the ability to handover the mounting
# process to a helper script. This is usefull when mounting/unmounting
# luks encrypted disks. This helper combines the following steps for mounting
# a disk:
#
# 1. cryptsetup luksOpen DEV UUID
# 2. mount -o helper=luks /dev/mapper/UUID DIR
#
# respectivly for unmounting
#
# 1. umount -i DEV
# 2. cryptsetup luksClose UUID
#
# INSTALL:
# place this script in /sbin/mount.luks and make it executable.
#
# USAGE:
# mount -t luks /dev/sda1 /home
#
# or via /etc/fstab:
# /dev/sda1 /home luks defaults 0 0
# followed by:
# mount /home
#
# @copyright 2013 Steffen Vogel
# @license http://www.gnu.org/licenses/gpl.txt GNU Public License
# @author Steffen Vogel <post@steffenvogel.de>
# @link http://www.steffenvogel.de
##
if [ "$(basename $0)" == "mount.luks" ]; then
DEV=$1
DIR=$2
shift 2
OPTS=$@
UUID=$(cryptsetup luksUUID $DEV)
if [ $? -ne 0 ]; then
echo -e "$DEV is not a LUKS device"
exit 1
fi
cryptsetup luksOpen $DEV $UUID
mount $OPTS -o helper=luks /dev/mapper/$UUID $DIR
# NOTE: The mount option '-o helper=luks' is essentially required
# because the encrypted filesystem is not of type "luks".
# This option tells umount to use this helper script,
# instead of using the normal unmounting procedure and
# leaving the dm-crypt volume unclosed and therefore unproteced!
elif [ "$(basename $0)" == "umount.luks" ]; then
DEV=$(mount | grep $1 | cut -f 1 -d " ")
UUID=$(basename $DEV)
shift
OPTS=$@
umount -i $OPTS $DEV
# NOTE: The umount option '-i' is essentially required. It skips this
# helper script which would cause otherwise an endless self recursion
cryptsetup luksClose $UUID
fi

View file

@ -1,2 +0,0 @@
#!/bin/bash
gpg -d /media/STEFFEN-KEY/.secret/luks.key.enc | sudo pmount -p - $1 $2

View file

@ -82,8 +82,7 @@ case ${FORMAT} in
FORMAT="davs\://${L2P_USER}@www2.elearning.rwth-aachen.de\1/materials/documents L²P\:\2 \4"
;;
*)
echo "invalid format!" >&2
echo
echo -e "invalid format!"
usage
exit 1
esac

View file

@ -0,0 +1,36 @@
#!/bin/bash
##
# dhclient wrapper to update your dns
#
# @copyright 2013 Steffen Vogel
# @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
# @author Steffen Vogel <post@steffenvogel.de>
# @link http://www.steffenvogel.de
#
# Add this file to /etc/dhcp/dhclient-exit-hooks.d/nsupdate
# to update your dns autmatically when you get a new DHCP/IP lease from your ISP
##
##
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing
# permissions and limitations under the License.
##
NS=/usr/local/bin/nsupdate.sh
key=/etc/bind/dhcp.key
zone=0l.de
host=wg.0l.de
server=127.0.0.1
case $reason in
BOUND|RENEW|REBIND|TIMEOUT)
$NS update -d $new_ip_address -k $key -z $zone -n $server -i $interface $host ;;
RELEASE)
$NS delete -d $old_ip_address -k $key -z $zone -n $server $host ;;
esac

159
bash/nsupdate.sh Normal file
View file

@ -0,0 +1,159 @@
#!/bin/bash
##
# Bind9 nsupdate wrapper
#
# @copyright 2013 Andrew Leonard
# @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
# @author Andrew Leonard <sysadmin@andyleonard.com>
# @author Steffen Vogel <post@steffenvogel.de>
# @link http://www.steffenvogel.de
##
##
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing
# permissions and limitations under the License.
##
function usage {
echo "Usage: $0 CMD [FLAGS] HOST"
echo
echo " HOST is the hostname you want to update"
echo
echo " CMD is one of:"
echo " add, delete, update"
echo
echo " FLAGS are:"
echo " -n nameserver - DNS server to send updates to"
echo " -k file - Path to private key file"
echo " -y [hmac:]keyname:secret - key supplied via cli"
echo " -z zone - Zone to update"
echo " -t type - Record type; default is determined by -i,-4,-6 option"
echo " -d data - Record data / IP address"
echo " -i interface - Use the address of this interface as record data"
echo " -T ttl - Time to live for updated record; default: 1h."
echo " -4 / -6 use IP version"
exit 1
}
# parsing cmd
if [ "$1" == "add" -o "$1" == "delete" -o "$1" == "update" ]; then
CMD=$1
else
echo -e "missing/invalid command"
echo
usage
fi
shift 1
# default options
NS=localhost
TTL=3600
OPTS=
VER=4
# parse arguments
while getopts "n:k:y:T:i:t:z:46" OPT ; do
case $OPT in
n) NS=$OPTARG ;;
k) KEYFILE=$OPTARG ;;
y) KEY=$OPTARG ;;
d) RDATA=$OPTARG ;;
t) TYPE=$OPTARG ;;
T) TTL=$OPTARG ;;
z) ZONE=$OPTARG ;;
i) IF=$OPTARG ;;
4) VER=4 ;;
6) VER=6 ;;
*) usage ;;
esac
done
# clear all options and reset the command line
shift $((OPTIND-1))
# parsing host
if [ -n "$1" ]; then
HOST=$1
else
echo -e "missing host"
echo
usage
fi
if [ -n "$KEYFILE" ] ; then
OPTS="-k $KEYFILE"
elif [ -n "$KEY" ] ; then
OPTS="-y $KEY"
fi
if [ -z "$ZONE" ] ; then
echo -e "missing zone"
echo
usage
fi
if [ -z "$TYPE" ] ; then
case $VER in
4) TYPE=A ;;
6) TYPE=AAAA ;;
*)
echo "type missing"
usage
esac
fi
# get current IPv4/6 address from net or interface
if [ -z "$RDATA" ] ; then
if [ -z "$IF" ] ; then
RDATA=$(curl -s 'http://checkip.dyndns.org' | sed 's/.*Current IP Address: \([0-9\.]\{7,15\}\).*/\1/')
else
RDATA=$(ip -o -$VER address show dev $IF | sed -nr 's/.*inet6? ([^/ ]+).*/\1/p')
fi
fi
OPTS="$OPTS -v"
# update zone
case $CMD in
add)
nsupdate $OPTS <<EOF
server $NS
zone $ZONE
update add $HOST $TTL $TYPE $RDATA
show
send
EOF
exit ;;
delete)
nsupdate $OPTS <<EOF
server $NS
zone $ZONE
update delete $HOST $TYPE
show
send
EOF
exit ;;
update)
nsupdate $OPTS <<EOF
server $NS
zone $ZONE
update delete $HOST $TYPE
update add $HOST $TTL $TYPE $RDATA
show
send
EOF
exit ;;
*)
echo -e "invalid command"
echo
usage ;;
esac

View file

@ -114,34 +114,17 @@ fi
# parse arguments
while getopts "z:p:u:t:i:Dhd46" OPT; do
case ${OPT} in
p)
PASS=${OPTARG}
;;
u)
USER=${OPTARG}
;;
t)
TTL=${OPTARG}
;;
4)
VER=4
;;
6)
VER=6
;;
i)
IF=${OPTARG}
;;
D)
DAEMON=1
;;
d)
DEBUG=${OPTARG:-5}
;;
p) PASS=${OPTARG} ;;
u) USER=${OPTARG} ;;
t) TTL=${OPTARG} ;;
4) VER=4 ;;
6) VER=6 ;;
i) IF=${OPTARG} ;;
D) DAEMON=1 ;;
d) DEBUG=${OPTARG:-5} ;;
h)
usage
exit 0
;;
exit 0 ;;
*)
usage
exit 1

11
bash/uptime.sh Normal file
View file

@ -0,0 +1,11 @@
#!/bin/bash
### BEGIN INIT INFO
# Provides: uptime
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Default-Stop: 0 1 6
# Short-Description: Log uptime of server before shutdown
### END INIT INFO
echo $(date +%s) $(cat /proc/uptime) >> /var/log/uptime.log

View file

@ -1,5 +0,0 @@
# Netscape HTTP Cookie File
# http://curlm.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.
#HttpOnly_www.geocaching.com FALSE / FALSE 0 ASP.NET_SessionId kpnkhincysmgh2z2wwxayp55

View file

Before

Width:  |  Height:  |  Size: 781 B

After

Width:  |  Height:  |  Size: 781 B

View file

Before

Width:  |  Height:  |  Size: 715 B

After

Width:  |  Height:  |  Size: 715 B

View file

Before

Width:  |  Height:  |  Size: 133 B

After

Width:  |  Height:  |  Size: 133 B

View file

Before

Width:  |  Height:  |  Size: 173 B

After

Width:  |  Height:  |  Size: 173 B

View file

Before

Width:  |  Height:  |  Size: 117 B

After

Width:  |  Height:  |  Size: 117 B

View file

Before

Width:  |  Height:  |  Size: 127 B

After

Width:  |  Height:  |  Size: 127 B

View file

Before

Width:  |  Height:  |  Size: 120 B

After

Width:  |  Height:  |  Size: 120 B

View file

Before

Width:  |  Height:  |  Size: 166 B

After

Width:  |  Height:  |  Size: 166 B

View file

Before

Width:  |  Height:  |  Size: 98 B

After

Width:  |  Height:  |  Size: 98 B

View file

Before

Width:  |  Height:  |  Size: 156 B

After

Width:  |  Height:  |  Size: 156 B

View file

Before

Width:  |  Height:  |  Size: 126 B

After

Width:  |  Height:  |  Size: 126 B

View file

Before

Width:  |  Height:  |  Size: 236 B

After

Width:  |  Height:  |  Size: 236 B

Binary file not shown.

View file

@ -104,6 +104,7 @@ function crawl_address($room) {
return (count($matches)) ? $matches : false;
}
/* send HTTP 500 for Google to stop fetching */
function error() {
global $scriptUrl;
@ -194,16 +195,24 @@ if (isset($matrnr) && isset($passwd)) {
}
$address = array();
$category = '';
$lines = explode("\r\n", $body);
foreach ($lines as $line) {
if ($line) {
list($key, $value) = explode(":", $line);
switch ($key) {
case 'END':
if ($value == 'VEVENT') flush();
$address = array();
if ($value == 'VEVENT') {
flush();
$address = array();
$category = '';
}
break;
case 'CATEGORIES':
$category = $value;
case 'LOCATION':
$matches = array();
if (preg_match('/^([0-9]+\|[0-9]+)/', $value, $matches)) {
@ -225,20 +234,20 @@ if (isset($matrnr) && isset($passwd)) {
$additional = $value;
$value = '';
if (@$address['building_no'] && @$address['room_no'])
$value .= '\n' . $address['building_no'] . '|' . $address['room_no'];
if (@$address['building'] || @$address['building_no'])
$value .= '\nGebäude: ' . $address['building_no'] . ' ' . $address['building'];
if (@$address['room'])
$value .= ' ' . $address['room'];
if (@$address['building'])
$value .= '\n' . 'Gebäude: ' . $address['building'];
if (@$address['room'] || @$address['room_no'])
$value .= '\nRaum: ' . $address['room_no'] . ' ' . $address['room'];
if (@$address['floor'])
$value .= '\n' . 'Geschoss: ' . $address['floor'];
$value .= '\nGeschoss: ' . $address['floor'];
if (@$address['cluster'])
$value .= '\n' . 'Campus: ' . preg_replace('/^Campus /', '', $address['cluster']);
$value .= '\nCampus: ' . preg_replace('/^Campus /', '', $address['cluster']);
if (@$category)
$value .= '\nTyp: ' . $category;
if ($additional && $additional != 'Kommentar')
$value .= '\n' . $additional;

View file

@ -1,38 +0,0 @@
<?php
require_once 'config.php';
require_once 'xmlrpc/xmlrpc.inc';
require_once 'sipgateAPI.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$headers = getallheaders();
$body = file_get_contents('php://input');
$json = json_decode($body, true);
switch ($headers['x-amz-sns-message-type']) {
case 'SubscriptionConfirmation':
fopen($json['SubscribeURL'], 'r');
break;
case 'Notification':
$sipgate = new sipgateAPI($config['username'], $config['password']);
$balance = $sipgate->getBalance();
$message = preg_replace('/\r?\n/m', '\n', trim($json['Message']));
if ($balance < $config['reserve']) {
header("HTTP/1.0 402 Payment Required");
}
else {
$sipgate->sendSMS($config['recipient'], $message, NULL, $config['recipient']);
}
break;
default:
header("HTTP/1.1 501 Not Implemented");
}
}
else {
header("HTTP/1.1 405 Method Not Allowed");
}
?>

View file

@ -820,7 +820,7 @@
var $key='';
var $keypass='';
var $verifypeer=true;
var $verifyhost=1;
var $verifyhost=2;
var $no_multicall=false;
var $proxy='';
var $proxyport=0;
@ -3773,4 +3773,4 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
}
}
?>
?>