quick bash script for datewise backup of directory

I was working on something and the data was very critical and needed backup. I wrote a bash script to back up the data every 5 hours with cron and bash script. Thought I will share that with you. Here is the script.

#!/bin/bash - 
#===============================================================================
#
#          FILE:  backup.sh
# 
#         USAGE:  ./backup.sh 
# 
#   DESCRIPTION:  Backup all the current files.
# 
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR: Amit Agarwal (aka), amit.agarwal@roamware.com
#       COMPANY: Roamware India Pvt Ltd
#       CREATED: 08/19/2011 02:43:32 PM IST
#      REVISION:  ---
#===============================================================================
backupf=( a b c d) #Here you need to specify the directory you need to backup.
bkupdir="/tmp/backup/$(date +%Y)/$(date +%m)/$(date +%d)" #Here you can change the basedir for the backups
mkdir  -p $bkupdir
sudo tar cvfz $bkupdir/bkup_$(date "+%Y%m%d_%H%M").tgz $backupf[*] #Remove sudo if you do not need it.
echo "Backup created in dir $dir"

And then just add it to cron as “00 */5 * * * /home//bin/backup.sh”. Change the path if it does not match what is given here.

Enhanced by Zemanta

conky script used to monitor server status remotely.

A typical Conky look
Image via Wikipedia

I was looking for something to monitor few details on the server. I thought about quite a lot of applications, some open source and some scripts developed in house. But my requirements were quite petty and the scripts and applications were quite heavy. So, I thought why not conky :).

 

So, with some quick work on conky script, I was able to get what I wanted and here it is for all of you.

This is a bash script, that will create the conky script and then start conky.

#!/bin/bash - 
#===============================================================================
#
#          FILE:  conky.sh
# 
#         USAGE:  ./conky.sh 
# 
#   DESCRIPTION:  Start conky to display who on all the servers.
# 
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR: Amit Agarwal (aka), amit.agarwal@roamware.com
#       COMPANY: Roamware India Pvt Ltd
#       CREATED: 12/21/2011 02:06:33 PM IST
# Last modified: Wed Dec 21, 2011  05:56PM
#      REVISION:  ---
#===============================================================================
echo killall conky processes
kill -9 $(ps -eaf|grep conkyrc|awk '{print $2}'|tr '\n' ' ')
conkyrc=~/.conkyrc.who
cat <$conkyrc
background no
alignment top_right
update_interval 50
double_buffer
short_units yes

#colors
#color black
#color0 ff6633
#color1 ffffff
#color2 cccccc
#color3 ffff66

#xft settings
use_xft yes
xftfont arial:size=10

cpu_avg_samples 2
net_avg_samples 2
#window settings
own_window yes
own_window_transparent no
own_window_colour black
#own_window_type
own_window_hints undecorated, sticky, skip_taskbar, below

#spacing
gap_x 0
gap_y 0
alignment top_left
#colors
color0 ffffff
color1 00FF97
color2 FF6D00
color3 FE003C
color4 FD9C00
color5 FDF700
color6 9101FE
color7 A2FF00
color8 FE003C
color9 000000

TEXT
EOF
count=1
for i in 1.1 1.2  1.3
do

    line1="$line1\${color$count}172.16.$i      "
    line2="$line2 \${color$count}Count : .....\${execi 50 ssh user@172.16.$i \"who\"|wc -l}.. "
    line3="$line3 \${color$count}..\${execi 50 ssh user@172.16.$i \"who\"|sed \"s/[    ].*//\" |sort |uniq -c|sort -nr|head -1|tr '\t' ' '|sed 's/ \\{2,\\}/ /g'}"
    line4="$line4 \${color$count}Processes:\${execi 50 ssh user@172.16.$i ps -fu user|wc -l}"
    ((count++))
done 
echo $line1 >>$conkyrc
echo $line2 >> $conkyrc
echo $line3 >> $conkyrc
echo $line4 >> $conkyrc
conky -c $conkyrc  &
Enhanced by Zemanta

Add ssh key to remote host

Example of tunnelling an X11 application over SSH
Image via Wikipedia

If you are working on recent versions of the *nix OS like Fedora or Ubuntu then you would know about the commad ssh-copy-id. But if you land up using one of the older versions like Solaris or something where the command is not present, then probably you need a simpler solution to this. One of the simplest solution is with a lot of assumtions, simply copy the id_rsa file to remote server and hope it works. And here isĀ  a script to do just that:

#!/bin/bash -
#===============================================================================
#
#          FILE:  add_ssh_key.sh
#
#         USAGE:  ./add_ssh_key.sh
#
#   DESCRIPTION:  Add the ssh key
#
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR: Amit Agarwal (aka), amit.agarwal@roamware.com
#       COMPANY: Roamware India Pvt Ltd
#       CREATED: 09/19/2011 11:02:08 AM IST
# Last modified: Mon Sep 19, 2011  11:02AM
#      REVISION:  ---
#===============================================================================
ip=$2
un=$1
IFS=$"\n' key=$(cat .ssh/id_rsa.pub)
ssh $un@$ip "echo $key >> ~/.ssh/authorized_keys"

or the other option is to copy the ssh-copy-id script to the server in question. If you don’t have the script handy, I am pasting it here:

#!/bin/sh

# Shell script to install your public key on a remote machine
# Takes the remote machine name as an argument.
# Obviously, the remote machine must accept password authentication,
# or one of the other keys in your ssh-agent, for this to work.

ID_FILE="${HOME}/.ssh/id_rsa.pub"

if [ "-i" = "$1" ]; then
  shift
  # check if we have 2 parameters left, if so the first is the new ID file
  if [ -n "$2" ]; then
    if expr "$1" : ".*\.pub" > /dev/null ; then
      ID_FILE="$1"
    else
      ID_FILE="$1.pub"
    fi
    shift         # and this should leave $1 as the target name
  fi
else
  if [ x$SSH_AUTH_SOCK != x ] && ssh-add -L >/dev/null 2>&1; then
    GET_ID="$GET_ID ssh-add -L"
  fi
fi

if [ -z "`eval $GET_ID`" ] && [ -r "${ID_FILE}" ] ; then
  GET_ID="cat ${ID_FILE}"
fi

if [ -z "`eval $GET_ID`" ]; then
  echo "$0: ERROR: No identities found" >&2
  exit 1
fi

if [ "$#" -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
  echo "Usage: $0 [-i [identity_file]] [user@]machine" >&2
  exit 1
fi

# strip any trailing colon
host=`echo $1 | sed 's/:$//'`

{ eval "$GET_ID" ; } | ssh $host "umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys" || exit 1

cat <<EOF
Now try logging into the machine, with "ssh '$host'", and check in:

  ~/.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

EOF
Enhanced by Zemanta