Bash Backup Script to backup directories and MySQL databases

Below is the backup script i coded and use on my server to keep backups. You should properly setup variables within the script before using it. You need to setup a cron entry to make it run as frequently as you want. I have set it to run on every sunday at 5PM using following cron entry.

00 17 * * 0 /home/anildewani/backup.sh > /home/anildewani/backup.log 2>&1

Script can make directory backups and MySQL database dumps, and store it on remote FTP location or another drive. Here goes the script:

#!/bin/bash

# Simple Backup Script in bash.
# Coder: Anil Dewani
# Date: 19 Jan 2011

###################################
#           Setup Vars            #
###################################
# Mofidy these variables according to your needs
# which directory to backup
DIR=/var/www
# set backup type. 0 = ftp 1 = other directory
BACKUPTYPE=0

# Set this if you have selected to backup data to other directory only
BACKUPDIR=/home/anildewani/backups/

# Set following ftp details if you have selected to use ftp as backup type
# ftp server
FTPSERVER=ftp.domain.com
# ftp username
FTPUSER="user@domain.com"
# ftp password
FTPPASS=secretpassword
# ftp remote directory
FTPDIR=/backups


# Do you want to take backup of mysql databases too? 0 = false 1 = true
SQLBACKUP=1
# if yes, setup following details
# MySQL username
SQLUSER=root
# MySQL password
SQLPASS=secretpassword
# MySQL databases to include in backup. Seperate each database name with a comma (,)
SQLDBS=mysql,userdata,admindata



##################################
#       Do Not Edit Below        #
##################################

DIRBACKUPFILE=dirbackup.`date +%d-%m-%Y`.tgz
SQLBACKUPFILE=sqlbackup.`date +%d-%m-%Y`.tgz
if [ ! -d $DIR ]; then
    echo "Directory to backup does not exist. Directory backup aborted. <br />"
else
    #do here
    tar -zcvf $DIRBACKUPFILE "$DIR"
    if [ $BACKUPTYPE = 0 ]; then
        ncftpput -u $FTPUSER -p $FTPPASS $FTPSERVER $FTPDIR $DIRBACKUPFILE
        echo "Directory backup file uploaded to FTP Server<br />"
    elif [ $BACKUPTYPE = 1 ]; then
        if [ ! -d $BACKUPDIR ]; then
        echo "Backup Directory does not exist. Creating it right now."
        mkdir $BACKUPDIR
        fi
        mv $DIRBACKUPFILE $BACKUPDIR
        echo "Directory backup file moved to backup directory<br />"
    else
        echo "Backup type was not set properly<br />"
    fi
fi

if [ $SQLBACKUP = 1 ]; then
    #code to create sql files
    arr=$(echo $SQLDBS | tr "," "\n")
    for x in $arr
    do
        mysqldump -u$SQLUSER -p$SQLPASS --opt $x > $x.sql
    done
    #code to make tarball of those sql files
    tar -zcvf $SQLBACKUPFILE *.sql
    for x in $arr
    do
        rm -f $x.sql
    done
    if [ $BACKUPTYPE = 0 ]; then
        ncftpput -u $FTPUSER -p $FTPPASS $FTPSERVER $FTPDIR $SQLBACKUPFILE
        echo "SQL backup file uploaded to FTP Server<br />"
    elif [ $BACKUPTYPE = 1 ]; then
        if [ ! -d $BACKUPDIR ]; then
        echo "Backup Directory does not exist. Creating it right now."
        mkdir $BACKUPDIR
        fi
        mv $SQLBACKUPFILE $BACKUPDIR
        echo "SQL backup file moved to backup directory<br />"
    else
        echo "Backup type was not set properly<br />"
    fi
fi

#exit, bye bye!

Pastebin.com Bash Script – Paste directly from your terminal

Pastebin.com is one of well-known code paste sites around the globe. Programmers mostly use such sites to share code between other programmers or while chatting on IRC to solve some problems. I have developed a bash script which can easily take input from stdin and just throw back pastebin.com url. Forget about copying the code, pasting it again to the site. Below is how you can use the script and the source.

