Categories
Linux Red Hat Ubuntu

Bash: remove trailing slash from a variable

Suppose you have a variable containing a directorypath:

#!/bin/bash
directorytouse="/home/myhome/whateverdirectory/"

If you try to test in your bash script if the directory exists, then it will fail because of the trailing slash:

if [ ! -d "${directorytouse} ] 
then
    echo -e "directory does not exist!"
fi

The easiest way to remove this / if it is present is to use shell parameter expansion:

if [ ! -d "${directorytouse%/}" ]
then
    echo -e "directory does not exist!"
fi

This will get rid of the trailing slash if there is one. If there is no trailing slash present, then nothing will happen.

Categories
Linux Red Hat Ubuntu

Vi (or Vim) session no longer responding?

If your Vi or Vim session is hanging and is not responding to anything, then it might be because you pressed CTRL+S accidentally (out of habbit). To fix this, just press CTRL+Q to unblock the session.

Categories
Backup Linux MySQL

MySQL: backup your databases

I have put an interesting bash script to backup your database on my git page. You can find it here on https://github.com/afeys/backupmysql/.

Feel free to use and modify it however you please. If you add interesting functionality, or find any annoying bugs, then some feedback through github would be nice.

You can use this script interactively by just typing ./backupmysql.sh in the scripts folder, or you can find out about all the parameters it accepts by using the -h switch.

The script also allows you to create config files with all necessary info to backup a database and then run or schedule it with

/home/backupadmin/scripts/backupmysql.sh -c /home/backupadmin/configs/mycompanydatabase.config > /var/log/backupdatabase.log

Where mycompanydatabase.config is a textfile containing something like this:

databasehost = 10.10.10.1
databaseuser = backupuser
databasepassword = myverysecretdatabasepassword
backupmode = DATA
targetdirectory = /mnt/backupserver/backups/databases
tempdirectory = /tmp
comment = daily backup