Skip to content

uberspace and let's encrypt

Nichts besonderes, ich habe einfach nur das Wissen aus dem wiki und dem blog genommen und ich ein shell script gepackt.

Das Resultat ist folgender Cronjob, der einmal aller 60 Tage läuft.

!/bin/bash -l

#

@see:

https://blog.uberspace.de/lets-encrypt-rollt-an/

https://wiki.uberspace.de/webserver:https?s[]=lets&s[]=encrypt

@author: stev leibelt <[email protected]>

@since: 2015-12-28

#

begin of local parameters

LOCALROOTPATH='/home/<user name>' LOCALLOGPATH=$LOCALROOTPATH'/<path to your log files>' LOCAL_ACCOUNT='<your.domain.tld>'

end of local parameters

begin of parameters for letsencrypt-renewer

LOCALCONFIGURATIONPATH=$LOCALROOTPATH'/.config/letsencrypt' LOCALLOGGINGPATH=$LOCALROOTPATH'/.config/letsencrypt/logs' LOCALWORINGPATH=$LOCALROOTPATH'/tmp/'

end of parameters for letsencrypt-renewer

begin of parameters for uperspace-prepare-certificate

LOCALKEYPATH=$LOCALROOTPATH'/.config/letsencrypt/live/'$LOCALACCOUNT'/privkey.pem' LOCALCERTIFICATEPATH=$LOCALROOTPATH'/.config/letsencrypt/live/'$LOCALACCOUNT'/cert.pem'

end of parameters for uperspace-prepare-certificate

letsencrypt-renewer --config-dir $LOCALCONFIGURATIONPATH --logs-dir $LOCALLOGGINGPATH --work-dir $LOCALWORINGPATH &>$LOCALLOGPATH uberspace-prepare-certificate -k $LOCALKEYPATH -c $LOCALCERTIFICATEPATH &>>$LOCALLOGPATH

In schön gibt es das script auch noch einmal hier.
Großen Dank an uberspace und lets encrypt.

Damit das script funktioniert, müsst ihr natürlich zu erst lets encrypt aufsetzen:


uberspace-letsencrypt 
letsencrypt certonly
Ich bin recht faul. Aus diesem Grund lass ich mir die Zertifikate einmal im Monat neu generieren. Um die Infrastruktur nicht zu sehr zu belasten, habe ich mir einen anderen Tag, als den Ersten im Monat ausgesucht. Das gleiche gilt für die Uhrzeit.

Translate to de es fr it pt ja

determine if an apache process is still running via bash to prevent multiple instances running

Given is the fact that you have some processes (like cronjobs) executed via an webserver like apache. Furthermore you have installed and enables apache server status. To add some re usability benefits, we should divide and conquer the problems into either shell scripts or shell functions. Side note, if I am writing about shell, I am in the bash environment. What are the problems we want to tackle down?:

  • find the correct environment
  • check all available webservers if a process is not running
  • specify which process should not run and start it if possible

We can put the first two problems into shell functions like the following ones. I am referencing to some self written shell functions. The reference is indicated by the "net_bazzline_" prefix.

#!/bin/bash
#find the correct environment

if net_bazzline_string_contains $HOSTNAME 'production';
    NET_BAZZLINE_IS_PRODUCTION_ENVIRONMENT=1
else
    NET_BAZZLINE_IS_PRODUCTION_ENVIRONMENT=0
fi

And the mighty check.

