Tuesday, May 1, 2012

user add script in linux, how to add user from shell script


#!/bin/bash
# Script to add a user to Linux system
if [ $(id -u) -eq 0 ]; then
read -p "Enter username : " username
read -s -p "Enter password : " password
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
fi
else
echo "Only root may add a user to the system"
exit 2
fi

mysql backup, web-data backup shell script

#!/bin/bash
# creates a backup of the mysql & webdata for a specific website.
TODAY=`date +%A`
# these variables cannot contain any spaces and must be modified based on:
#       the name of the /automation/folder/ of the application being backed up (application)
#       on what server (localhost)
#       where the backed up data should be replicated (remotehost)
#       the location of the www data for the specific application (webdata)
#               /var/www/somefolder
#       the username, password and database required  to establish the mysql connection (sqluser, sqlpass, sqldb)
#               SQLPASS must contain the -p prefix
#               example: -pS0m3PassW0rd
APPLICATION=someapp
LOCALHOST=thisserver
REMOTEHOST=otherserver
WEBDATA=location
SQLUSER=usernam
SQLPASS=-ppassword
SQLDB=dbname
# these variables are specific  to the application being backed up.
ARCHIVE=/automation/$APPLICATION/$LOCALHOST
ERRORLOG=/automation/$APPLICATION/errorlog.txt
SQLFILE=/automation/$APPLICATION/$APPLICATION.sql
WEBFILE=/automation/$APPLICATION/$APPLICATION.web.tar
SQLGZ=/automation/$APPLICATION/$APPLICATION.sql.gz
WEBGZ=/automation/$APPLICATION/$APPLICATION.web.tar.gz
SQLA=/automation/$APPLICATION/$LOCALHOST/$APPLICATION.sql.gz
WEBA=/automation/$APPLICATION/$LOCALHOST/$APPLICATION.web.tar.gz
rm $SQLFILE
rm $WEBFILE
rm $SQLGZ
rm $WEBGZ
echo "Backup started: " `date` >> $ERRORLOG
mysqldump -u $SQLUSER $SQLPASS $SQLDB > $SQLFILE
tar -cf $WEBFILE  $WEBDATA
if [ -s $SQLFILE  ];
then
    if [ -s $WEBFILE  ];
    then
       gzip $SQLFILE
       gzip $WEBFILE
    else
       echo $SQLFILE  was created, $WEBFILE  was not,  script terminating. >> $ERRORLOG
       exit
    fi
else
    echo $SQLFILE  was not created,  script terminating. >> $ERRORLOG
    exit
fi
if [ -s $SQLGZ ];
then
    if [ -s $WEBGZ ];
    then
       rm $ARCHIVE/*.$TODAY
       mv $SQLGZ $SQLA.$TODAY
       mv $WEBGZ $WEBA.$TODAY
    else
       echo $SQLGZ was created, $WEBGZ was not,  script terminating. >> $ERRORLOG
       exit
    fi
else
    echo $SQLGZ was not created,  script terminating. >> $ERRORLOG
    exit
fi
scp $SQLA.$TODAY username@$REMOTEHOST.domain.com:/automation/$APPLICATION/$LOCALHOST
scp $WEBA.$TODAY edcns5al@$REMOTEHOST.domain.com:/automation/$APPLICATION/$LOCALHOST
chown -R root:automation /automation/
chmod -R 770 /automation/
echo Backup completed: `date`  >> $ERRORLOG

Your Reviews/Queries Are Accepted