Archive for category Linux/Unix

Returning linux timestamp

To return linux timestamp use the following:

Date and time now in timestamp format:
date "+%s"

A specific date and time in timestamp format:

date -d "<time>" "+%s"
e.g. date <del>d "2014</del>01-30 15:00:00" "+%s"
</time>

Date and time now in timestamp format and nano seconds:

date "+%s%N"

Date and time now in timestamp format and milli seconds:

date “+%s%N” | cut -b1-13
or
echo $(($(date “+%s%N”)/1000000))

To return a specific date and time in timestamp with nano or milli seconds then simply add -d parameter similar to the previous example.

No Comments

How to convert Scientific Notation number to Decimal using bash?

Turns out this is pretty easy to do using awk and printf in bash!

Lets say you have a scientific value of “2.1061863333e+03” and you wish to convert it to decimal. You can do something like below:

>> echo "2.1061863333e+03" | awk {' printf "%12.2fn",$1 '}
     2106.19

  • Obviously you can do the same if you have series of values in a file using cat.
  • I found this specially useful when obtaining values from RRDs.

No Comments

Need to modify linux filesystem when mounted in read-only?

In cases where you’re at the Repair Filesystem prompt and need to modify one or more files to fix what is broken then you can mount the root in read-write mode to avoid needing the rescue disk. Otherwise the disk is mounted in read-only mode and you’re unable to modify any files.

The following command will mount the root in read-write mode:

mount -w -o remount /

No Comments

SELinux basic commands

Get a list of all rules currently applied:
getsebool -a

Set a SELinux rule:
setsebool -P <rule_name>=X where X is 0 for OFF and 1 for ON</rule_name>

No Comments

VSFTPD on CentOS – Unable to list files or directories

Details:

  • Running VSFTPD on CentOS

  • The FTP port is opened through IPTables

  • Authenticating using the user credentials works

  • XferLog doesn’t provide any specific details

Solution

  • Checkout /var/log/messages for any hints as to the problem

  • SELinux is blocking the FTP daemon from listing the home directory. Use setsebool -P ftp_home_dir=1 to relax the rule in SELinux

No Comments

MySQL – Backup Script

Great Backup script.

#!/bin/bash

  1. Shell script to backup MySql database
  2. To backup Nysql databases file to /backup dir and later pick up by your
  3. script. You can skip few databases from backup too.
  4. For more info please see (Installation info):
  5. http://www.cyberciti.biz/nixcraft/vivek/blogger/2005/01/mysql-backup-script.html
  6. Last updated: Aug - 2005
  7. --------------------------------------------------------------------
  8. This is a free shell script under GNU GPL version 2.0 or above
  9. Copyright (C) 2004, 2005 nixCraft project
  10. Feedback/comment/suggestions : http://cyberciti.biz/fb/
  11. -------------------------------------------------------------------------
  12. This script is part of nixCraft shell script collection (NSSC)
  13. Visit http://bash.cyberciti.biz/ for more information.
  14. -------------------------------------------------------------------------

MyUSER="SET-MYSQL-USER-NAME" # USERNAME
MyPASS="SET-PASSWORD" # PASSWORD
MyHOST="localhost" # Hostname

  1. Linux bin paths, change this if it can not be autodetected via which command
    MYSQL="$(which mysql)"
    MYSQLDUMP="$(which mysqldump)"
    CHOWN="$(which chown)"
    CHMOD="$(which chmod)"
    GZIP="$(which gzip)"
  1. Backup Dest directory, change this if you have someother location
    DEST="/backup"
  1. Main directory where backup will be stored
    MBD="$DEST/mysql"
  1. Get hostname
    HOST="$(hostname)"
  1. Get data in dd-mm-yyyy format
    NOW="$(date +"%d-%m-%Y")"
  1. File to store current backup file
    FILE=""
  2. Store list of databases
    DBS=""
  1. DO NOT BACKUP these databases
    IGGY="test"

[ ! -d $MBD ] && mkdir -p $MBD || :

  1. Only root can access it!
    $CHOWN 0.0 -R $DEST
    $CHMOD 0600 $DEST
  1. Get all database list first
    DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"

for db in $DBS
do

skipdb=-1 if [ "$IGGY" != "" ]; then
for i in $IGGY
do [ "$db" "$i" ] && skipdb=1 || :
done fi if [ "$skipdb" “-1” ] ; then
FILE=”$MBD/$db.$HOST.$NOW.gz”
  1. do all inone job in pipe,
  2. connect to mysql using mysqldump for select mysql database
  3. and pipe it out to gz file in backup dir :)
$MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE fi
done

Cron Job:
@daily /path/to/yourmysql.sh

No Comments

iSCSI discovery fails with FreeNAS

Error Messages:

Windows Server 2008:
Connection Failed
The iSCSI name specified contains invalid characters or is too long.

Solution:
This issue drove me crazy. I checked the firewall, connectivity, event viewer but didn’t come across anything that would indicate the real problem. I finally checked the log for FreeNAS and didn’t see anything that would indicate what is the problem. After a reboot on the Windows machine and trying again the following warning message showed up in the log which shed light on the issue:

FreeNAS Log:
istgt[2330]: istgt_lu.c:1232:istgt_lu_add_unit: ***WARNING*** TargetName iqn.2007-09.jp.ne.peach.istgt:SQL_Log contains an invalid character.

As you can see in the error message I had named the target as SQL_Log and it looks like the underscore caused the problem! Removed the underscore and fixed the issue..

No Comments

How to set up FreeNAS with VMWare

http://www.sysprobs.com/nas-vmware-workstation-iscsi-target

No Comments

ProFTPD: getting terminated once a week

Once a week on Monday morning when I would come to work the ProFTPD process on one of my linux boxes (Fedora) had terminated and I had to restart the service to get it going. After checking the logs I found that it happened exactly 2AM on Monday morning every week. The following was the error I was getting in the logs:

received SIGHUP -- master server reparsing configuration file

After a bit of research I found that the process was included in the /etc/logrotate.d directory and what was happening was that the logrotate was attempting to rename the log file, compress it and create a new one however that seemed to crash the ProFTPD server. Removed the proftpd entry from the directory and resolved the issue.

No Comments

VSFTPd: Configuration variables

https://www.redhat.com/docs/manuals/enterprise/RHEL-3-Manual/ref-guide/s1-ftp-vsftpd-conf.html

No Comments