Usage:

#pass stdin using | operator
anil@ubuntu:~$ cat bashcode.txt | ./pastebin -t bash
http://pastebin.com/93SuECwJ

#or you can just use command as below, paste code and then press ctrl + D to get the link
anil@ubuntu:~$ ./pastebin -n anil -t php
<?php
echo "Hey, this is a test";
?>
http://pastebin.com/ixPbWfS9

Source:

#!/bin/bash
# Paste at Pastebin.com using command line (browsers are slow, right?)
# coder : Anil Dewani
# date : Novemeber 7, 2010

#help function
howto()
{
    echo "\
   Pastebin.com Bash Script \
 
    Usage : $0 [ -n <paste name> ] [ -e <paste email> ] [ -t <type of code> ] [ -h ]
   
    <paste name>   Specify the name of paste to be used (optional)
    <paste email>  Specify email to be used while pasting (optional)
    <type of code> Specify code language used, use any of the following values (optional and default value is plain text)

    => Some famous [ -t <type of code> ] Values::

    php - PHP
    actionscript3 - Action Script 3
    asp - ASP
    bash - BASH script
    c - C language
    csharp - C#
    cpp - C++
    java - JAVA
    sql - SQL
    "

}


NAME=
EMAIL=
TYPE=

#getopts, config
while getopts "n:e:t:h" OPTION
do
    case $OPTION in
        n)
            NAME=$OPTARG
            ;;
        e)
            EMAIL=$OPTARG
            ;;
    t)
        TYPE=$OPTARG
        ;;
        h)
            howto
            exit
            ;;
    ?)
        howto
        exit
        ;;
    esac
done

#get data from stdin
INPUT="$(</dev/stdin)"

querystring="paste_private=0&paste_code=${INPUT}&paste_name=${NAME}&paste_email=${EMAIL}&paste_format=${TYPE}"

#post data to pastebin.com API
curl -d "${querystring}" http://pastebin.com/api_public.php

echo ""

You Know You’re An Internet Marketer When

1). You send your kids their allowance by Paypal.
2). Your Mom asks you to email her your children’s Christmas gift wishlist and you embed it with affiliate links.
3). You check domain name availability before naming your kids.
4). You go to work in your underwear.
5). You have trouble explaining to your friends what “you do.”
6). Everybody else is looking for a job and you’re trying to get rid of yours
7). The neighbours think you’re into something illegal when they observe you not going to work but having all the trappings of success.
8). Every item at your garage sale ends with a ‘7.’
9). You add three ps’s to an email to your mother.
10). Your wife accuse you to be cheating on her with Alexa.
11). You make a lot of money spending max 1 hour per day in front of your PC.
12). You do your work in the toilet
13). You wake up on midnight and then your body opens PayPal automatically like a bots, ignoring your wife’s bulging eyes
14). When you announce the birth of your child as a “New Product Launch”
15). When you are worried more getting banned from your cpa network than getting caught cheating on your girlfriend.
16). When you’ve thought about different ways to monetize Justin Bieber and tried at least one.
17). when you take a post like this and bundle it up into a nice little pdf file and throw it up for sale at DP.
18). You would prefer freezing yourself instead of your PayPal account
19). when you parents and other older people you know think that you are going to go to jail someday
20). When 80% of your electric bill goes to all the computers you have on autopilot in your bedroom!
21). when you wake up early on the morning just to see everyone else go to work (have a big grin) and then make a cup of coffee, turn on your computer and check how much you made last night!
22). Christmas is coming up and you tell your friends and family, “No, I don’t want a shirt, a tie, or the Xbox 360. What do you want then? I want Scrapebox, Xrumer, oh, and Sitegrinder too…”
And they’re like…”WTF is a Scrapebox and a Sitegrinder? And Xrumer sounds like some porn shit…”
23). When you don’t tell people you’re an “internet marketer”. Web developer sounds so much better.
24). when you start leaving 100% tips on the table
25). When you hire high school nerds to write articles for you in exchange of alcohal.
26). When people view you with suspicion thinking you send all those Nigerian spam emails.
27). When waking up before 11 am feels like an achievement
28). When you go to a new store and ask for a review copy
29). you press your finger on an underlined word in a printed book
30). Your the only one out of your friends who’s making $$$ on facebook instead of spending $$$> on farmville cash

