Monthly Archive for June, 2010

SMBackup

This Script was designed to backup a database onto a backup-server that (only) provides SMB-Shares.
If there are already more than $CYCLE files present within the destination directory, the oldest file will be overwritten.

You might get the impression that I am abusing my blog to host scripts.
Well, you’re right ;-)

There you go:

#!/bin/sh
# This script backs up a db with adjustable cycles
 
BAKPATH='/mnt/backup'                                   ## The Path to store the backups in
SMBPATH='//10.10.10.10/backup'                     	## Path to the smb-share to store the backup in
SMBUSER='user'                                     	## User to login with
SMBPASSWD=''                                		## Password to use for smb-auth. in base64 encoding
CYCLE=7                                                 ## Number of backup-files in one cycle, the oldest backup will be overwritten
SUFFIX='_backup.sql'                                    ## The suffix of the filename
MYSQL_ROOTPW=''                             		## MySQL-Password for root in base64 encoding
MYSQL_DB='somedb'                                 	## The database to be backed up ;)
LOGFILE='/var/log/backups'                              ## Where shall i write logs to?
 
### Mount the SMB-share to BAKPATH
mount -t smbfs $SMBPATH $BAKPATH -o username=$SMBUSER,password=$( echo $SMBPASSWD | openssl enc -base64 -d )
 
### Is it mounted correctly?
MOUNTS=$( mount | grep $BAKPATH | wc -l )
if [ $MOUNTS -eq 0 ] ; then
        echo $(date)"   Couldn't map SMB-share to my local FS! Exiting, no Backup created" >> $LOGFILE;
 
        if [ ! -d $BAKPATH ]; then
                echo $(date)"   The specified BAKPATH doesn't exist!" >> $LOGFILE;
        fi
 
else
        echo $(date)"   SMB-share $SMBPATH mapped successfully to $BAKPATH!" >> $LOGFILE;
 
### Get the number of files in our BAKPATH
        NUMFILES=$(ls -1 $BAKPATH | wc -l)
 
### While the number of backups is >= $CYCLE
        while [ $NUMFILES -ge $CYCLE ]; do
 
### Get the oldest backup; delete it and do some output/logging
                OLDESTFILE=$( ls -t1 $BAKPATH | tail -n 1 )
                OLDESTABSFILE=$BAKPATH"/"$OLDESTFILE
                rm $OLDESTABSFILE
                if [ ! -f $OLDESTABSFILE ]; then
                        NUMFILES=`expr $NUMFILES - 1`
                        echo $(date)"   Deleted Backup $OLDESTABSFILE to make space ;)" >> $LOGFILE
                fi
 
        done
 
### Now create the backup
        FILE=$(date +%Y%m%d)"_"$(date +%H%M%S)$SUFFIX
        ABSFILE=$BAKPATH"/"$FILE
        mysqldump --user=root --password=$( echo $MYSQL_ROOTPW | openssl enc -base64 -d ) $MYSQL_DB > $ABSFILE
        tar -czf $ABSFILE.tar.gz $ABSFILE
        rm $ABSFILE
 
### Do some logging
        if [ -f $ABSFILE.tar.gz ]; then
                echo $(date)"   Backup '$ABSFILE' successfully created" >> $LOGFILE;
        else
                echo $(date)"   Couldn't create the backup!" >> $LOGFILE;
        fi
        umount $BAKPATH
 
fi

Mount/Unmount ISO images in nautilus

Hey,

I figured out that nautilus, the file browser of GNOME, has a nice feature of adding scripts to the context menu (right click).
For instance, you can place the following two scripts in ~/.gnome2/nautilus-scripts/ and make them executable (chmod +x).
Then you will be able to do a right-click on iso-images for (un)mounting them.

mount.sh:

#!/bin/bash
# mount
 
gksudo -k /bin/echo "got r00t?"
BASENAME=`basename $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS`
MOUNTPOINT="/media/$BASENAME"
 
if [ ! -d "$MOUNTPOINT" ] ; then
	sudo mkdir "$MOUNTPOINT"
fi
 
