Test Code

ubuntu@ubuntu:~$ sudo mount /dev/sda1 /media/
ubuntu@ubuntu:~$ sudo mount --bind /dev /media/dev/
ubuntu@ubuntu:~$ sudo mount --bind /proc /media/proc/
ttirtawi@tedy-laptop:~$ su -
Password:
root@tedy-laptop:~#
root@tedy-laptop:~# tar cvpzf backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --exclude=/media --exclude=/home --exclude=/sys /

#!/bin/ksh
########################################################################
#Copy (sFTP) the gatherConfig.sh SUPPORT_AUDIT files to a central server
#Requires ssh permissions to be set up on remote hosts
#
########################################################################

# Define sFTP user
sFTP_USER=smf_oper

# check sFTP_USER is running script
ID=`id |sed '/uid/!d; s/) gid=.*//;s/uid=.*(//'`
if [ "$ID" != smf_oper ]; then
 echo "Error: Wrong user [ $ID ]"
 echo "       You must be user smf_oper to run $0\n"
 exit
fi

########################################################################
# start and stop messages
MYPID=$$
SNAME=${0##*/}
NAME=${SNAME%%\.*}
echo "##$NAME [ $MYPID ] START $(date)"
trap 'echo "\n##$NAME [ $MYPID ] END $(date)"' EXIT
trap 'echo "##Interrupted!"; exit 0' 1 2 3 15

########################################################################
# Varaibles
# Copy files form here
GET_DIR=/IN/service_packages/SUPPORT/audit
# Use extended regular expressions (man regex) to filter the file or
# files to copy from GET_DIR
FILE_FILTER=".*\.tar\.gz$"

# Copy the files here
PUT_DIR=/IN/service_packages/SUPPORT/audit-prod
[ -d "$PUT_DIR" ] || mkdir -p "$PUT_DIR"
cd $PUT_DIR

########################################################################
# Define the hosts machines (ip address or hostname)
# empty lines and commented lines will be ignored
REMOTE_HOSTS=`cat <<EOF |sed '/^$/d;/^#/d'
 10.21.80.108
EOF
`

#echo $REMOTE_HOSTS
[ -z "$REMOTE_HOSTS" ] && echo "No remote hosts specified!" && exit 0

########################################################################
# secure FTP files from remote hosts
# do not quote REMOTE_HOSTS variable so any leading whitespace is stripped
for HOST in $REMOTE_HOSTS
do
 echo "\n++ $HOST"

 # check for *tar.gz files and copy if exist
 for FILE in `ssh "${sFTP_USER}@${HOST}" "[ -d "${GET_DIR}" ] && (ls ${GET_DIR} |egrep "${FILE_FILTER}" 2>/dev/null || echo NONE)"`
 do
 #echo ==$FILE==
 # If no *tar.gz fule found then print message
 [ "${FILE}" =  NONE ] && echo "No files found!" && break
 FILE="${GET_DIR}/$FILE"

 # do sFTP
 sftp ${sFTP_USER}@${HOST} <<eof
 !echo "get ${FILE}"
 get ${FILE}
 quit
eof

 # check sFTP status
 if [ "$?" = 0 ]; then
 echo "\nFile copied to $PUT_DIR"
 else
 echo "Copy failed!"
 fi

 done

done
ttirtawi@tedy-laptop:~$ su -
Password:
root@tedy-laptop:~#
root@tedy-laptop:~# tar cvpzf backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --exclude=/media --exclude=/home --exclude=/sys /

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s