Installing SSH Server on Windows 2008

windows server 2008 r2Installing an SSH server on the windows server will help you manage your server by remotely accessing the server terminal. It helps you control the server when you don’t have GUI access. All you need is an SSH client (such as puTTY), which helps you develop connection to the server in an encrypted manner.

There are many SSH server options available for windows 2008, some of them are as follows:

  • SSH.com – Free non-commercial SSH Server
  • SSH.com – SSH Tectia Client and Server (commercial)
  • OpenSSH
  • Van Dyke – vShell 3.0 Server (commercial)
  • Free SSHd (this is what we are gonna use in this tutorial)
  • WinSSHd (commercial)
  • Kpym Telnet/SSH Server
  • copSSH for Windows (a modified build of OpenSSH)
  • Sysax Multi-Server (SSH Server) for Windows

We are gonna use Free SSHd in this tutorial, its easiest to install and works perfectly. Visit there official site and download the .exe file from the Downloads page. After download is over, run the exe file..
freesshd setup

Keep pressing next and install it on your server, at the last step you will be asked about creating private keys, press Yes.
private key freesshd

Click yes to run FreeSSHd as a system service.
system service FreeSSHd

FreeSSHd will now be running on your server, the next job is to add an user. To do so, click on FreeSSHd icon at bottom right icon tray. That shall open settings, click on “users” tab and click “add” to add an new user.
add user freesshd

Add details as showed in the image above. There are many other options you can make use of, but i’m pretty sure you can do that well by own ;-)

That being done, It is necessary to add an exception to the windows firewall to allow connections from TCP port 22. Thus, click on “windows firewall and advanced security” option from “Administrative tools” in start menu.
adminstrative and firewall windows

Click on “Inbound Rules” on the left, and after that click on “New Rule” from the right part of window.
add rule

That shall give you a new window. Click on “port” and press Next.
port firewall 22

Select TCP and write “22″ in Specific local ports option. That shall allow connections from port 22.
22 port tcp allow

Keep on pressing Next until Finish.
That’s it. you have set up an SSH server. Now you can use any SSH client (for example, puTTY) and access the server terminal via SSH (tcp port 22).

Creating user friendly interface with dialog command

In this article, we will see how to use “dialog” command to create a user-friendly interface for your bash scripts. dialog command helps us build text-based front-end windows. Still confused about what i am talking about exactly? The below image makes it all clear. We are gonna learn how to make such windows (there are various kinds of them).

I am gonna show you how to create such windows and how to integrate them into your bash script. Following are various different types of dialog box:

    >> calendar, checklist, form, fselect, gauge, infobox, inputbox, inputmenu, menu, msgbox, password, pause, radiolist, tailbox, tailboxbg, textbox, timebox, and yesno.

we are gonna cover some of the basic one’s.
Tip Tip : Mostly the random numbers which you see behind an dialog command are nothing but height and width. heh. :P
msgbox
The image at top of this post is an pure example of an “message box”

$ dialog --title "This is a message box title" \
--msgbox "Hello, this is the message box text." 10 30

yesno

dialog --title "www.anildewani.com" \
        --yesno "Are you gonna press yes or not ?" 5 40

case $? in
0)
        echo "Yes! You pressed Yes";;
1)
        echo "No! You pressed No";;
255)
        echo "Esc? Thats cool with me";;
esac

That would look something like :

Inputbox
Inputbox option is used to read input from user. It mostly prints out instantly upon submission to the stderr. Following is an sample bash script which probably redirects the stderr to an temp file and store the input.

