Bash backup script from config file - bash

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

Related

Dynamically encrypting configuration variables and placing them in specific folders

I have a configuration file that contains a list of string variables that the user is required to change to suit their environment:
Configuration file example:
# first_file.yml
value_one: <UPDATE>
value_two: <UPDATE>
# second_file.yml
value_one: <UPDATE>
value_two: <UPDATE>
Once the user has changed the UPDATE value, I want to be able to use vault to encrypt each variable before copying the encrypted variable to a file specified in the comment, with the desired out below:
# first_file.yml
value_one: !vault |
$ANSIBLE_VAULT;1.1;AES256
30663734346135353432323531336536636566643739656332613031636630383237666636366630
6164633835363766666535656438306534343565636434330a626239396536373032373866353861
37376665313438363561323262393337313266613237313065396338376438313737393234303434
3035326633616339340a346164646366623932313261613662633938356662373438643831643830
3432
value_two: !vault |
$ANSIBLE_VAULT;1.1;AES256...
I am unsure how to best approach this problem, with the main challenge being how to:
Encrypt each variable successfully, without encrypting the entire file
Copy the encrypted variable over to a specified file
I just threw this together, but it works for your case, preserving structure and indents:
#!/bin/bash
IFS=; while read line; do
# read key and value from line
key=$( echo "${line}" | cut -d: -f1 )
value=$( echo "${line}" | cut -d: -f2 | tr -d '\n' )
# Get spaces to indent
indent=$( echo "${key}" | grep -o '^ *' )
# if value is not empty...
if [ -n "$value" ]; then
# Encrypt value and indent
cval=$( echo -n "${value## }" | sed -e "s/^'//" -e "s/'$//" | ansible-vault encrypt_string --vault-password-file ~/.ssh/vault_key.txt | sed "s/^ / ${indent}/")
fi
# if key is not empty...
if [ -n "$key" ]; then
echo -n "${key}: ${cval}"
fi
# End the line
echo
# unset cval
unset cval
done < /dev/stdin
Name it encrypt_values.sh, run chmod +x encrypt_values.sh, then you can run it with
cat {input-file} | ./encrypt_values.sh > {output_file}
If you have some bizzare structure, run the file through yq first to clean it up:
yq r {imput-file} | ./encrypt_values.sh > {output_file}

Bash script any reason why it wont write the file

I am helping debug some code that exec the following script is there any reason why its not writing a file to the server? - if that what it does. All the $ data and permissions are ok:
Script:
#!/bin/bash
RANGE=$1
ALLOCATION=`echo $RANGE | cut -f1,2,3 -d'.'`
/sbin/ip rule add from $1 lookup $2
echo $ALLOCATION
rm /path/too/file/location/$ALLOCATION
for i in `seq 3 254`
do
echo $ALLOCATION.$i >> /path/too/file/location/$ALLOCATION
done
ETH=`/sbin/ifconfig | grep eth0 | tail -n1 | cut -f2 -d':' | cut -f1 -d' '`

bash scripting for mysqldump

i use the following code to download all mysql database in a different file and not in one file (like --all-databases) and put them in the /backup/mysql folder
#!/bin/bash
mysqldump=`which mysqldump`
echo $mysqldump
mkdir /backup/mysql/$(date '+%d-%b-%Y')
echo "creating folder for current date done"
for line in "$(mysqlshow |cut -f1 -d"-" | cut -c 3- | cut -f1 -d" ")"
do
$mysqldump $line > /backup/mysql/$(date '+%d-%b-%Y')/"$line"
echo "$line\n"
done
I used the cut pipes to remove dashes and empty space before and at the end of the database name and it gave me what I want.
The problem is at line 13 according to bash but with no more details. Any ideas what I'm doing wrong?
mysqlshow output format
+---------------------+
| Databases |
+---------------------+
| information_schema |
| gitlabhq_production |
| mysql |
| performance_schema |
| phpmyadmin |
| test |
+---------------------
Your script doesn't manage the first 3 lines nor the last one, so you $line variable is invalid.
Solution
mysql | tail -n +4 | head -n -1 | tr -d '| '
tail -n +4: skip first four lines (may need adjustement);
head -n -1: ignore last line ;
tr -d '| ': remove pipe and space.
Advices
quotes your variables ;
use $() instead of backtick ;
don't use for i in $(ls *.mp3).
read How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?
Better solution
Instead of a for loop you should use a while with a Process Substitution:
while read -r db; do
echo "[$db]";
done < <(mysqlshow -u root -p | tail -n +3 | head -n -1 | tr -d ' |' )

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

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