ret=`sudo mount -o loop,uid=$UID,gid=$GROUPS $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS "$MOUNTPOINT"`
if [ $? ] ; then
	zenity --info --title "ISO Mounter" --text "$BASENAME Successfully Mounted."
	exit 0
else
	zenity --error --title "ISO Mounter" --text "Could not mount $BASENAME!\nReason: $ret"
	sudo rmdir "$MOUNTPOINT"
	exit 1
fi

unmount.sh:

#!/bin/bash
# unmount
 
gksudo -k /bin/echo "got r00t?"
BASENAME=`basename $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS`
MP="/media/$BASENAME"
 
ret=`sudo umount "$MP"`
if [ $? ] ; then
	zenity --info --title "ISO Mounter" --text "Successfully unmounted $MP"
	sudo rmdir "$MP"
	exit 0
else
	zenity --error --title "ISO Mounter" --text "Could not
	unmount $MP\nReason: $ret"
fi

LaTeX PDFmerge

Hey there,

since merging several pdf files is a task that needs to be done pretty often, I decided to write a little TeX file for that.
The initial version assumes that all pdf files have the same name pattern and end with a number:

\documentclass[a4paper,landscape]{article}
\usepackage{pdfpages}
\usepackage{forloop}
 
\begin{document}
 
\newcounter{ct}
\forloop{ct}{1}{\value{ct} < 8}%
{%
    \includepdf[pages=-]{FILENAME\arabic{ct}.pdf}
}
 
\end{document}

About one usage after that, I wrote a perl-script that scans a given directory for pdf files and merges them by creating a TeX-script:

#!/usr/bin/perl
 
use Getopt::Std;
use Switch;
 
$texfile = "/tmp/merge.tex";
$getoptstr = 'p:f:';
$path = '';
$format = 'portrait';
 
sub usage() {
	print <<"EOF";
Usage: $0 -p <path> [-f <l|p>]
	-p: path where the pdf's to merge reside
	-f: page format (defaults to p):
		l: landscape
		p: portrait
EOF
	exit(1);
}
 
@ARGV < 2 and usage();
 
getopts($getoptstr, \%args) or usage();
 
while (($key,$value) = each %args) {
	switch ($key) {
		case 'p' { $path = $value; }
		case 'f' { $format = $value eq "l" ? "landscape" : "portrait"; }
	}
}
 
unless (-e $path) {
	die("Directory \"$path\" doesn't exist!");
}
 
open(TEXFILE, ">" . $texfile)
	or die("Couldn't open TeX-File");
 
print TEXFILE <<"EOF";
\\documentclass[a4paper,$format]{article}
\\usepackage{pdfpages}
\\usepackage{forloop}
\\begin{document}
EOF
 
foreach $file (<$path/*.pdf>) {
	print TEXFILE '\includepdf[pages=-]{'.$file.'}'."\n";
}
print TEXFILE '\end{document}'."\n";
close(TEXFILE);
 
print `pdflatex $texfile`;
 
`rm $texfile` and die("Couldn't delete $texfile");
 
exit(0);

Captions in Algorithm2e

Hey guys,

these days I came across the algorithm2e package of LaTeX, which I prefer to the classic packages algorithm and algorithmic. Unfortunately, it has a weird behaviour concerning captions. One can set the caption font with \SetAlCapFnt, but then the complete caption (including caption label and caption text) will be changed. I can provide the following workaround:

\usepackage[algochapter,boxed,longend,linesnumbered]{algorithm2e}
\newcommand{\myalgofont}[1]{\textbf{\sffamily{#1}}}
\SetKwSty{myalgofont}
\SetAlCapFnt{\normalfont}
 
% fix for captions in algorithms
\makeatletter
% org: \newcommand{\algocf@captiontext}[2]{#1\algocf@typo: \AlCapFnt{}#2}
\renewcommand\algocf@captiontext[2]{\usekomafont{captionlabel}#1\algocf@typo:
\usekomafont{caption}#2}
\makeatother

You can replace the \usekomafont stuff with whatever font-setting you prefer. If, however, you are using a koma document class, then I suggest to use it like that.