How do I say the shell script to run within a given time-frame? - bash

I am using the following shell script to fetch data from the database,and
in these fetched data I have a field named 'frequency', then I need to
set the script to run according to this frequency.Can I handle this
functionality in the script so that it will tell the crone job to run it
frequently?
#! /bin/bash
######## Settings ########
BASE_DIR_PATH='/var/www/html/aer/'
######## End Settings ########
CUS_PHP_FILE_PATH='shd/save_customer_info.php'
EMAIL_PHP_FILE_PATH='shd/email_customer_report.php'
CUSTOMER_REPORT_STORAGE_DIRECTORY='ev/customer_report/'
COMPLETED_CUSTOMER_REPORT_STORAGE_DIRECTORY='ev/rty'
LOG_DIRECTORY='ev/customer_report/logs'
DB_PHP_FILE='db/db_config.php'
#Obtain FTP server credentials from database
DB_FILE_NAME="$BASE_DIR_PATH$DB_PHP_FILE"
DB_NAME=`cat $DB_FILE_NAME | grep DB_DATABASE | cut -d \' -f 4`
DB_USERNAME=`cat $DB_FILE_NAME | grep DB_SERVER_USERNAME | cut -d \' -f
4`
DB_PASSWORD=`cat $DB_FILE_NAME | grep DB_SERVER_PASSWORD | cut -d \' -f
4`
FTP_PROFILE='bgty'
SYSTEM_PACKAGE='bfgtt'
results=($(mysql --user $DB_USERNAME -p${DB_PASSWORD} ${DB_NAME} -Bse
"SELECT api_url,api_user_name,api_password,frequency FROM
brt_profile WHERE profile_name='$FTP_PROFILE' AND
distributor='$SYSTEM_PACKAGE'"))
if [ $? -ne 0 ]; #check if database connection is failed
then
exit
fi

Related

if statement throws error as end of file

i have a script that looks something like
#!/bin/bash$
#x=new value$
#y=old value$
$
export PATH=/xxx/xxx/xxx:$PATH$
$
#get the difference of two files$
diff --side-by-side --suppress-common-lines file.txt file1.txt | tr -d "|,<,>,'\t'" | sed 's/ /:/g' | sed 's/^://' > diff.txt$
cat diff.txt$
$
#get the values$
for i in `cat diff.txt`; do$
plug_x=`echo $i | cut -d ":" -f1`$
echo "the value of jenkins plugin is $plug_x"$
ver_x=`echo $i | cut -d ":" -f2`$
echo "the value of jenkins version is $ver_x"$
plug_y=`echo $i | cut -d ":" -f3`$
echo "the value of db plugin is $plug_y"$
ver_y=`echo $i | cut -d ":" -f4`$
echo "the value of db version is $ver_y"$
if [ -z "$ver_y" ] && [ -z "$ver_x" ] ;$
then $
echo "the plugin is newly added"$
#newly added plugin should be updated in the db$
# mysql -u root -ppassword -h server --local-infile db << EOFMYSQL$
#update the table with the new version$
#EOFMYSQL$
else$
echo "the plugin has changes"$
mysql -u root -ppassword -h server --local-infile db << EOFMYSQL$
insert into table (xxx, xxx) values('$ver_x','$plug_x');$
$
EOFMYSQL $
fi$
done$
but when i run this script it saya
Syntax error: end of file unexpected (expecting "fi")
but the fi is there..i cant figure out why it is throwing the error
this error does not come when i just have echo statements in the script
I would suggest that if you are just running that one query to insert into table, then echo the query and pipe it to the mysql command, that would be (in my opinion) a better and efficient choice.
#!/bin/bash
diff --side-by-side --supress-common-lines test1.txt test2.txt | tr -d "|,<,>,'\t'" | sed 's/ /:/g' | sed 's/^://' > ./diff.txt
for i in `cat ./diff.txt`; do
plug_x=`echo $i | cut -d ":" -f1`
echo "the value of jenkins plugin is $plug_x"
ver_x=`echo $i | cut -d ":" -f2`
echo "the value of jenkins version is $ver_x"
plug_y=`echo $i | cut -d ":" -f3`
echo "the value of db plugin is $plug_y"
ver_y=`echo $i | cut -d ":" -f4`
echo "the value of db version is $ver_y"
if [ -z "$ver_y" ] && [ -z "$ver_x" ]
then
echo "the plugin is newly added"
else
echo "the plugin has changes"
echo "insert into table (xxx, xxx) values('$ver_x','$plug_x')" | mysql -u root -ppassword -h server --local-infile db
fi
done
That should do the job or if you'd like to use While loop, you can certainly do so.
#!/bin/bash
diff --side-by-side --supress-common-lines test1.txt test2.txt | tr -d "|,<,>,'\t'" | sed 's/ /:/g' | sed 's/^://' > ./diff.txt
cat ./diff.txt | while read i
do
plug_x=`echo $i | cut -d ":" -f1`
echo "the value of jenkins plugin is $plug_x"
ver_x=`echo $i | cut -d ":" -f2`
echo "the value of jenkins version is $ver_x"
plug_y=`echo $i | cut -d ":" -f3`
echo "the value of db plugin is $plug_y"
ver_y=`echo $i | cut -d ":" -f4`
echo "the value of db version is $ver_y"
if [ -z "$ver_y" ] && [ -z "$ver_x" ]
then
echo "the plugin is newly added"
else
echo "the plugin has changes"
echo "insert into table (xxx, xxx) values('$ver_x','$plug_x')" | mysql -u root -ppassword -h server --local-infile db
fi
done

OpenLDAP: get `directory` from cn=config

How can I get directory for specified DN by one ldapsearch request?
I mean - I have few databases. OpenLDAP configured with cn=config. For each DN - it hve own ldif-file, where it's olcDbDirectory specified.
Can I obtain olcDbDirectory value for each DN?
For backup script - I need to set varibale which contains directory, and this variable changes every time for every DN, wich backuped/restored at this moment.
So - in bash I just found solution to create function like:
#!/bin/bash
getDir () {
file=`grep -R "$1" /etc/openldap/slapd.d/ | cut -d":" -f 1 | tail -n 1`
echo $file
dir=`cat $file | grep "olcDbDirectory" | awk '{print $2}'`
echo $dir
}
getDir testdb;
$ ./dn.sh
/etc/openldap/slapd.d/cn=config/olcDatabase={9}bdb.ldif
/var/lib/ldap/testdb
But this solution seems not tidy... And I'd preffered to use something like:
getDir () {
dir=`ldapsearch -x -D "cn-root,cn=config" "*somefilter*"
}
Here it is:
$ ldapsearch -x -LLL -D 'cn=root,cn=config' -w PassWord -b 'cn=config' '(&(olcDbDirectory=*)(olcSuffix='testdb'))' olcDbDirectory | grep "olcDbDirectory" | cut -d":" -f 2
/var/lib/ldap/testdb
Of in bash function:
#!/bin/bash
getDir () {
dirtodel=`ldapsearch -x -LLL -D 'cn=root,cn=config' -w PassWord -b 'cn=config' '(&(olcDbDirectory=*)(olcSuffix='${1}'))' olcDbDirectory | grep "olcDbDirectory" | cut -d":" -f 2`
echo $dirtodel
}
getDir 'dc=testdb'
Result:
$ ./dn.sh
/var/lib/ldap/testdb

Bash script help/evaluation

I'm trying to learn some scripting however I can't find solution for one functionality.
Basically I would like to ask to evaluate my script as it's probably possible to reduce the complexity and number of lines.
The purpose of this script is to download random, encrypted MySQL backups from Amazon S3, restore the dump and run some random MySQL queries.
I'm not sure how to email the output from printf statements - one is for headers and second one for actual data. I've tried to format the output so it looks like below but I had to exclude the headers from the loop:
Database: Table: Entries:
database1 random_table 0
database2 random_table 0
database3 random_table 0
database4 random_table 0
I would like to include this output in the email and also change the email subject based on the success/failure of the script.
I probably use to much if loops and MySQL queries are probably to complicated.
Script:
#!/usr/bin/env bash
# DB Details:
db_user="user"
db_pass="password"
db_host="localhost"
# Date
date_stamp=$(date +%d%m%Y)
# Initial Setup
data_dir="/tmp/backup"
# Checks
if [ ! -e /usr/bin/s3cmd ]; then
echo "Required package (http://s3tools.org/s3cmd)"
exit 2
fi
if [ -e /usr/bin/gpg ]; then
gpg_key=$(gpg -K | tr -d "{<,>}" | awk '/an#example.com/ { print $4 }')
if [ "$gpg_key" != "an#example.com" ]; then
echo "No GPG key"
exit 2
fi
else
echo "No GPG package"
exit 2
fi
if [ -d $data_dir ]; then
rm -rf $data_dir/* && chmod 700 $data_dir
else
mkdir $data_dir && chmod 700 $data_dir
fi
# S3 buckets
bucket_1=s3://test/
# Download backup
for backup in $(s3cmd ls s3://test/ | awk '{ print $2 }')
do
latest=$(s3cmd ls $backup | awk '{ print $2 }' | sed -n '$p')
random=$(s3cmd ls $latest | shuf | awk '{ print $4 }' | sed -n '1p')
s3cmd get $random $data_dir >/dev/null 2>&1
done
# Decrypting Files
for file in $(ls -A $data_dir)
do
filename=$(echo $file | sed 's/\.e//')
gpg --out $data_dir/$filename --decrypt $data_dir/$file >/dev/null 2>&1 && rm -f $data_dir/$file
if [ $? -eq 0 ]; then
# Decompressing Files
bzip2 -d $data_dir/$filename
if [ $? -ne 0 ]; then
echo "Decompression Failed!"
fi
else
echo "Decryption Failed!"
exit 2
fi
done
# MySQL Restore
printf "%-40s%-30s%-30s\n\n" Database: Table: Entries:
for dump in $(ls -A $data_dir)
do
mysql -h $db_host -u $db_user -p$db_pass < $data_dir/$dump
if [ $? -eq 0 ]; then
# Random DBs query
db=$(echo $dump | sed 's/\.sql//')
random_table=$(mysql -h $db_host -u $db_user -p$db_pass $db -e "SHOW TABLES" | grep -v 'Tables' | shuf | sed -n '1p')
db_entries=$(mysql -h $db_host -u $db_user -p$db_pass $db -e "SELECT * FROM $random_table" | grep -v 'id' | wc -l)
printf "%-40s%-30s%-30s\n" $db $random_table $db_entries
mysql -h $db_host -u $db_user -p$db_pass -e "DROP DATABASE $db"
else
echo "The system was unable to restore backups!"
rm -rf $data_dir
exit 2
fi
done
#Remove backups
rm -rf $data_dir
move out of the loop :
random_tables=$(mysql -h $db_host -u $db_user -p$db_pass $db -e "SHOW TABLES" | grep -v 'Tables')
table_nb=$(wc -l <<<"$random_tables")
and in the loop
random_table=$(sed -n $((RANDOM%table_nb+1))p <<<"$random_tables")
A remark $? is the status of latest command executed so after && rm it will not be the status of decrypt

bash script pulling variables from .txt, keeps giving syntax error while trying to use mount command

Ive been trying to get this to work for the last week and cannot figure out why this is not working. I get mixed results typing directly into the terminal, but keep getting syntax error messages when running from the .sh. using ubuntu 11.10
It looks like part of the mount command gets pushed to the next line not allowing it to complete properly.. I have no idea why this is happening or how to prevent it from going to the second line.
i have several lines defined as follows in mounts.txt, that gets read from mount-drives.sh below
I have called it to run using sudo so it shouldnt be a permissions issue.
Thanks for taking a look, let me know if additional info is needed.
mounts.txt
mountname,//server/share$,username,password,
mount-drives.sh ---origional, updated below
#!/bin/bash
while read LINE;
do
# split lines up using , to separate variables
name=$(echo $LINE | cut -d ',' -f 1)
path=$(echo $LINE | cut -d ',' -f 2)
user=$(echo $LINE | cut -d ',' -f 3)
pass=$(echo $LINE | cut -d ',' -f 4)
echo $name
echo $path
echo $user
echo $pass
location="/mnt/test/$name/"
if [ ! -d $location ]
then
mkdir $location
fi
otherstuff="-o rw,uid=1000,gid=1000,file_mode=0777,dir_mode=0777,username=$user,password=$pass"
mount -t cifs $otherstuff $path $location
done < "/path/to/mounts.txt";
mount-drives.sh ---updated
#!/bin/bash
while read LINE
do
name=$(echo $LINE | cut -d ',' -f 1)
path=$(echo $LINE | cut -d ',' -f 2)
user=$(echo $LINE | cut -d ',' -f 3)
pass=$(echo $LINE | cut -d ',' -f 4)
empty=$(echo $LINE | cut -d ',' -f 5)
location="/mount/test/$name/"
if [ ! -d $location ]
then
mkdir $location
fi
mounting="mount -t cifs $path $location -o username=$user,password=$pass,rw,uid=1000,gid=1000,file_mode=0777,dir_mode=0777"
$mounting
echo $mounting >> test.txt
done < "/var/www/MediaCenter/mounts.txt"
Stab in the dark (after reading the comments). The "$pass" is picking up a newline because the mounts.txt was created in windows and has windows line endings. Try changing the echo $pass line to:
echo ---${pass}---
and see if it all shows up correctly.
There's a lot here that could stand improvement. Consider the following -- far more compact, far more correct -- approach:
while IFS=, read -u 3 -r name path user pass empty _; do
mkdir -p "$location"
cmd=( mount \
-t cifs \
-o "rw,uid=1000,gid=1000,file_mode=0777,dir_mode=0777,username=$user,password=$pass" \
"$path" "$location" \
)
printf -v cmd_str '%q ' "${cmd[#]}" # generate a string corresponding with the command
echo "$cmd_str" >>test.txt # append that string to our output file
"${cmd[#]}" # run the command in the array
done 3<mounts.txt
Unlike the original, this will work correctly even if your path or location values contain whitespace.

read variables from wp-config.php

I'm busy writing a basic migration scripts for some WP sites, and i'm trying to automate creating mysql database and user credentials from reading the wp-config file
how can i read the following variables from the wp-config file so that i effectively end up with 3 variables within a bash script:
/** The name of the database for WordPress */
define('DB_NAME', 'somedbname');
/** MySQL database username */
define('DB_USER', 'someusername');
/** MySQL database password */
define('DB_PASSWORD', 'somerandompassword');
eg my output should effectively give me:
WPDBNAME=somedbname
WPDBUSER=somedbuser
WPDBPASS=somerandompassword
Try this:
WPDBNAME=`cat wp-config.php | grep DB_NAME | cut -d \' -f 4`
WPDBUSER=`cat wp-config.php | grep DB_USER | cut -d \' -f 4`
WPDBPASS=`cat wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`
If you want to use wp-config details to connect to mysql you can use;
mysql -u `cat wp-config.php | grep DB_USER | cut -d \' -f 4` -p`cat wp-config.php | grep DB_PASSWORD | cut -d \' -f 4` -h `cat wp-config.php | grep DB_HOST | cut -d \' -f 4` `cat wp-config.php | grep DB_NAME | cut -d \' -f 4`
you can use awk:
awk -F"[()']" '/^define/{printf "%s=\"%s\"\n", $3, $5;}' < foo.php
This will give you:
DB_NAME="somedbname"
DB_USER="someusername"
DB_PASSWORD="somerandompassword"
Note, that this solution will work with variables containing spaces.
find . -name "wp-config.php" -print0 | xargs -0 -r grep -e "DB_NAME" -e "DB_USER" -e "DB_PASSWORD"
If you are trying to dump the MySQL database, you can also use wp-cli db export function:
wp db export --path=PATHTOYOURWP
Here is an example of the universal way to pass php variables to bash without the need to parse:
#!/bin/bash
source <(php -r 'require("wp-config.php"); echo("DB_NAME=".DB_NAME."; DB_USER=".DB_USER."; DB_PASSWORD=".DB_PASSWORD); ')
mysqldump --user $DB_USER --password="$DB_PASSWORD" $DB_NAME | gzip > $DB_NAME-$(date +%Y%m%d%H%M%S).sql.gz
Algorithm explanation in a few words:
run your php
print variables as var=value
source output
use variables in bash

Resources