dialog --title "www.anildewani.com" \
        --inputbox "Enter your name..." 10 40 2>/tmp/dialog

case $? in
0)
        echo "Welcome `cat /tmp/dialog`";;
1)
        echo "Cancel was pressed";;
esac

That’s how it shall look like:

Textbox
Textbox option is used to show contents of an file into an window. User can even scroll through and use various keys like pageup and pagedown. User can even search by pressing /

dialog --title "www.anildewani.com" \
 --textbox file.txt 20 50

It will display contents of file.txt in following manner:

Checklist

dialog --title "www.anildewani.com" \
        --checklist "Select the OS you use.." 20 40 3 \
        "CentOS" "" on \
        "Ubuntu" "" off \
        "Fedora" "" off 2>/tmp/dialog

case $? in
0)
        echo "Glad to know you use `cat /tmp/dialog`";;
1)
        echo "Cancel was pressed";;
esac

In above example, 20 is height, 40 is the width and 3 is total number of choices.
Below is image of how this should look like:

Radiolist
Just replace –checklist in checklist example script to –radiolist. Rest is all the same :)

Guage
Guage window can be used to show a progress bar showing the progress of your program. Lets look at an simple example.

{
for i in {1..100}
do
        echo $i
        sleep 1
done
echo;
} | dialog --guage "Loading www.anildewani.com ..." 7 70 0

The dialog commands gets input from code between parenthesis. That’s the tricky part. ;)
Here’s the screenshot of how it looks like:

Will update with more when time allows me :)

Creating a Counter Strike 1.6 No-Steam Linux Server

cs 1.6Creating a personal server for your clan or group of friends is always good. you get to play what you want. In this tutorial i am gonna explain how to create a Counter strike 1.6 Nosteam linux server. Make sure to have around 1-2GB free disk space and an speedy internet connection. First of all, let’s create a separate directory. we call it hlds.

mkdir hlds
cd hlds

Next step would be to download all the essential’s we gonna need. Fire following commands.

wget http://www.steampowered.com/download/hldsupdatetool.bin
wget http://www.linuxcumsa.ro/download/engine.v15.tgz
wget http://ovh.dl.sourceforge.net/sourceforge/halflifeadmin/halflife-admin-2.50.60-linux.tar.gz

First one is the hldsupdatetool, second download is the patch and the last one is “adminmod + metamod”

Setting up the server
1. executing hldsupdatetool.bin

chmod +x hldsupdatetool.bin
./hldsupdatetool.bin

This shall show you agreement, just type yes and press enter. It will extract a new file named “steam”.

2. updating and downloading game data. execute following command

./steam -command update -game valve -dir .

Repeat the same command again. It shall download the game data.

3. Downloading and installing cstrike mod.. execute the following command just once

./steam -command update -game cstrike -dir .

Now, the data is up to date.

4. Patching. Execute the following command and it will patch the server to allow no-steam clients to connect.

rm -f engine_amd.so
rm -f engine_i486.so
rm -f engine_i686.so
tar xfvz engine.v15.tgz

5. Changing server.cfg
Move to /hlds/cstrike directory and delete server.cfg and create server.cfg again in same directory with following data.

hostname "Server Name"
mp_autokick 0
mp_autocrosshair 0
mp_autoteambalance 0
mp_buytime 2
mp_consistency 1
mp_c4timer 35
mp_fadetoblack 0
mp_falldamage 0
mp_flashlight 1
mp_forcecamera 3
mp_forcechasecam 2
mp_friendlyfire 1
mp_freezetime 0
mp_fraglimit 0
mp_hostagepenalty 0
mp_limitteams 6
mp_logfile 1
mp_logmessages 1
mp_logdetail 3
mp_maxrounds 0
mp_playerid 0
mp_roundtime 3
mp_startmoney 16000
mp_timelimit 0
mp_tkpunish 0
mp_winlimit 0
sv_aim 0
sv_airaccelerate 10
sv_airmove 1
sv_allowdownload 1
sv_clienttrace 1.0
sv_clipmode 0
sv_allowupload 1
sv_cheats 0
sv_gravity 800
sv_lan 1
sv_maxrate 7000
sv_maxspeed 320
sv_maxupdaterate 101
sys_ticrate 10000
decalfrequency 60
pausable 0
log on
decalfrequency 60
edgefriction 2
host_framerate 0
exec listip.cfg
exec banned.cfg
rcon_password PASSWORD

