Benutzer:Lyl: Unterschied zwischen den Versionen
Lyl (Diskussion | Beiträge) xmpp |
Dirk (Diskussion | Beiträge) K →Inbetriebnahme: -Sy in -S gewandelt gemäß Diskussion hier: https://bbs.archlinux.de/viewtopic.php?id=17347 |
||
(21 dazwischenliegende Versionen von einem anderen Benutzer werden nicht angezeigt) | |||
Zeile 1: | Zeile 1: | ||
Jabber: lyl@jabber.ccc.de | |||
== Scripting == | |||
=== Übersicht === | |||
*[[Benutzer:Lyl#Backup|Backup]]: Script zum automatisierten Einhängen einer [[Festplatte_verschlüsseln#Crypto-Devices_anlegen|Verschlüsselten Festplatte]] und starten eines [[Backups]] mit hilfe von [[rsnapshot]]. | |||
*[[Benutzer:Lyl#rename-image-r|rename-image-r]]: Benennen von Bildern mit Ordnername und Aufnahmendatum im aktuellen Verzeichniss und allen Unterordnern (sofern Vorhanden). z.B: dsc_0001.jpg --> Gebrtstag-Max_2009-01-09_19-10-25-jpg | |||
*[[Benutzer:Lyl#macchanger|macchanger]]: Daemon zum zuweisen einer zufälligen MAC-Addresse. | |||
=== Backup === | |||
Script zum automatisierten Einhängen einer [[Festplatte_verschlüsseln#Crypto-Devices_anlegen|Verschlüsselten Festplatte]] und starten eines [[Backups]] mit hilfe von [[rsnapshot]]. | |||
==== Inbetriebnahme ==== | |||
# Beispiel mit folgenden Variablen: | |||
STATUSFILE=/root/script/backup.day | |||
KEYFILE=backup/backup.key | |||
BACKUPCLOSE=/root/script/backup-close | |||
pacman -S rsnapshot | |||
mkdir /extbackup/ | |||
mkdir /mnt/keyfs/ | |||
mount /mnt/usbstick-auf-dem-das-passwort-gespeichert-wir /mnt/keyfs/ | |||
echo "passwort-für-externe-hdd" /mnt/keyfs/$KEYFILE | |||
echo 0 > $STATUSFILE | |||
# Konfigurieren von rsnapshot: http://www.rsnapshot.org/howto/ | |||
# anlegen des Backup-Scripts Inhalt | |||
# anpassen der Konfiguration am Anfang des Backup-Scripts | |||
# anlegen von $BACKUPCLOSE | |||
==== Script ==== | |||
Inhalt des Backup-Scripts (./backup): | |||
#!/bin/sh | |||
######################################################################## | |||
## ## | |||
## Script to automate mounting crypted, external hdd and start backup ## | |||
## using rsnapshot. ## | |||
## ## | |||
## by lyl 25.10.2008 - 10.02.2009 GPL v3 ## | |||
## ## | |||
######################################################################## | |||
# | |||
# rsnapshot is configured in /etc/rsnapshot.conf | |||
# parameters given to this script will be forwarded to rsnapshot | |||
# see `man rsnapshot` for mor information | |||
# | |||
# use cron or anacron to start backup automaticaly | |||
# | |||
# depends=('rsnapshot') | |||
# optdepends=('anacron') | |||
# | |||
# filesystems | |||
BACKUPFS=/dev/disk/by-id/usb-WDC_WD2500AB-1234567890-0\:0-part1 | |||
KEYFS=/dev/disk/by-id/usb-USB_DISK_USB_DISK_123456790-0\:0-part1 | |||
# files | |||
STATUSFILE=/root/script/backup.day | |||
KEYFILE=backup/backup.key | |||
BACKUPCLOSE=/root/script/backup-close | |||
# Number of hourly backups until more deep backups are made | |||
DAYS=2 | |||
WEEKS=6 | |||
MONTHS=18 | |||
YEARS=90 | |||
# END OF CONFIG | |||
# | |||
# Mounting filesystems | |||
# | |||
echo "mounting filesystem containing the key-file..." | |||
mount $KEYFS /mnt/keyfs/ | |||
echo "opening and mounting backup filesystem..." | |||
cryptsetup luksOpen $BACKUPFS extbackup < /mnt/keyfs/$KEYFILE | |||
mount /dev/mapper/extbackup /extbackup/ | |||
if (mount | grep -c "/dev/mapper/extbackup on /extbackup" > /dev/null) | |||
then | |||
echo "done" | |||
else | |||
echo "fail" | |||
/root/script/backup-close | |||
exit 1 | |||
fi | |||
# | |||
# Start backing up | |||
# | |||
BACKUPNR=`cat $STATUSFILE` | |||
echo "starting hourly backups (Nr. $BACKUPNR)..." | |||
rsnapshot hourly $1 | |||
# calculation deepth | |||
if (test $[$BACKUPNR % $DAYS] -eq 0); then | |||
BACKUPDAY=1 | |||
else | |||
BACKUPDAY=0 | |||
fi | |||
if (test $[$BACKUPNR % $WEEKS] -eq 0); then | |||
BACKUPDAY=2 | |||
fi | |||
if (test $[$BACKUPNR % $MONTHS] -eq 0); then | |||
BACKUPDAY=3 | |||
fi | |||
if (test $[$BACKUPNR % $YEARS] -eq 0); then | |||
BACKUPDAY=4 | |||
fi | |||
if (test $BACKUPDAY -ge 1) | |||
then | |||
echo "done" | |||
echo "creating daily backup..." | |||
rsnapshot daily $1 | |||
if (test $BACKUPDAY -ge 2) | |||
then | |||
echo "done" | |||
echo "creating weekly backup..." | |||
rsnapshot weekly $1 | |||
if (test $BACKUPDAY -ge 3) | |||
then | |||
echo "done" | |||
echo "creating monthly backups..." | |||
rsnapshot monthly $1 | |||
if (test $BACKUPDAY -ge 4) | |||
then | |||
echo "done" | |||
echo "creating yearly backups..." | |||
rsnapshot yearly $1 | |||
fi | |||
fi | |||
fi | |||
fi | |||
echo `expr $BACKUPNR + 1` > $STATUSFILE | |||
echo "done" | |||
$BACKUPCLOSE | |||
exit $? | |||
Die Nummer das aktuellen Backups wird aus der Datei /root/script/backup.day ($STATUSFILE) gelesen: | |||
1 | |||
Inhalt von /mnt/keyfs/backup/backup.key ($KEYFILE): | |||
passwort-für-externe-hdd | |||
Inhalt von /root/script/backup-close ($BACKUPCLOSE): | |||
#!/bin/sh | |||
ERRORCODE=0 | |||
echo "umounting key filesystem..." | |||
umount /mnt/keyfs | |||
echo "umounting backup filesystem..." | |||
umount /dev/mapper/extbackup | |||
echo "closing crypto device..." | |||
cryptsetup luksClose extbackup | |||
if (ls /dev/mapper | grep -c "extbackup" > /dev/null) | |||
then | |||
echo "fail" | |||
echo "if you have opened the backup-file-system, please close it and try again!" | |||
ERRORCODE=1 | |||
else | |||
if (ls /mnt/keyfs/ |grep -c "" > /dev/null) | |||
then | |||
echo "fail" | |||
echo "if you have opened the key-file-system, please close it and try again!" | |||
ERRORCODE=2 | |||
fi | |||
echo "done" | |||
fi | |||
exit $ERRORCODE | |||
=== rename-image-r === | |||
Benennen von Bildern mit Ordnername und Aufnahmendatum im aktuellen Verzeichniss und allen Unterordnern (sofern Vorhanden). | |||
z.B: dsc_0001.jpg --> Gebrtstag-Max_2009-01-09_19-10-25-jpg | |||
==== Inbetriebnahme ==== | |||
# Erstellen einer ausführbaren Datei mit folgendem Inhalt | |||
==== Script ==== | |||
#!/bin/sh | |||
# | |||
# recrusive rename of *.jpg to JJJJ-MM-DD_HH-MM-SS.jpg | |||
# | |||
# by lyl 18.01.2009 | |||
# | |||
# based on: | |||
# $Id: iRename,v 1.1 2001/11/20 03:11:28 vijoy Exp vijoy $ | |||
# http://www.varp.net/photos/download.html | |||
# | |||
# | |||
#CONFIG: | |||
# | |||
#text to write in front or after date | |||
# "dir" - the directory-name the image is in | |||
# "original" - the original (current) name of the image (be careful when re-renameing) | |||
front="dir" | |||
end="original" | |||
#seperators | |||
# seperator: between JJJJ,MM,DD and HH,MM,SS | |||
# seperatorddhh between DD,HH and $front,timestamp,$end if $front/$end contain "dir" or "original" | |||
seperator="-" | |||
seperatorddhh="_" | |||
#overwrite config via parameters | |||
#not implemented jet... | |||
# Cleanup and create input | |||
echo "renaming files in `pwd`..." | |||
ls --escape *.jpg *.JPG | while read imgname | |||
do | |||
if [ "$imgname" != "" ];then | |||
if [ ! -f "$imgname" ]; then | |||
echo "$imgname not found." | |||
break | |||
fi | |||
# Extracting image information | |||
info=`head "$imgname"| strings| \ | |||
grep -e "19..:..:.. ..:..:.." -e "20..:..:.. ..:..:.." -e "21..:..:.. ..:..:.."` | |||
orgname=`echo $imgname| awk '{ print substr( $0, 0 , length($0) - 4 ) }'` | |||
# Set name | |||
newname=`echo $info|cut -d ' ' -f 1`"$seperatorddhh"`echo $info|cut -d ' ' -f 2` | |||
newname=`echo $newname|sed s/:/"$seperator"/g` | |||
if [ "$front" == "dir" ] | |||
then | |||
newname=`echo ${PWD##*/}`"$seperatorddhh""$newname" | |||
else | |||
if [ "$front" == "original" ] | |||
then | |||
newname=$orgname"$seperatorddhh"$newname | |||
else | |||
newname=$front$newname | |||
fi | |||
fi | |||
if [ "$end" == "dir" ] | |||
then | |||
newname="$newname""$seperatorddhh"`echo ${PWD##*/}`".jpg" | |||
else | |||
if [ "$end" == "original" ] | |||
then | |||
newname=$newname"$seperatorddhh"$orgname".jpg" | |||
else | |||
newname=$newname$end".jpg" | |||
fi | |||
fi | |||
# Rename only if valid newname | |||
if [ "X$newname" != "X++" ] && [ "$imgname" != "" ] && [ "$imgname" != "$newname" ] | |||
then | |||
mv -i "$imgname" "$newname" | |||
fi | |||
fi | |||
done | |||
# Do the smae for each subdirectory | |||
ls -l --escape | grep '^d'|cut -d ':' -f 2- | cut -d ' ' -f 2-| while read imgsubdir | |||
do | |||
if [ "$imgsubdir" != "" ];then | |||
cd "$imgsubdir" | |||
if [ "`echo $0 |cut -b 1`" == "/" ] | |||
then | |||
echo $0 | |||
$0 | |||
else | |||
../$0 | |||
fi | |||
cd .. | |||
fi | |||
done | |||
=== macchanger === | |||
Daemon zum zuweisen einer zufälligen MAC-Addresse. | |||
==== Inbetriebnahme ==== | |||
# Erstellen der Datei /etc/rc.d/macchanger mit unten stehendem Inhalt | |||
# Anpassen der Netzwerk-Adapter, falls andere als eth0 und wlan0 geändert werden sollen | |||
# Eintragen in /etc/rc.configs vor network: DAEMONS(... macchanger network ...) | |||
==== Script ==== | |||
#!/bin/bash | |||
. /etc/rc.d/functions | |||
case "$1" in | |||
start) | |||
stat_busy "Starting Network" | |||
error=0 | |||
#change mac of eth0 | |||
ifconfig eth0 down | |||
macchanger -A eth0 > /dev/null | |||
ifconfig eth0 up | |||
#change mac of wlan | |||
ifconfig wlan0 down | |||
macchanger -A wmaster0 > /dev/null | |||
macchanger -A wlan0 > /dev/null | |||
ifconfig wlan0 up | |||
if [ $error -eq 0 ]; then | |||
stat_done | |||
else | |||
stat_fail | |||
fi | |||
;; | |||
stop) | |||
stat_busy "I cant remember my old MAC-Addresses" | |||
stat_done | |||
;; | |||
restart) | |||
$0 start | |||
;; | |||
*) | |||
echo "usage: $0 {start|stop|restart}" | |||
esac |
Aktuelle Version vom 5. September 2010, 21:41 Uhr
Jabber: lyl@jabber.ccc.de
Scripting
Übersicht
- Backup: Script zum automatisierten Einhängen einer Verschlüsselten Festplatte und starten eines Backups mit hilfe von rsnapshot.
- rename-image-r: Benennen von Bildern mit Ordnername und Aufnahmendatum im aktuellen Verzeichniss und allen Unterordnern (sofern Vorhanden). z.B: dsc_0001.jpg --> Gebrtstag-Max_2009-01-09_19-10-25-jpg
- macchanger: Daemon zum zuweisen einer zufälligen MAC-Addresse.
Backup
Script zum automatisierten Einhängen einer Verschlüsselten Festplatte und starten eines Backups mit hilfe von rsnapshot.
Inbetriebnahme
# Beispiel mit folgenden Variablen: STATUSFILE=/root/script/backup.day KEYFILE=backup/backup.key BACKUPCLOSE=/root/script/backup-close
pacman -S rsnapshot mkdir /extbackup/ mkdir /mnt/keyfs/ mount /mnt/usbstick-auf-dem-das-passwort-gespeichert-wir /mnt/keyfs/ echo "passwort-für-externe-hdd" /mnt/keyfs/$KEYFILE echo 0 > $STATUSFILE # Konfigurieren von rsnapshot: http://www.rsnapshot.org/howto/ # anlegen des Backup-Scripts Inhalt # anpassen der Konfiguration am Anfang des Backup-Scripts # anlegen von $BACKUPCLOSE
Script
Inhalt des Backup-Scripts (./backup):
#!/bin/sh ######################################################################## ## ## ## Script to automate mounting crypted, external hdd and start backup ## ## using rsnapshot. ## ## ## ## by lyl 25.10.2008 - 10.02.2009 GPL v3 ## ## ## ######################################################################## # # rsnapshot is configured in /etc/rsnapshot.conf # parameters given to this script will be forwarded to rsnapshot # see `man rsnapshot` for mor information # # use cron or anacron to start backup automaticaly # # depends=('rsnapshot') # optdepends=('anacron') # # filesystems BACKUPFS=/dev/disk/by-id/usb-WDC_WD2500AB-1234567890-0\:0-part1 KEYFS=/dev/disk/by-id/usb-USB_DISK_USB_DISK_123456790-0\:0-part1 # files STATUSFILE=/root/script/backup.day KEYFILE=backup/backup.key BACKUPCLOSE=/root/script/backup-close # Number of hourly backups until more deep backups are made DAYS=2 WEEKS=6 MONTHS=18 YEARS=90 # END OF CONFIG # # Mounting filesystems # echo "mounting filesystem containing the key-file..." mount $KEYFS /mnt/keyfs/ echo "opening and mounting backup filesystem..." cryptsetup luksOpen $BACKUPFS extbackup < /mnt/keyfs/$KEYFILE mount /dev/mapper/extbackup /extbackup/ if (mount | grep -c "/dev/mapper/extbackup on /extbackup" > /dev/null) then echo "done" else echo "fail" /root/script/backup-close exit 1 fi # # Start backing up # BACKUPNR=`cat $STATUSFILE` echo "starting hourly backups (Nr. $BACKUPNR)..." rsnapshot hourly $1 # calculation deepth if (test $[$BACKUPNR % $DAYS] -eq 0); then BACKUPDAY=1 else BACKUPDAY=0 fi if (test $[$BACKUPNR % $WEEKS] -eq 0); then BACKUPDAY=2 fi if (test $[$BACKUPNR % $MONTHS] -eq 0); then BACKUPDAY=3 fi if (test $[$BACKUPNR % $YEARS] -eq 0); then BACKUPDAY=4 fi if (test $BACKUPDAY -ge 1) then echo "done" echo "creating daily backup..." rsnapshot daily $1 if (test $BACKUPDAY -ge 2) then echo "done" echo "creating weekly backup..." rsnapshot weekly $1 if (test $BACKUPDAY -ge 3) then echo "done" echo "creating monthly backups..." rsnapshot monthly $1 if (test $BACKUPDAY -ge 4) then echo "done" echo "creating yearly backups..." rsnapshot yearly $1 fi fi fi fi echo `expr $BACKUPNR + 1` > $STATUSFILE echo "done" $BACKUPCLOSE exit $?
Die Nummer das aktuellen Backups wird aus der Datei /root/script/backup.day ($STATUSFILE) gelesen:
1
Inhalt von /mnt/keyfs/backup/backup.key ($KEYFILE):
passwort-für-externe-hdd
Inhalt von /root/script/backup-close ($BACKUPCLOSE):
#!/bin/sh ERRORCODE=0 echo "umounting key filesystem..." umount /mnt/keyfs echo "umounting backup filesystem..." umount /dev/mapper/extbackup echo "closing crypto device..." cryptsetup luksClose extbackup if (ls /dev/mapper | grep -c "extbackup" > /dev/null) then echo "fail" echo "if you have opened the backup-file-system, please close it and try again!" ERRORCODE=1 else if (ls /mnt/keyfs/ |grep -c "" > /dev/null) then echo "fail" echo "if you have opened the key-file-system, please close it and try again!" ERRORCODE=2 fi echo "done" fi exit $ERRORCODE
rename-image-r
Benennen von Bildern mit Ordnername und Aufnahmendatum im aktuellen Verzeichniss und allen Unterordnern (sofern Vorhanden).
z.B: dsc_0001.jpg --> Gebrtstag-Max_2009-01-09_19-10-25-jpg
Inbetriebnahme
- Erstellen einer ausführbaren Datei mit folgendem Inhalt
Script
#!/bin/sh # # recrusive rename of *.jpg to JJJJ-MM-DD_HH-MM-SS.jpg # # by lyl 18.01.2009 # # based on: # $Id: iRename,v 1.1 2001/11/20 03:11:28 vijoy Exp vijoy $ # http://www.varp.net/photos/download.html # # #CONFIG: # #text to write in front or after date # "dir" - the directory-name the image is in # "original" - the original (current) name of the image (be careful when re-renameing) front="dir" end="original" #seperators # seperator: between JJJJ,MM,DD and HH,MM,SS # seperatorddhh between DD,HH and $front,timestamp,$end if $front/$end contain "dir" or "original" seperator="-" seperatorddhh="_" #overwrite config via parameters #not implemented jet... # Cleanup and create input echo "renaming files in `pwd`..." ls --escape *.jpg *.JPG | while read imgname do if [ "$imgname" != "" ];then if [ ! -f "$imgname" ]; then echo "$imgname not found." break fi # Extracting image information info=`head "$imgname"| strings| \ grep -e "19..:..:.. ..:..:.." -e "20..:..:.. ..:..:.." -e "21..:..:.. ..:..:.."` orgname=`echo $imgname| awk '{ print substr( $0, 0 , length($0) - 4 ) }'` # Set name newname=`echo $info|cut -d ' ' -f 1`"$seperatorddhh"`echo $info|cut -d ' ' -f 2` newname=`echo $newname|sed s/:/"$seperator"/g` if [ "$front" == "dir" ] then newname=`echo ${PWD##*/}`"$seperatorddhh""$newname" else if [ "$front" == "original" ] then newname=$orgname"$seperatorddhh"$newname else newname=$front$newname fi fi if [ "$end" == "dir" ] then newname="$newname""$seperatorddhh"`echo ${PWD##*/}`".jpg" else if [ "$end" == "original" ] then newname=$newname"$seperatorddhh"$orgname".jpg" else newname=$newname$end".jpg" fi fi # Rename only if valid newname if [ "X$newname" != "X++" ] && [ "$imgname" != "" ] && [ "$imgname" != "$newname" ] then mv -i "$imgname" "$newname" fi fi done # Do the smae for each subdirectory ls -l --escape | grep '^d'|cut -d ':' -f 2- | cut -d ' ' -f 2-| while read imgsubdir do if [ "$imgsubdir" != "" ];then cd "$imgsubdir" if [ "`echo $0 |cut -b 1`" == "/" ] then echo $0 $0 else ../$0 fi cd .. fi done
macchanger
Daemon zum zuweisen einer zufälligen MAC-Addresse.
Inbetriebnahme
- Erstellen der Datei /etc/rc.d/macchanger mit unten stehendem Inhalt
- Anpassen der Netzwerk-Adapter, falls andere als eth0 und wlan0 geändert werden sollen
- Eintragen in /etc/rc.configs vor network: DAEMONS(... macchanger network ...)
Script
#!/bin/bash . /etc/rc.d/functions case "$1" in start) stat_busy "Starting Network" error=0 #change mac of eth0 ifconfig eth0 down macchanger -A eth0 > /dev/null ifconfig eth0 up #change mac of wlan ifconfig wlan0 down macchanger -A wmaster0 > /dev/null macchanger -A wlan0 > /dev/null ifconfig wlan0 up if [ $error -eq 0 ]; then stat_done else stat_fail fi ;; stop) stat_busy "I cant remember my old MAC-Addresses" stat_done ;; restart) $0 start ;; *) echo "usage: $0 {start|stop|restart}" esac