#!/bin/bash
#check all available webservers if a process is not running
####
# @param string <process name>
# @return int (0 if at least one process was found)
####
function local_is_there_at_least_one_apache_process_running()
{
    if [[ $# -lt 1 ]]; then
       echo 'invalid number of arguments'
       echo '    local_is_there_at_least_one_apache_process_running <process name>'

       return 1
    fi

    if [[ $NET_BAZZLINE_IS_PRODUCTION_ENVIRONMENT -eq 1 ]]; then
        LOCAL_ENVIRONMENT='production'
    else
        LOCAL_ENVIRONMENT='staging'
    fi

    #variables are prefixed with LOCAL_ to prevent overwriting system variables
    LOCAL_PROCESS_NAME="$1"

    #declare the array with all available host names
    declare -a LOCAL_HOSTNAMES=("webserver01" "webserver02" "webserver03");

    for LOCAL_HOSTNAME in ${LOCAL_HOSTNAMES[@]}; do
        APACHE_STATUS_URL="http://$LOCAL_HOSTNAME.my.domain/server-status"

        OUTPUT=$(curl -s $APACHE_STATUS_URL | grep -i $LOCAL_PROCESS_NAME)
        EXIT_CODE_OF_LAST_PROCESS="$?"

        if [[ $EXIT_CODE_OF_LAST_PROCESS == "0" ]]; then
            echo "$LOCAL_PROCESS_NAME found on $LOCAL_HOSTNAME"
            return 0
        fi
    done;

    return 1
}

And here is an example how to use it.

#!/bin/bash
#specify which process should not run and start it if possible

source /path/to/your/bash/functions

LOCAL_PROCESS_NAME="my_process"

local_is_there_at_least_one_apache_process_running $LOCAL_PROCESS_NAME

EXIT_CODE_OF_LAST_PROCESS="$?"

if [[ $EXIT_CODE_OF_LAST_PROCESS == "0" ]]; then
    echo "$LOCAL_PROCESS_NAME still running"
    exit 0;
else
    #execute your process
    echo 'started at: '$(date +'%Y-%m-%d %H:%M:%S');
    curl "my.domain/$LOCAL_PROCESS_NAME"
    echo 'started at: '$(date +'%Y-%m-%d %H:%M:%S');
fi

You can put this into a loop by calling it via the cronjob environment or use watch if you only need it from time to time:

watch -n 60 'bash /path/to/your/shell/script'

Enjoy your day :-).

Translate to de es fr it pt ja

bash function to search and replace in all files with special extension recursively

Pretty much, the code is below. It is a part of my function collection for the bash.

####

Replaces a string in all files in given path and below

taken from: http://www.cyberciti.biz/faq/unix-linux-replace-string-words-in-many-files/

taken from: http://stackoverflow.com/questions/4437901/find-and-replace-string-in-a-file

taken from: http://stackoverflow.com/questions/7450324/how-do-i-replace-a-string-with-another-string-in-all-files-below-my-current-dir

#

@author stev leibelt artodeto@arcor.de

@since 2013-7-30

#

function netbazzlinereplacestringin_files () { if [[ $# -lt 3 ]]; then echo 'invalid number of arguments provided' echo 'command search replace fileextension [path]' return 1 fi

if [[ $# -eq 4 ]]; then
    find "$4" -name "*.$3" -type f -exec sed -i 's/'"$1"'/'"$2"'/g' {} \;
else
    find . -name "*.$3" -type f -exec sed -i 's/'"$1"'/'"$2"'/g' {} \;
fi

}

Translate to de es fr it pt ja

bash function to sync to and from a remote host by using rsync and ssh

I quickly created two bash functions to use rsync and ssh for syncing directories from the local host to a remote host and vice versa.

The two methods are available on my shell function repository. The two methods are called net_bazzline_sync_from_host and net_bazzline_sync_to_host. Enjoy it :-).

####

Uses rsync with ssh to copy data from remote to local host

#

@param string $1 - remote user

@param string $2 - remote host

@param string $3 - source path on remote host

@param string $4 - destination path on local host

#

@author stev leibelt artodeto@arcor.de

@since 2013-07-05

#

function netbazzlinesyncfromhost () { if [[ $# -eq 4 ]]; then USER="$1" HOST="$2" SOURCE="$3" DESTINATION="$4" else echo 'invalid number of variables provided' echo 'command user host source destination' fi

rsync -avz -e ssh $USER@$HOST:$SOURCE $DESTINATION

}

#

Uses rsync with ssh to copy data from local to remote host

#

@param string $1 - remote user

@param string $2 - remote host

@param string $3 - source path on local host

@param string $4 - destination path on remote host

#

@author stev leibelt artodeto@arcor.de

@since 2013-07-05

function netbazzlinesynctohost () { if [[ $# -eq 4 ]]; then USER="$1" HOST="$2" SOURCE="$3" DESTINATION="$4" else echo 'invalid number of variables provided' echo 'command user host source destination' fi

rsync -avz -e ssh $SOURCE $USER@$HOST:$DESTINATION

}

Translate to de es fr it pt ja

bash - enhanced svn diff

Again i want to share a simple bash function for dealing with the svn diff. I was tired to add the file i want to compare multiple times so i wrote a small wrapper function. The syntax is the following. netbazzlinesvndiff https://svn.mydomain.org/trunk https://svn.mydomain.org/branches/mybranch module/myModule/myFile.foo

Enjoy it and be aware that there is no fancy validation logic inside.

#####
# Calles svn diff for two repositories.
# Call $repositoryUrlOne $repositoryUrlTwo $filePath
#
# @author stev leibelt
# @since 2013-01-30
####
function net_bazzline_svn_diff ()
{
  if [ $# -eq 3 ]; then
    svn diff "$1""/""$3" "$2""/""$3"
  else
    echo 'No valid arguments supplied'
  fi
}

Sourceode also available on github.com.

Translate to de es fr it pt ja

bash - enhanced mkdir

Since i am fully addicted to the shell, the customisation started.

This time, i want to share a small enhancement for the "mkdir" bash command. The code i will paste below is doing the following thing. If you supply one argument, it will create the directory you want with "mkdir -p" and change into that. If you supply multiple arguments, it will behave like the normal "mkdir".

How to use it? Open your .bashrc file and add the code provided below. Then define an alias like "alias mkdir=netbazzlinemkdir". When you open your next shell, your mkdir is enhanced :-).

function net_bazzline_mkdir ()
{
  #check if at least one argument is supplied
  if [ $# -eq 0 ]
  then
    echo "No arguments supplied"
    return 1
  fi

  #if one argument is supplied, create dir and
  # change to it
  if [ $# -eq 1 ]
  then
    mkdir -p "$1"
    cd "$1"
    return 0
  fi

  #if more then one argument is supplied
  # execute default mkdir
  if [ $# -gt 1 ]
  then
    mkdir $@
    return 0
  fi
}

Sourceode available on github.com.

Translate to de es fr it pt ja

tool - zend framework zf cli aka zend tool

Just a quick entry for the zend framework comand line tool (available for *nix and windows). I tought it would be a good idea to use it and create the general files or directory layout. But it is not perfect at all. Creating modules and layouts is quick nice. But a

zf create controller myModuleName
does not what i have expected. An controller was created below the general controller directory and not inside the module "myModuleName". Lets see if i like the tool at all. I will stick on it for this project and fill up the links in the following section when i find some usefull one. I can give one hint so far, the zend tool can only create, not delete anything. If you want to delete something, you have to do it manualy (remember the zfproject.xml file).

available parameters for application.ini official zend tool manual using the cli tool create a layout zend tool in phpstorm zend framework as cli application tutorial from akrabat Proposed Q&A site for PHP developers using Zend Framework 1 and Zend Framework 2

Translate to de es fr it pt ja

howto - mount filesystem via ssh using sshfs

First of all, you have to install "sshfs". After that you just have to type.

sshfs $user@$host:/path/to/dir /path/to/mount

If fuse throws an error of permission, you have to add your user to the group fuse by typing:

usermod -a -G fuse $user

After you re-logged-in you can see by typing "groups" in your shell that everything is done fin. Do not want to logoff? Try

su $user
in your shell. This is a new login and so your new group is available now.

Just another hint, if you want to access symbolic links, try:

-o followsymlinks
and/or
-o transformsymlinks

The man page will help you to understand what you are doing.

Now put everything as an alias in your bashrc (or something similar) and you are done :-).

alias mountMyShare="sshfs [email protected]:/path/to/dir /path/to/mount -o follow_symlinks"; alias umountMyShare="fusermount -u /path/to/mount";

Happy mounting and unmounting.

Translate to de es fr it pt ja

bash - unrar more than one file in separate directories

Long story short, here is the script.

#!/bin/sh for f in \*.rar do mkdir ${f%.rar} unrar e $f ${f%.rar}/ done
What does it? For every *.rar file in you current directory, a subdirectory will be created by using the filename except of '.rar'.

Just use this script on your command line.

sh ~/my/path/to/the/script.sh
Thats it, have fun :-).

It should not be that problem to adapt this script to other types of archive.

Want to know more about bash scripting and string manipulation? Try to check the following links. string manipulation special parameters

Translate to de es fr it pt ja