Please don’t forget to edit “server name” with your own server name which you desire and PASSWORD with the password you want to set.

6. Setting up adminmod and metamod

tar xfvz halflife-admin-2.50.60-linux.tar.gz

Install adminmod

cd Adminmod
./install_admin

Follow the instructions and install it.

7. Configuring adminmod

cd /hlds/cstrike/addons/adminmod/config/
nano adminmod.cfg

Change allow_client_exec to 1.
Save and close.

8. Adding admin

nano users.ini

add following line to the end of the file

yourusername:yourpassword:131071

Replace it with your own username and password which you desire.

9. Go in /hlds/cstrike directory and open liblist.gam with pico, nano or mcedit. Replace secure “1″ with secure “0″.

10. Starting the server

./hlds_run -game cstrike  +sv_lan 1 -nomaster +maxplayers 18 +map de_dust2

If you get any error related to libSteamValidateUserIDTickets_i386.so..

Error:Error:libSteamValidateUserIDTickets_i386.so: cannot open shared object file: No such file or directory

than go at following url and download libSteamValidateUserIDTickets_i386.so and place it in /hlds directory.

http://amxx.pl/post-a1559-libsteamvalidateuseridtickets-i386rar.html

Or else if you get any error related to metamod_i386.so file.

LoadLibrary failed on /hlds/cstrike/addons/metamod/dlls/metamod_i386.so: /hlds/cstrike/addons/metamod/dlls/metamod_i386.so: cannot open shared object file: No such file or directory

Download the file using following command and try again

cd /hlds/cstrike/addons/metamod/dlls
wget http://prdownloads.sourceforge.net/metamod/metamod-1.19-linux.tar.gz?download
tar xfvz metamod-1.19-linux.tar.gz
rm -f metamod-1.19-linux.tar.gz

Go play Go! ;D

Rapidshare Download Manager Script in Bash

Just developed a simple and dirty rapidshare download manager script in bash. You can pass on the links via STDIN and the script would download them one by one. All the downloaded data is saved in /downloads directory. The script also generates a simple log file named “downloads.log” in the /downloads directory. The script works only for premium rapidshare account users. You need to pass username and password of your rapidshare premium account via command line switches -u and -p respectively.

#!/bin/bash
# Rapidshare Download Manager script
# coder : Anil Dewani

howto()
{
    echo "\
    Rapidshare Download Manager Script \
   
    Usage : $0 -u <username> -p <password>
   
    <username>  Rapidshare premium account username
    <password>  Rapidshare premium account password
   
    This script is used to download a queue of rapidshare files.
    It will create a 'downloads' directory. That's where all your
    downloaded files are saved.
    A downloads.log file would also be created in the downloads directory
    it will display your download logs.
    "

}

USER=
PASS=

while getopts "u:p:" OPTION
do
    case $OPTION in
        u)
            USER=$OPTARG
            ;;
        p)
            PASS=$OPTARG
            ;;
        ?)
            howto
            exit
            ;;
    esac
done
if [[ -z $USER ]] || [[ -z $PASS ]]
then
    howto
    exit 1
fi


echo "Enter rapidshare links and press ctrl + D when you are done.. "
cat > links

echo "Thanks for the links"
echo "Download Queue starting..."

mkdir ./downloads 2>/dev/null

lines=$(cat links | wc -l)

