Match email address

If you have a file with several lines that contains email addresses that you like to extract, you can do that with Linux grep command:

File “emails.txt”:

somestreng anotherstring 342345 somename.lastname@domain.com

where somename.lastname@domain.com is the string you like to extract:

you can do this with:

grep -EiEio '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b' emails.txt

Result:

somename.lastname@domain.com

git-cola in Windows 7

I have Windows 7 on my computer, but needed to run a git gui tool for our project. Searching on Google I found several, but they all were software for Linux. We have plenty of Linux Redhat servers, but no one running X window.

I installed then a Xming X server (http://sourceforge.net/projects/xming/) on my Windows 7 computer, and by configuring Putty (also a Windows program) and the sshd_config on the Linux server I could run git-cola and other Linux software in my Windows enviroment:

git-cola3

Putty configuration:

Connection -> SSH -> X11:

Enable X11 forwarding = true

X display location: location:0.0

 

Linux Redhat server configuration:

X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost yes

 

Also I noticed if I changed to another user in Putty (SSH) with:

sudo su –

then I wouldn’t be able to forward any X program. So, just after loggin in to the server in Putty with SSH, I had to start the program right away:

 

 

References:

 

 

 

Sending an email with a different From: address

Sometimes you have to send an email with a different “From:” address. This can be easily done in LINUX. Please see the video below, and read the text below the video for further explanations. If you have questions, please, just add your comment, I will be happy to answer 🙂

Was the video helpful? Here are some explanations of the commands that I used:

The:

export EMAIL="Title Firstname Lastname <differentaddress@validemail.uib.no>"

sets the environment variable EMAIL to what you specify between the two “”. Note, the syntax has to be correct, remember the two <>

The command:

echo -e "some text" | mutt -s "subject text" somename@somewhere

means:

Send the text “some text” through a pipe (the character “|”)  to the program “mutt”, which will send the email to somename@somewhere with the subject “subject text” and the body-text “some text”

If you need to send to multiple people, this is achieved by making the list of email addresses separated with a comma. For example:

echo -e "my message to you" | mutt -s "my subject text" firstperson@something.no,secondperson@somethingelse.no

NB: The Linux command above is written on one-line. No Enter or Returns on keyboard should be done.

Now you can send your emails with any From: address you like.

Remember: it is still your account that sends the email, so changing the From: address doesn’t hide your real identity in the email system.

Searching in Excel files

I had to find a specific Microsoft Spreadsheet among thousands of files on a mapped network drive O: on a Windows 7 Computer. The problem was that searching and indexing was only performed on local disk like C: and D: so I could only search for keyword in local Excel files. It is not optimal, because it means copying thousands of temporary excel files to a local drive (D:), but as long as I know that it was a Excel file, I could copy only this file-types. But, when you need something, you need something. This is how I solved it.

– Mount the Windows share on a Linux server (requires sudo rights)

– Find the total size of all excel files on mapped network drive (so that you know the size of the total number of files that you have to put on your local Drive)

– Copy all excel files from share to local disk

– Let Windows 7 index the excel files locally (should happen automatically when new files are added)

– Finally, search for keywords in Windows Explorer and finding the Excel spreadsheet in question.

First mount the share:
sudo mount -t cifs -o username=,domain=example.com //WIN_PC_IP/ /mountdir

where mountdir is any name for a folder. The mount command will create it. It could be your username for instance.
WIN_PC_IP is the ip number of the Windows computer where your share is located.

To find the total size of the files:

cd mountdir

find . -name '*.xls*' -exec ls -l {} \; | awk '{ Total += $5} END { print Total}'

Then find all the excel files, and copy them to a new folder:
cd ..
mkdir EXCEL-FILES-FOLDER/
find . -iname '*.xls' -exec cp --parent {} EXCEL-FILES-FOLDER/ \;
find . -iname '*.xlsx' -exec cp --parent {} EXCEL-FILES-FOLDER/ \;

Copy the files to local D: drive (using CMD in Windows 7)

D:\>copy "o:\EXCEL-FILES-FOLDER\*" "d:\EXCEL-FILES-FOLDER\"

 
You might have to wait some hours, because if you have a lot of files, the Windows 7 computer might use a day or two before it is finished. “PATIENCE YOU MUST HAVE my young padawan”..

Postgresql 9.1 BACKUP DUMP BASH script

We wrote an improved postgresql dump bash script for Postgresql version 9.1. This one will save each dump file with the name: database_name_DAYNAME.sql.bz2
In this way, we would only have 7 backups at any time, because each file will be overwritten after seven days. Since our backup system (TSM) saves 7 versions of each file, we would then have 49 versions at any time. That means we can go 49 days back in time to restore a certain dumpfile. In addition the script saves monthly a dump file with the name: database_name_MONTHNAME.sql.bz2.
You can also set a variable TEST to ‘yes’ to, then the script will only dump one specific filename: database_name_daily.sql.bz2. This can be useful on Test servers, where you might not be interested in a historical backup back in time.

Here it is:

#!/bin/bash

## This scripts dumps all the databases in a Postgres 9.1 server, localhost
## 2 dump files are made for each database. One with Inserts another without.

## TODO
# - implement a 'if system is Test' option to minimize number of dump files UNDER PROGRESS
# - use functions instead?
# - some kind of integration with Jenkins?
# - fix the 2 strange '|' that appears in the DATABASE list FIXED?
# - Add timer so we can optimize speed of the script execution time
# - enable use of the logfile LOGFILE. Could be nice to log what this script is/has been doing and when.
# - number of days to keep a dump file could be a parameter to this script
# - enable print of name of the script, where the script is run (hostname and directory). Makes it easy to find the script on a server
# - would be nice to add a incremental feature for this script. Then one can dump files several times a day, without worrying about space problems on the harddisk DIFFICULT?
## TODO END

# Timer
start_time=$(date +%s)

# Variables
LOGFILE="/var/lib/pgsql/9.1/data/pg_log/pgsql_dump.log"
BACKUP_DIR="/var/backup/postgresql_dumps"
BACKUP_DIR2="var/backup/postgresql_dumps" # Gosh..
HOSTNAME=`hostname`
MAILLIST="someone att somewhere" # should be edited
# Is this a test system? Set TESTSYSTEM to 'yes' in order to remove date and time information from dumpfile names (in order to minimize number of dumpfiles).
TESTSYSTEM="no"
TODAY=$(date|awk '{ print $1 }')
MONTH=$(date|awk '{ print $1 }')
MONTHNAME=`date +%b --date '0 month'`
DAYINMONTH=$(date|awk '{ print $3 }')
YEAR=$(date | awk '{ print $6 }')

# Only postgres can run this script
if [ `whoami` != "postgres" ]; then
echo "pgsql_dump tried to run, but user is not postgres!" >> $LOGFILE
echo "You are not postgres, can not run."
echo "Try: su -c ./pgsql_dump.sh postgres"
exit;
fi

# Check if there any backup files. If not, something is wrong!
if [ `find $BACKUP_DIR -type f -name '*.sql.bz2' -mtime -2 | wc -l` -eq 0 ]; then
echo "There are no pgsql dumps for the last 2 days at $HOSTNAME. Something is wrong!" | mail -s "[PGSQLDUMP ERROR] $HOSTNAME" $MAILLIST
fi

# logfile might be nice to have (or maybe Jenkins is the way to go?)
if [ ! -e $LOGFILE ]; then
touch $LOGFILE
fi

if [ $TESTSYSTEM == "yes" ];then
#DATABASES=`psql -q -c "\l" | sed -n 4,/\eof/p | grep -v rows | grep -v template0 | awk {'print $1}' | sed 's/^://g' | sed -e '/^$/d' | grep -v '|'`
# For testing purposes
DATABASES="database-1
database-2"
else
DATABASES=`psql -q -c "\l" | sed -n 4,/\eof/p | grep -v rows | grep -v template0 | awk {'print $1}' | sed 's/^://g' | sed -e '/^$/d' | grep -v '|'`
fi

for i in $DATABASES; do

## Create folders for each database if they don't exist
if [ ! -d "$BACKUP_DIR/$i/" ];then
mkdir $BACKUP_DIR/$i
fi
if [ ! -d "$BACKUP_DIR/$i/daily" ];then
mkdir $BACKUP_DIR/$i/daily
fi
if [ ! -d "$BACKUP_DIR/$i/monthly" ];then
mkdir $BACKUP_DIR/$i/monthly
fi

# On Test servers we don't want dump files with date and time information
if [ $TESTSYSTEM == "yes" ];then
DAILYFILENAME="daily_$i"
MONTHLYFILENAME="monthly_$i"
ALLDATABASESFILENAME="all-databases"
else
DAILYFILENAME="daily_$i_$TODAY"
MONTHLYFILENAME="monthly_$i_$MONTHNAME"
ALLDATABASESFILENAME="all-databases_$TODAY"
fi

# backup for each weekday (Mon, Tue, ...)
nice -n 10 /usr/pgsql-9.1/bin/pg_dump --column-inserts $i > $BACKUP_DIR/$i/daily/"$DAILYFILENAME".sql
nice -n 10 tar cjf $BACKUP_DIR/$i/daily/"$DAILYFILENAME".sql.bz2 -C / $BACKUP_DIR2/$i/daily/"$DAILYFILENAME".sql
rm -f $BACKUP_DIR/$i/daily/"$DAILYFILENAME".sql

# dump with copy statements
nice -n 10 /usr/pgsql-9.1/bin/pg_dump $i > $BACKUP_DIR/$i/daily/"$DAILYFILENAME"_copy.sql
nice -n 10 tar cjf $BACKUP_DIR/$i/daily/"$DAILYFILENAME"_copy.sql.bz2 -C / $BACKUP_DIR2/$i/daily/"$DAILYFILENAME"_copy.sql
rm -f $BACKUP_DIR/$i/daily/"$DAILYFILENAME"_copy.sql

# monthly backup (Jan, Feb...)
if [ $DAYINMONTH==10 ]; then
cp -f $BACKUP_DIR/$i/daily/"$DAILYFILENAME".sql.bz2 $BACKUP_DIR/$i/monthly/"$MONTHLYFILENAME".sql.bz2
cp -f $BACKUP_DIR/$i/daily/"$DAILYFILENAME"_copy.sql.bz2 $BACKUP_DIR/$i/monthly/"$MONTHLYFILENAME"_copy.sql.bz2
fi

# Year backup
# coming after a while

done

## Full backup
nice -n 10 /usr/pgsql-9.1/bin/pg_dumpall --column-inserts > $BACKUP_DIR/"$ALLDATABASESFILENAME".sql
nice -n 10 /usr/pgsql-9.1/bin/pg_dumpall > $BACKUP_DIR/"$ALLDATABASESFILENAME"_copy.sql
nice -n 10 tar cjf $BACKUP_DIR/"$ALLDATABASESFILENAME".sql.bz2 -C / var/backup/postgresql_dumps/"$ALLDATABASESFILENAME".sql
nice -n 10 tar cjf $BACKUP_DIR/"$ALLDATABASESFILENAME"_copy.sql.bz2 -C / var/backup/postgresql_dumps/"$ALLDATABASESFILENAME"_copy.sql
rm -f $BACKUP_DIR/"$ALLDATABASESFILENAME".sql
rm -f $BACKUP_DIR/"$ALLDATABASESFILENAME"_copy.sql

## Vacuuming (is it really necessary for PG 9.1? Don't think so...)
#nice -n 10 vacuumdb -a -f -z -q

finish_time=$(date +%s)
echo "Time duration for pg_dump script at $HOSTNAME: $((finish_time - start_time)) secs." | mail $MAILLIST

Mysql SQL in bash one-liner

If you just need a quick way to get some data from a mysql database in your shell (bash), you could do something like this in one line:

mysql -h your.server.edu -u db_username -p`cat /path/to/your/homedir/secretpasswordfile` -e ";use databasename; SELECT tablename.columnname FROM tablename where id like '421111' and something like '1' and option like '23';"; > /tmp/datayouwant.txt; while read i; do echo ";$i";; done < /tmp/datayourwant.txt | sort | uniq

If you don't like to scroll:
-bash-3.2$ mysql -h your.server.edu -u db_username -p`cat /path/to/your/homedir/secretpasswordfile` -e "use databasename; SELECT tablename.columnname FROM tablename where id like '421111' and something like '1' and option like '23';" > /tmp/datayouwant.txt; while read i; do echo "$i"; done < /tmp/datayourwant.txt | sort | uniq

On my server I would then get a list of words/numbers or whatever you might have in the database, which one might want to use further in another script or command:

Dikult
Drupal
DSpace
Mediawiki
Open Journal Systems
Piwik
Postgresql og Mysql
Redhat Enterprise Linux 6 (RHEL6)
Redmine
Solr
Webmail (RoundCubemail)
Wordpress
Xibo