start tmux on login to a new bash

I use tmux for a while now.

It is a Terminal Mutliplexer, so you can open multiple windows and split your windows in lot of panes.

For example I use it for development to have my vim in one pane, my mongo connection in another, and a python shell in the next one.

Sometimes I forgot to open a sessionĀ at startup and later wondered why the shortcuts won’t work…

I tinkered around and just wrote tmux to the end of my .bashrc, not a good idea. It would try open tmux in every new pane or window.

At the end of the day you would have thousands of tmux session nested ;) Trust me you don’t want this.

So you need so check if you are either in a ‘normal’ session like xterm or similar or if you already in a tmux session.

Just append this single line to your .bashrc:

Here I check if the TERM environment variable is NOT set to screen, otherwise I fire up a new tmux session.

Click the link to learn more about the TERM variable

Simple WordPress Bash Backup Script

Hey Guys,

I needed to do some backups of multiple wordpress sites, so I decided to write a backup script especially for wordpress.

All you need is to call the script with two parametes, the first is the folder where your wordpress instance is installed, in my case /var/www/example.com.
The second is the folder where to backup it.

So for example:
./backup_wordpress /var/www/example.com /tmp/backup/example.com

That’s all, the script figures out how to connect to the database, creates a gzipped dump with timestamp, and creates a tar archive also with a timestamp.

You’ll find it in your specified folder under wp and db.

That’s it, have fun.

WTF: /bin/rm: Argument list too long

Imagine you got a directory, with millions of files in it. If you ever had the error message “/bin/rm: Argument list too long”,
you’ll know what I mean.

Here’s a workaround with find.

For example you wanted to delete all pdf files in the directory, here is how to do with find:

find -mindepth 1 -maxdepth 1 -type f -name '*.pdf' -delete

Let’s split the command:

find --> sure, the name of the command
-mindepth 1 --> find will search at least 1 level in depth
-maxdepth 1 --> find will search maximum 1 level in depth
-type f --> search only for files(see man find for other types)
-name '*.pdf' --> search for files ending with .pdf(use -iname for case insensitive search --> damn slow)
-delete --> deletes all matching files

With find you have the full control, and no need to be afraid, that you accidentally delete more files.

Have Fun!

how to get the most out of mkdir

Everybody should know that you are able to build paths with mkdir in bash. For those who don’t:
mkdir -p /a/whole/damn/path
-p means parents. It won’t print any error if a directory in the path exists and will create the missing ones.

Let’s go further and build our first little tree. You can do this by putting folder names in curly braces and seperate them by commas:
mkdir -p level0/{level1-1,level1-2}

Please consider that the comma and curly braces are not a feature of mkdir, but of bash. It is called bash expansion. You can use it in most other commands to,
e.g. touch “touch level0/{file1,file2}” or rm “rm foobar/{*.pdf,1.txt}” <– yep, you can use wildcards here

Ain’t that hard…

Now we come to the coolest point, nest folders.
Let’s assume, we want this structure:

nested tree

You could to this with multiple mkdir-commands, or with just one:

mkdir -p level0/{level1-1/{level2-1/{level3-1,level3-2},level2-2},level1-2}

But what the heck does this mean? Let’s split it.

level0/ - create the folder level0
{level1-1/ - create folder level1-1
{level2-1/ - nest the folder level2-1 into the folder 1-1
{level3-1,level3-2} - nest the folder level3-1 and level3-2 into the folder 2-1
,level2-2} - this folder is again nested under level1-1
,level1-2} - this folder is listed under level0

Everything clear? ;) have fun!

BTW: the tree in the picture is made with tree
sudo apt-get install tree

Quick and dirty mysql backup

When you just need a simple backup of all your databases, this three lines do the job for you.

Just save it to a file and run it as a cron every night
00 01 * * * /srv/scripts/mysql_backup
and you get a zipped dump of every database with a timestamp.

It is easy extendable, if you want to exclude a database just write it into the grep regex
grep -Ev "(information_schema|performance_schema|another_database)"

Have fun! :)

Check your failover from time to time

If you got a failover system like a master slave replication set up, you should change the master from time to time to check how long the pivot will last and if everything works fine.

But how to achieve that? Calendar notifications, notices on the wall, smoke signals? No, of course not.

Why not do it with your monitoring? You look at it mutliple times all day.
Below you can see one possible way to to this.

In most cases you got a virtual IP address(keepalived, pacemaker, …), so I generate file named as the hash sum of a given IP and check its ctime.
Sounds easy, doesn’t it?

Here’s the script:

Now you only need to define a command in Nagios/Icinga:
define command {
command_name failover_status
command_line /usr/lib/nagios/plugins/check_failover $ARG1$ $ARG2$
}

How do you remember yourself doing failover checks?

dmidecode – get every detail about your system

While working as a sysadmin I often need to know hardware details about my server, e.g. for scalability.
How much memory can your system address, how many CPUs has the system got, and so on.

You could go downstairs to your basement or to your datacenter and open every server case and count the free memory banks
or the CPUs. Or just use the powerful framework dmidecode.

If you just type dmidecode you’ll get hundreds of lines, that’s not very useful.
dmidecode support multiple methods to get the information you want. It has keywords, numbers and types.

A few examples:

Getting the Service-Tag of a DELL-Server by a keyword:
dmidecode -s system-serial-number

You want to know how many memory your server is able to address with a type number:
dmidecode -t 16

Or you just want all information about your processors with a type:
dmidecode -t processor

Generate Qr-Codes with Python3

Hey guys,

In a recent project of mine I have to work with qr-codes.
Here’s an easy way to create them with python3.4.

First you have to install the two modules qrcode and Pillow.

pip3 install qrcode Pillow

Then copy this script and save it as qr_gen.py:

import qrcode

class Qrcode:
def init(self, path = None, content = None):
self.path = path
self.content = content

if name == 'main':
from sys import argv, exit
if len(argv) < 3:
print("Usage: %s <text> <path-to-save>" % argv[0])
exit(1)
else:
qr = Qrcode(argv[2], argv[1])
qr.generate()

Make it executable:
chmod +x qr_gen.py

And now you’re able to generate QR codes, e.g. for your website:
./qr_gen.py "http://hauck-daniel.de" hauckdaniel.png

python leet speak generator

#!/usr/bin/env python3
from sys import argv

def convert_text(text):
text = text.upper()
alphabet = get_alphabet()
for letter in text:
if letter in alphabet:
text = text.replace(letter, alphabet[letter])

return text

def get_alphabet():
return {
"A": "4",
"B": "8",
"C": "(",
"D": "[)",
"E": "3",
"F": "|=",
"G": "6",
"H": "|-|",
"I": "|",
"J": ".]",
"K": "|<",
"L": "1",
"M": "|Y|",
"N": "/\/",
"O": "0",
"P": "|>",
"Q": "0,",
"R": "|2",
"S": "5",
"T": "7",
"U": "[_]",
"V": "\/",
"W": "\^/",
"X": "}{",
"Y": "`/",
"Z": "2"
}

print(convert_text(argv[1]))