for (( c=1; c<=${lines}; c++ ))
do
    link=$(cat links | sed -n ${c}p)
   
    #check if link is an valid rapidshare.com link
    match="rapidshare.com"
    if [[ "${link}" =~ "${match}" ]] ; then
   
        st=`date`
        name=$(echo ${link} | awk -F "/" '{print $NF}')
       
        # login and store cookiefile
        curl \
        --cookie-jar rapidsharecookie \
        --data "login=${USER}&password=${PASS}" \
        https://ssl.rapidshare.com/cgi-bin/premiumzone.cgi \
        > /dev/null 2>&1

        echo "Downloading ... ${link}"

        # download file using the cookiefile
        curl -# -L -o "./downloads/${name}" --cookie rapidsharecookie ${link}
       
        # write to the log
        echo "Start: ${st}  ${link}  End: `date`" | cat >> ./downloads/downloads.log
    else
        echo "Link number ${c} is not a valid rapidshare link! This link will not be downloaded!"
    fi
done

rm -f links
rm -f rapidsharecookie

what does > /dev/null 2>&1 mean ?

Ever been confused seeing those funny greater than signs being used on a unix system ? Let me try to explain those weird commands.

The greater than sign means the output is being redirected somewhere else. Let’s have a look at an example to be more clear.

wget ${url} > /dev/null 2>&1

In this example, something is being redirected to /dev/null and something else is being redirected to &1.

Before moving further, lets discuss about STDIN,STDOUT, and STDERR. These are the 3 sources of input and output for any particular program. STDIN means Standard Input, STDOUT means Standard Output, and STDERR means Standard Error. They are numerically denoted as 0, 1 and 2 respectively. If you do not name or number any of them, that particularly means you are talking about STDOUT.

Getting back to the example,

  • > /dev/null means that STDOUT is being redirected to /dev/null. It is known as bit-bucket and everything being written there is discarded automatically.
  • 2>&1 means 2 (i.e STDERR) is being redirected to &1 (i.e where the STDOUT is going. In this case /dev/null ).
  • That means, using > /dev/null 2>&1 can be used to make any program work quietly with no STDOUT and/or STDERR.

    This can also be done alternatively as

     >/dev/null 2>/dev/null

    It’s still the same.

    Checking domain availability using PHP

    There are many whois servers around the interweb, we can use such whois server to determine if an domain name is already taken or is still available to be registered. Mostly whois server are connected on port 43. Below is the php script i developed to check domains with com,net,org,info,biz and name extensions.

    <?php
    function checkdomain($domainname)
    {
    	$extension = explode(".",$domainname);
    	$extension = $extension[1];
    	switch($extension)
    	{
    		case "com": $server = "whois.verisign-grs.com"; break;
    		case "net": $server = "whois.verisign-grs.com"; break;
    		case "org": $server = "whois.pir.org"; break;
    		case "info": $server = "whois.afilias.net"; break;
    		case "biz": $server = "whois.biz"; break;
    		case "name": $server = "whois.nic.name"; break;
    		default: return "Given Extension not supported"; exit();
    	}
        // connecting to the whois server.
        $handle = fsockopen($server, 43);
        if (!$handle) 
            return false; // connectin failure 
    
        //asking the server
        fwrite($handle, $domainname."\r\n");
    
        // getting response
        $response = '';
        while (!feof($handle))
            $response .= fgets($handle, 1024);
    
        fclose($handle);
    	//checking if domain is available or not
    	if($extension=="com" || $extension=="net" || $extension=="name")
    	{
    		if(stripos($response, 'No match') === FALSE )
    			return $domainname." is not Available!
    "; else return $domainname. " is Available!
    "; } else { if(stripos($response, "NOT FOUND") === FALSE) return $domainname. " is not Available!
    "; else return $domainname. " is Available!
    "; } } echo checkdomain("anildewani.com"); echo checkdomain("anildewani.net"); echo checkdomain("anildewani.org"); echo checkdomain("anildewani.info"); echo checkdomain("anildewani.biz"); echo checkdomain("anildewani.name"); echo checkdomain("anildewani.in"); ?>