changeset 0:e85e3470a41e

Initial commit of useful scripts
author IBBoard <dev@ibboard.co.uk>
date Sat, 02 Aug 2014 19:31:02 +0100
parents
children 182cce081e61
files backup-desktop backup-to-disk backup-website-images backup-websites do-backup find-small-album-art make-certificate mount-crypt record-app record-audio rename-photos tidy-fspot-database umount-crypt
diffstat 13 files changed, 334 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backup-desktop	Sat Aug 02 19:31:02 2014 +0100
@@ -0,0 +1,6 @@
+#! /bin/bash
+
+DISK=/mnt/backup/rsync
+mkdir -p $DISK
+rpm -qa | sort > "$DISK"/installed-packages-`date +%F`.txt
+/usr/local/bin/do-backup $DISK
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backup-to-disk	Sat Aug 02 19:31:02 2014 +0100
@@ -0,0 +1,18 @@
+#! /bin/bash -e
+
+DISK=/run/media/ibboard/backup-linux/
+
+if [ ! -d "$DISK" ]; then
+	echo "Disk not found at $DISK!" >&2
+	exit 1
+fi
+
+cp "$(ls -1 /mnt/backup/rsync/installed-packages-*|tail -n1)" "$DISK"
+rm "$(ls -1 "$DISK"/installed-packages-* | head -n1)"
+rm -rf $DISK/backup-prev
+if [ -d "$DISK"/backup ]; then
+	mv $DISK/backup{,-prev}
+fi
+mkdir $DISK/backup
+/usr/local/bin/do-backup "$DISK"/backup/
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backup-website-images	Sat Aug 02 19:31:02 2014 +0100
@@ -0,0 +1,18 @@
+#! /bin/bash
+
+dest=~/Websites/image-backup/
+mkdir -p $dest
+
+paths="glittergoth/image/data
+glittergoth/image/*.*
+glittergoth/AlchemyCatalogue/*.*
+glittergoth/AlchemyCatalogue/Thumbs/*.*
+skins/images"
+
+for path in $paths; do
+	dest_path=${dest}${path%/*}
+	mkdir -p $dest_path
+	rsync -ravz -e "ssh -i /home/ibboard/.ssh/cron" vps:/srv/sites/$path $dest_path >> /home/ibboard/cron.log 2>&1
+done
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backup-websites	Sat Aug 02 19:31:02 2014 +0100
@@ -0,0 +1,6 @@
+#! /bin/bash
+
+for site in Admin CMS ErrorHandling Forums GlitterGoth GraceBertram HWT IBBoard Puppet PuppetPrivate SkinsAtHWT WarFoundry server-usr-local; do
+	cd ~/Websites/$site
+	hg pull --config extensions.mercurial_keyring=\!
+done
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/do-backup	Sat Aug 02 19:31:02 2014 +0100
@@ -0,0 +1,16 @@
+#! /bin/sh
+
+if [ $# -ne 1 ]; then
+	echo "Usage: $0 <dest>" >&2
+	exit 1
+elif [ ! -d "$1" ]; then
+	echo "Destination $1 did not exist/was not a directory" >&2
+	exit 1
+fi
+
+DISK="$1"
+RSYNC_ARGS=${RSYNC_ARGS:-}
+rsync -av $RSYNC_ARGS --exclude=.thumbnails --exclude=temp --exclude=banshee.db --exclude=.cache --exclude=.local/share/Trash --exclude=.SpiderOak --exclude Documents/Games\ Workshop/LivingRuleBooks /home/ibboard "$DISK"/
+rsync -av $RSYNC_ARGS --exclude=Videos/Other --include=Downloads/games --include=Downloads/M4Ps --exclude=Downloads/* --exclude=.Trash-1000 --exclude=.Trash-1001 --exclude=Pictures/Photos/Temp --exclude=VirtualBox/VBoxGuestAdditions* /mnt/media "$DISK"/
+rsync -av $RSYNC_ARGS /usr/local/bin "$DISK"/
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/find-small-album-art	Sat Aug 02 19:31:02 2014 +0100
@@ -0,0 +1,6 @@
+#! /bin/sh
+
+identify ~/.cache/media-art/*.jp{,e}g 2>/dev/null | while read file; do size=$(echo $file | awk '{print $3}'); size1=$(echo $size | cut -f1 -dx); size2=$(echo $size | cut -f2 -dx); if [ $size1 -lt 500 -a 
+$size2 
+-lt 500 ]; 
+then echo $file | awk '{print $1 " " $3 }' | sed 's/\[[0-9]\+\]//'; fi; done
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/make-certificate	Sat Aug 02 19:31:02 2014 +0100
@@ -0,0 +1,27 @@
+#! /bin/bash
+
+set -e
+
+if [ $# -ne 1 ]; then
+	echo "Usage: $0 <key-name>"
+	exit 1
+fi
+
+if [ ! -d ~/Websites/certs/ ]; then
+	echo "~/Websites/certs/ didn't exist - can't create certs"
+	exit 1
+fi
+
+pushd ~/Websites/certs/
+
+#openssl genrsa -des3 -out $1.key 4096
+#openssl req -new -key $1.key -out $1.csr
+#openssl x509 -req -days 365 -in $1.csr -CA ~/.ssh/CA/ca.crt -CAkey ~/.ssh/CA/ca.key -CAserial ~/.ssh/CA/ca.srl -out $1.crt
+#-set_serial $serial
+#openssl rsa -in $1.key -out $1.key.insecure
+#mv $1.key $1.key.secure
+#mv $1.key.insecure $1.key
+#echo $(($serial + 1)) > ~/.ssh/CA/next-serial.dat
+openssl req -nodes -newkey rsa:2048 -keyout $1.key -out $1.csr -subj "/C=GB/ST=Worcestershire/O=$1/CN=$1"
+openssl x509 -req -days 365 -in $1.csr -CA ~/.ssh/CA/ca.crt -CAkey ~/.ssh/CA/ca.key -CAserial ~/.ssh/CA/ca.srl -out $1.crt
+popd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mount-crypt	Sat Aug 02 19:31:02 2014 +0100
@@ -0,0 +1,17 @@
+#! /bin/bash
+
+if [ $# -ne 1 ]; then
+	echo "Usage: $0 <filename>" >&2
+	exit 1
+fi
+
+if [ ! -f "$1" ]; then
+	echo "File doesn't exist" >&2
+	exit 2
+fi
+
+LOOP_DEV=$(sudo losetup --find --show "$1")
+MAPPER=${LOOP_DEV##*/}
+sudo cryptsetup luksOpen $LOOP_DEV $MAPPER
+sudo mkdir -p /media/$MAPPER
+sudo mount /dev/mapper/$MAPPER /media/$MAPPER
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/record-app	Sat Aug 02 19:31:02 2014 +0100
@@ -0,0 +1,33 @@
+#! /bin/bash
+
+echo "Does not work yet" >&2
+exit 1
+
+
+
+MYCMD=$1
+
+if [ -z "$MYCMD" ]; then
+	echo "Usage: $0 <app-name>" >&2
+	exit 1
+fi
+
+INDEX=''
+pacmd list-sink-inputs | while read line; do
+	if echo "$line" | grep -qE "^index:"; then
+		index=$(echo "$line" | cut -f2 -d:)
+	elif echo "$line" | grep -qE "^client:.*$MYCMD"; then
+		INDEX=$index
+	fi
+done
+
+if [ -z "$INDEX" ]; then
+	echo "Unable to find stream for $MYCMD" >&2
+	exit 1
+fi
+
+
+# TODO: Work out null sink stuff
+pactl load-module module-null-sink sink_name=steam
+pactl move-sink-input $INDEX steam
+parec -d steam.monitor | oggenc -b 192 -o steam.ogg --raw -
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/record-audio	Sat Aug 02 19:31:02 2014 +0100
@@ -0,0 +1,12 @@
+#! /bin/bash
+
+WAV="$1"
+if [ -z "$WAV" ]; then
+    echo "Usage: $0 OUTPUT.WAV" >&2
+    exit 1
+fi
+rm -f "$WAV"
+
+MONITOR=$(pactl list | grep -A2 '^Source #' | \
+    grep 'Name: .*\.monitor$' | awk '{print $NF}' | tail -n1)
+parec -d "$MONITOR" | sox -t raw -r 44100 -Lb 16 -e signed-integer -c 2 - "$WAV"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rename-photos	Sat Aug 02 19:31:02 2014 +0100
@@ -0,0 +1,9 @@
+#! /bin/bash
+
+DATE_PREFIX=$(date +Y%Y_%m)
+
+rename SAM_ ${DATE_PREFIX}_SAM_ SAM_*.JPG
+rename DSC_ ${DATE_PREFIX}_DSC_ DSC_*.JPG
+rename IMG_ ${DATE_PREFIX}_IMG_ IMG_*_*.JPG
+rename PANO_ ${DATE_PREFIX}_PANO_ PANO_*_*.JPG
+rename JPG jpg ${DATE_PREFIX}*.JPG
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tidy-fspot-database	Sat Aug 02 19:31:02 2014 +0100
@@ -0,0 +1,142 @@
+#!/bin/bash
+ 
+# A script to find missing files in the f-spot database, and then delete them.
+# At present these files crash f-spot. It's frustrating as all hell.
+ 
+echo "Welcome to the f-spot database cleaner. All the usual disclaimers apply, as you might imagine."
+echo "What would you like to do: " 
+echo "  1) Run in demo-mode " 
+echo "  2) Clean up your f-spot database" 
+echo "  3) Quit" 
+read -p "Your choice: " choice
+ 
+case $choice in 
+    1) demomode="true";;
+    2) demomode="false";;
+    3) exit 0;;
+esac
+ 
+# With that beginning stuff out of the way, let us do some functions
+# First, a function to gather the database contents and to print out the ones that are orphans
+ 
+function findAndFixOrphans {
+    # find our db, and set a var. Checking for XDG path first, since it's the more recent location of the db
+    if [ -f $XDG_CONFIG_DIR/f-spot/photos.db ] #checks if the $XDG_CONFIG_DIR variable is in use
+    then
+        DBPATH=$XDG_CONFIG_DIR/f-spot/photos.db
+    elif [ -f $HOME/.config/f-spot/photos.db ] #uses the default $XDG location, if that's being used.
+    then
+        DBPATH=$HOME/.config/f-spot/photos.db
+    elif [ -f $HOME/.gnome2/f-spot/photos.db ] #uses the old location of the DB, if the former aren't in use.
+    then
+        DBPATH=$HOME/.gnome2/f-spot/photos.db
+    else
+        echo "Error: Could not find database. Damn." 
+        exit 1
+    fi
+ 
+    # Select the filenames, and put them in a variable.
+    filenames=$(sqlite3 $DBPATH "SELECT base_uri || '/' || filename FROM PHOTOS")
+    filenames_versions=$(sqlite3 $DBPATH "SELECT base_uri || '/' || filename FROM PHOTO_VERSIONS")
+ 
+    # Chomp off the first instance of file://, and replace the rest with newlines.
+    filenames=$(echo $filenames | sed 's/file:\/\///' | sed 's/file:\/\//\n/g'  )
+    filenames_versions=$(echo $filenames_versions | sed 's/file:\/\///' | sed 's/file:\/\//\n/g' )
+ 
+    if [ $demomode == "true" ]
+    then            
+        while read -r line
+        do
+            # Decode the filename
+            decodedLine=$(echo -e "${line//\%/\\x}")
+            if [ ! -f  "$decodedLine" ] 
+            then
+                # If the file doesn't exist, we output the filename, if in demomode, or we fix it if we are not in demomode.
+                echo  "Errant record found in the photos table: $decodedLine"
+            fi
+        done <<< "$filenames"
+ 
+        # We do the same for the photo_versions table
+        while read -r line
+        do
+            # Decode filename
+            decodedLine=$(echo -e "${line//\%/\\x}")
+            if [ ! -f "$decodedLine" ]
+            then
+                # If the file doesn't exist, we output the filename
+                echo "Errant record found in the photo_versions table: $decodedLine"
+            fi
+        done <<< "$filenames_versions"
+ 
+    else
+        # We backup the database, and make the correction
+        cp $DBPATH $DBPATH.`date -I`.bak
+        if [ $? -eq 0 ]
+        then
+            #The backup worked, tell the user.
+            echo "Your database has been backed up to $DBPATH.`date -I`.bak"
+        else
+            echo "Error backing up your database."
+            exit 3
+        fi
+ 
+        # First we do the photos table
+        while read -r line
+        do
+            # Decode the filename
+            decodedLine=$(echo -e "${line//\%/\\x}")
+ 
+            if [ ! -f "$decodedLine" ]
+            then
+                # Do some sql here.
+                foo="file://${line}"
+                base_uri=${foo%/*}
+                filename=${foo##*/}
+                echo -n "Deleting URI $line from the database table photos..."
+                sqlite3 $DBPATH "DELETE FROM PHOTOS WHERE base_uri = '$base_uri' AND filename = '$filename'"
+                echo "done."
+            fi
+        done <<< "$filenames"
+ 
+        # Then we do the photo_versions table
+        while read -r line
+        do
+            # Decode the filename
+            decodedLine=$(echo -e "${line//\%/\\x}")
+ 
+            if [ ! -f "$decodedLine" ]
+            then
+                #Do some sql stuff
+                foo="file://${line}"
+                base_uri=${foo%/*}
+                filename=${foo##*/}
+                echo -n "Deleting URI $line from the database table photo_versions..."
+                sqlite3 $DBPATH "DELETE FROM PHOTO_VERSIONS WHERE base_uri = '$base_uri' AND filename = '$filename'"
+                echo "done."
+            fi
+        done <<< "$filenames_versions"
+ 
+    fi
+ 
+}
+ 
+if [ "$demomode" == "true" ]
+then
+    echo "Great. Proceeding in demomode."
+ 
+    findAndFixOrphans
+ 
+    echo "Demomode successfully finished. Exiting."
+    exit 0;
+elif [ "$demomode" == "false" ]
+then
+    echo "Great. Cleaning up your database."
+ 
+    findAndFixOrphans    
+ 
+    echo "Database cleaned successfully."
+    exit 0;
+else
+    echo "Something strange happened. See the script for details. Exiting."
+    exit 2;
+fi
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/umount-crypt	Sat Aug 02 19:31:02 2014 +0100
@@ -0,0 +1,24 @@
+#! /bin/bash
+
+if [ $# -ne 1 ]; then
+	echo "Usage: $0 <filename>" >&2
+	exit 1
+fi
+
+if [ ! -d "$1" ]; then
+	echo "Directory doesn't exist" >&2
+	exit 2
+fi
+
+ARG=${1%/}
+LOOP_DEV=${ARG##*/}
+
+if [ ! -d "/media/$LOOP_DEV" -o ! -b "/dev/mapper/$LOOP_DEV" ]; then
+	echo "Not a crypto loop" >&2
+	exit 3
+fi
+
+sudo umount "/media/$LOOP_DEV"
+sudo rmdir "/media/$LOOP_DEV"
+sudo cryptsetup luksClose "/dev/mapper/$LOOP_DEV"
+sudo losetup -d "/dev/$LOOP_DEV"