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
Related
ITNOA
I want to write bash script, to remove some environmental variable
my problem is when I run below command
env | grep -i _proxy= | cut -d "=" -f1 | xargs -I {} echo {}
I see below result
HTTPS_PROXY
HTTP_PROXY
ALL_PROXY
but when I replace echo with unset, something like below
env | grep -i _proxy= | cut -d "=" -f1 | xargs -I {} unset {}
I see below error
xargs: unset: No such file or directory
What is my problem? If I use xargs incorrectly?
You have xargs running in a pipeline. Therefore it is running in a separate process, and it cannot alter the environment of the "parent" shell.
Also, xargs works with commands, not shell builtins.
You'll need to do this:
while read -r varname; do unset "$varname"; done < <(
env | grep -i _proxy= | cut -d "=" -f1
)
or
mapfile -t varnames < <(env | grep -i _proxy= | cut -d "=" -f1)
unset "${varnames[#]}"
I came across this:
]$ env |grep AWS_|tr "=" " "|awk '{print $1}'
AWS_SESSION_TOKEN
AWS_SECRET_ACCESS_KEY
AWS_ACCESS_KEY_ID
]$ env |grep AWS_|tr "=" " "|awk '{print $1}'|xargs -n1 unset
xargs: unset: No such file or directory
]$
And below simple solution worked for me
unset `env |grep AWS_|tr "=" " "|awk '{print $1}'`
or below
unset `env |grep AWS_|cut -d= -f1`
I'm trying to extract database names from a config file and iterate over each db name to backup it
The config file looks like this:
#
# Databases:
#
BASE_1 = /firebird/data/BASE_1.fdb
BASE_2 = /firebird/data/BASE_2.fdb
BASE_3 = /firebird/data/BASE_3.fdb
# .../...
And my script looks like this:
#!/bin/bash
db_conf='./scripts/databases.conf'
bk_dir='./backups'
dbs=$(grep -E -o "[A-Z_]+\.fdb" $db_conf | sort | uniq)
for db in $dbs
do
gbak -b "localhost:${db/.fdb/}" "$bk_dir/${db/.fdb/.fbk}" -user xxx -pass xxx
done
The script is working but I would like to know if there is a better way to do it
I finaly found this way, that is the best as I know:
#!/bin/bash
db_conf='/firebird/etc/databases.conf'
bk_dir='/firebird/backups'
data_dir='/firebird/data'
dbs=$(grep -Eo "$data_dir/[A-Z0-9_]+\.fdb" $db_conf | cut -d/ -f4 | cut -d. -f1 | sort | uniq)
for db in $dbs
do
if test -f "$data_dir/${db}.fdb"; then
echo "backup ${db} to $bk_dir/${db}.fbk"
gbak -b "${db}" "$bk_dir/${db}.fbk"
else
echo "${db}.fdb not found"
fi
done
I have the below shell script:
du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
while IFS= read -r line;do
DIR=`echo $line | awk '{print$2}'`
Rep=`cat $DIR/repository-config |grep 'project\|repo' | tr '\n' ' '`
Size=`echo $line | awk '{print $1}' `
echo $Size $Rep
done
How can I run it thought Execute shell in Jenkins? I need also to add ssh command to the env (no need for a password).
Note I don't want to connect to the env and run this shell, but directly from Excecue shell box
If I'm not wrong your are using a Freestyle job and not a pipeline job.
Anyway, I think you have to try the following :
ssh -t XXXXX#YYYYY << 'EOF'
du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
while IFS= read -r line;do\
DIR=echo $line | awk '{print$2}'\
Rep=cat $DIR/repository-config |grep 'project\|repo' | tr '\n' ' '\
Size=echo $line | awk '{print $1}' \
echo $Size $Rep\
done
EOF
I've escaped the code inside your while loop using \, if it's doesn't works you can use ; instead.
If you want help for using a pipeline job, let me know but i might be a bit more complex.
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
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