# Declaration of Variables



## Sarouel (Nov 25, 2011)

Hi All,

So, I chose my host (Pair Networks) because it runs FreeBSD. However, I am a total beginner, so you can throw stones at me, because my question is stupid, but this problem keeps me from moving forward.

In fact, my script works with a very convoluted code, but I want to simplify it.

Here is my problem: I need to backup,  zip and encrypt my databases to finally send them to a mailbox.

Here is the bash script. In fact, the variable declaration does not work. Thank you for your help.


```
#!/bin/sh

# Pair Database Server URL:

SERVERDB='dbXYZ.pair.com'

# Database User Name:

USERDB='ABC_20'

# Database Password:

PASSDB='X1Y2Z3'

# Database Name:

DBNAME='POL_40'

#####

# Path to Backup directory:

PATH='/usr/home/USERNAME/public_html/Backup'

# Password to encrypt Database Before Sending it by email (Do NOT use semi-colon ";"):

OPENPASS='%4957I@6IcrV'

# Email address where send to the Backuped, Zipped and Encrypted Database:

EMAIL='me@mail.com'

# Email Subject (date and time is added at the end of this subject):

SUBJECT='POL_40 Backup'

# Delete the Zipped Backups Databases Older than n Day:

DAYS=1

##############################
#                            #
#  NO Change Under This Box  #
#                            #
##############################

DATE=`date +\%Y-\%m-\%d-at-\%H:\%M:\%S`

/usr/local/bin/mysqldump -h$SERVERDB -u$USERDB -p$PASSDB $DBNAME | gzip -9 > $PATH/$DBNAME-$DATE.sql.gz
 
openssl enc -aes-256-cbc -salt -a -in $PATH/$DBNAME-$DATE.sql.gz -out $PATH/$DBNAME-$DATE.sql.gz.ENC -pass pass:$PASS

/usr/local/bin/mutt -s "$SUBJECT $DATE" -a $PATH/$DBNAME-$DATE.sql.gz.ENC -- $EMAIL > /dev/null

rm $PATH/$DBNAME-$DATE.sql.gz.ENC

find $PATH/$DBNAME-*.sql.gz -mtime +$DAYS -exec rm {} \;
```


----------



## J65nko (Nov 26, 2011)

From the command line/shell type:

```
$ echo $PATH
```
You then will understand why overwriting PATH in your script is not such a good idea


----------



## Sarouel (Dec 7, 2011)

Hello J65nko,

Thank you, so I changed PATH with WAYTO.

However, it doesn't work. Here is the first alarm of email:


```
unknown option '+\%Y-\%m-\%d-at-\%H:\%M:\%S.sql.gz'
```

So, I changed those variables (Old > New):

DATE > CDATE
EMAIL > FEMAIL
DAYS > TDAYS

And I moved "CDATE" from line 47 to line 2, below "#!/bin/sh"

*****

Here is the new script:


```
#!/bin/sh

CDATE=`date +\%Y-\%m-\%d-at-\%H:\%M:\%S`

# Pair Database Server URL:

SERVERDB='dbXYZ.pair.com'

# Database User Name:

USERDB='ABC_20'

# Database Password:

PASSDB='X1Y2Z3'

# Database Name:

DBNAME='POL_40'

#####

# Path to Backup directory:

WAYTO='/usr/home/USERNAME/public_html/Backup'

# Password to encrypt Database Before Sending it by email (Do NOT use semi-colon ";"):

OPENPASS='%4957I@6IcrV'

# Email address where send to the Backuped, Zipped and Encrypted Database:

FEMAIL='me@mail.com'

# Email Subject (date and time is added at the end of this subject):

SUBJECT='POL_40 Backup'

# Delete the Zipped Backups Databases Older than n Day:

TDAYS=1

##############################
#                            #
#  NO Change Under This Box  #
#                            #
##############################

/usr/local/bin/mysqldump -h$SERVERDB -u$USERDB -p$PASSDB $DBNAME | gzip -9 > $WAYTO/$DBNAME-$CDATE.sql.gz
 
openssl enc -aes-256-cbc -salt -a -in $WAYTO/$DBNAME-$CDATE.sql.gz -out $WAYTO/$DBNAME-$CDATE.sql.gz.ENC -pass pass:$PASS

/usr/local/bin/mutt -s "$SUBJECT $CDATE" -a $WAYTO/$DBNAME-$CDATE.sql.gz.ENC -- $FEMAIL > /dev/null

rm $WAYTO/$DBNAME-$CDATE.sql.gz.ENC

find $WAYTO/$DBNAME-*.sql.gz -mtime +$DAYS -exec rm {} \;
```

Here are alarms:


```
dbXYZ.pair.com: not found
ABC_20: not found
X1Y2Z3: not found
POL_40: not found

%4957I@6IcrV: not found
me@mail.com not found
POL_40: not found
1: not found
```

So, script only find the first variable.

Thank you for your help!


----------



## SirDice (Dec 7, 2011)

Use the correct quotes. I'm guessing you used backticks on all the other variables too.

There is a big difference between *`*, *'* and *"*


----------



## Sarouel (Dec 7, 2011)

SirDice said:
			
		

> Use the correct quotes. I'm guessing you used backticks on all the other variables too.
> 
> There is a big difference between *`*, *'* and *"*



Which one need I use?


----------



## SirDice (Dec 7, 2011)

Give a man a fish and he'll have food for a day, teach a man to fish and he'll have food for the rest of his life.

The Bourne Shell.


----------



## Sarouel (Dec 7, 2011)

SirDice said:
			
		

> Give a man a fish and he'll have food for a day, teach a man to fish and he'll have food for the rest of his life.
> 
> The Bourne Shell.



*Give a man a beer and he'll have drink for 5 minutes, give a man a brewery and he'll have drinks for the rest of his life.*

Saint Gerard Adriaan Heineken, founder of Heineken.

*****

Thank you to everyone.

Here is the perfectly final working code:


```
#!/bin/sh

# Pair Database Server URL:

SERVERDB='dbXYZ.pair.com'

# Database User Name:

USERDB='ABC_20'

# Database Password:

PASSDB='X1Y2Z3'

# Database Name:

DBNAME='POL_40'

#####

# Path to Backup directory:

WAYTO='/usr/home/USERNAME/public_html/Backup'

# Password to encrypt Database Before Sending it by email (Do NOT use semi-colon ";"):

OPENPASS='%4957I@6IcrV'

# Email address where send to the Backuped, Zipped and Encrypted Database:

EMAIL='me@mail.com'

# Email Subject (date and time is added at the end of this subject):

SUBJECT='POL_40 Backup'

# Delete the Zipped Backups Databases Older than n Day:

DAYS=1

##############################
#                            #
#  NO Change Under This Box  #
#                            #
##############################

DATE=`date +\%Y-\%m-\%d-at-\%H:\%M:\%S`

/usr/local/bin/mysqldump -h$SERVERDB -u$USERDB -p$PASSDB $DBNAME | gzip -9 > $WAYTO/$DBNAME-$DATE.sql.gz
 
openssl enc -aes-256-cbc -salt -a -in $WAYTO/$DBNAME-$DATE.sql.gz -out $WAYTO/$DBNAME-$DATE.sql.gz.ENC -pass pass:$PASS

/usr/local/bin/mutt -s "$SUBJECT $DATE" -a $WAYTO/$DBNAME-$DATE.sql.gz.ENC -- $EMAIL > /dev/null

rm $WAYTO/$DBNAME-$DATE.sql.gz.ENC

find $WAYTO/$DBNAME-*.sql.gz -mtime +$DAYS -exec rm {} \;
```


----------



## SirDice (Dec 8, 2011)

Sarouel said:
			
		

> *Give a man a beer and he'll have drink for 5 minutes, give a man a brewery and he'll have drinks for the rest of his life.*
> 
> Saint Gerard Adriaan Heineken, founder of Heineken.


Haha.. Nice twist :beer


----------



## Sarouel (Oct 14, 2012)

Hello,

Feed back after 10 months of using of this sh(1) script.

First of all, I want you to remember I am a total newby, so my new command lines added are there after many attempts.

Please, they are NOT perfect, so bring your comments to solve some errors.

*****

This command line:


```
[ -f $ATROOT/sent.lock ] && rm $ATROOT/sent.lock
```

Finds and delete the sent.lock file, if needed, AND fixes this error:


```
Couldn't lock /usr/home/lidynasty/sent
```

Nota Bene:

The current database will be saved on the server, BUT it will NOT be sent by email.

AND, no database will be sent by email as the file sent.lock will NOT be deleted firstly.

*****

This command line:


```
[ -f $ATROOT/sent ] && rm $ATROOT/sent
```

Finds and delete the sent file, if needed, AND solves the unstoppable growing size of the sent file.

Nota Bene:

If you DON'T delete the sent file, the size of this file will increase after each sending. The increasing is the exact size of the previously sent database.

So, if you have a lot of databases saved each hour, without deletion, the sent file could be as big as tens or thousands mega bits after one or two days only.

*****

This command line:


```
[ ! -f $ATROOT/sent ] && touch $ATROOT/sent
```

Finds and create the sent file, if needed, AND creates a new sent empty file with touch(1) command, AND fixes this error:


```
Couldn't find /usr/home/lidynasty/sent
```

*****

I included a useful reminder "step by step" on "how to decrypt and opening database on your Windows desktop computer" at the bottom of the sh script.


```
#!/bin/sh

CDATE=`date +\%Y-\%m-\%d-at-\%H:\%M:\%S`

# Pair Database Server URL:

SERVERDB='dbXYZ.pair.com'

# Database User Name:

USERDB='ABC_20'

# Database Password:

PASSDB='X1Y2Z3'

# Database Name:

DBNAME='POL_40'

#####

# Path to Root :

ATROOT='/usr/home/USERNAME'

# Path to Backup directory:

WAYTO='/usr/home/USERNAME/public_html/Backup'

# Password to encrypt Database Before Sending it by email (Do NOT use semi-colon ";"):

OPENPASS='%4957I@6IcrV'

# Email address where send to the Backuped, Zipped and Encrypted Database:

FEMAIL='me@mail.com'

# Email Subject (date and time is added at the end of this subject):

SUBJECT='POL_40 Backup'

# Delete the Zipped Backups Databases Older than n Day:

TDAYS=1

##############################
#                            #
#  NO Change Under This Box  #
#                            #
##############################

DATE=`date +\%Y-\%m-\%d-at-\%H:\%M:\%S`

/usr/local/bin/mysqldump -h$SERVERDB -u$USERDB -p$PASSDB $DBNAME | gzip -9 > $WAYTO/$DBNAME-$DATE.sql.gz
 
openssl enc -aes-256-cbc -salt -a -in $WAYTO/$DBNAME-$DATE.sql.gz -out $WAYTO/$DBNAME-$DATE.sql.gz.ENC -pass pass:$OPENPASS

[ -f $ATROOT/sent.lock ] && rm $ATROOT/sent.lock

[ -f $ATROOT/sent ] && rm $ATROOT/sent

[ ! -f $ATROOT/sent ] && touch $ATROOT/sent

/usr/local/bin/mutt -s "$SUBJECT $DATE" -a $WAYTO/$DBNAME-$DATE.sql.gz.ENC -- $EMAIL > /dev/null

[ -f $ATROOT/sent.lock ] && rm $ATROOT/sent.lock

[ -f $ATROOT/sent ] && rm $ATROOT/sent

[ ! -f $ATROOT/sent ] && touch $ATROO/sent

rm $WAYTO/$DBNAME-$DATE.sql.gz.ENC

find $WAYTO/$DBNAME-*.sql.gz -mtime +$DAYS -exec rm {} \;

#####################################################################################################################
#                                             																									#
#  Decrypt from Pair:																										#
#                                             																									#
# ??? (Sorry, I don't know)	 																									#
#                                             																									#
# ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------	#
#                                             																									#
# Decrypt from Windows: 																										#
#                                             																									#
# 1 - Download "OpenSSL for Windows 64bit version" ref:																					# 
#      http://code.google.com/p/openssl-for-windows/																						#
# 2 - Install "OpenSSL for Windows 64bit version" in this directory: "C:\Program Files\OpenSSL"																#
# 3 - Run a command line from Windows: "cmd.exe" ref: 																						#
#      http://www.howtogeek.com/howto/windows-vista/run-a-command-as-administrator-from-the-windows-vista-run-box/													#
# 4 - From Windows command line, go to this directory: "OpenSSL" of your computer: "cd C:\Program Files\OpenSSL\bin"													#
# 5 - Run this command line: "openssl"																								#
# 6 - Run this command line:																										#
#"enc -d -aes-256-cbc -salt -a -in C:\Users\YourUserName\Downloads\$DBNAME-$DATE.sql.gz.ENC -out C:\Users\YourUserName\Downloads\$DBNAME-$DATE.sql.gz -pass pass:$OPENPASS"	#
# 7 - Enter your password: $OPENPASS																								#
#                                             																									#
#  *********************************************************************************************************************************************************************************************	#
#                                             																									#
#  Unzip from Pair:																											#
#                                             																									#
# gunzip $DBNAME-$DATE.sql.gz 																									#
#                                             																									#
# ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------	#
#                                             																									#
# Unzip from Windows : 																										#
#                                             																									#
# 1 - Install and run 7Zip ref:																									#
#       http://www.7-zip.org/																									#
# 2 - From 7Zip...																											#
# 3 - Open the archive on your computer $DBNAME-$DATE.sql.gz 																				#
# 4 - If needed, open SQL file with  Notepad++ ou SciTE ref:																					#
#      http://notepad-plus-plus.org/																									#
#      http://www.scintilla.org/SciTE.html																								#
# 5 - Or reinstall your database																									#
#                                             																									#
#####################################################################################################################
```


----------



## Sarouel (Sep 3, 2018)

_2018 update:_

*Warning with MySQL 5.6*

"--password=$dbpwd" gets an "insecure" warning with MySQL 5.6:

_"Warning: Using a password on the command line interface can be insecure."_

https://dev.mysql.com/doc/refman/5.6/en/mysql-config-editor.html

You can fix this issue this way:

1 - Create a ".mylogin.cnf" file with this command line:

mysql_config_editor set --login-path=DBNAME --host=DBSERVER --user=USERNAME --password

DBNAME - The name of the database for which you wish to set the login

DBSERVER - The name of the server holding the DB, such as localhost

USERNAME - The MySQL login username for the DBNAME database

You will then be prompted for your database password

Please note: This ".mylogin.cnf" file will be saved here: /usr/home/YourUsername/.mylogin.cnf

Your database password will be encrypted there.

2 - If you have more than only one database in your database server, please add your additional databases credentials into your just created ".mylogin.cnf" file your second, third, fourth... databases with the same command line:

mysql_config_editor set --login-path=DBNAME2 --host=DBSERVER --user=USERNAME2 --password
mysql_config_editor set --login-path=DBNAME3 --host=DBSERVER --user=USERNAME3 --password
Etc.

All your additional database passwords will be encrypted in the same ".mylogin.cnf" file.

*****

Please note, I leaved Pair networks in 2017 for a "MX90-SSD" Hetzner managed server the same year.

*IMPORTANT*

At Hetzner you will need to create a text file "mailmessage.txt" on your "$ATROOT" directory:

$ATROOT/mailmessage.txt

From this command line:

/usr/bin/mutt -s "$SUBJECT $DATE" -a $WAYTO/$DBNAME-$DATE.sql.gz.ENC -- $EMAIL < $ATROOT/mailmessage.txt

Message's body could be 2 words long like "Backup succeed.".


```
#!/bin/sh

# Database Server URL:

SERVERDB='my-database-server.com'

# Database User Name:

USERDB='Database-User-Name'

# Database Password:

# Do No more use Database Password from MySQL 5.6 because it is insecure!

# If you are using MySQL 5.6 and up, please read the above notice and use "mysql_config_editor" instead.

# PASSDB='Do-Not-Note-Database-Password-Here-Because-This-Is-Insecure'

# Database Name:

DBNAME='My-Database-Name'

#####

# Path to Root :

ATROOT='/usr/home/My-Account'

# Path to Backup directory:

WAYTO='/usr/home/My-Account/My-Backup-Directory'

# Password to encrypt Database Before Sending it by email (Do NOT use semi-colon ";"):

OPENPASS='2$qr€ab2xnUB64BxVn'

# Email address where send to the Backuped, Zipped and Encrypted Database:

EMAIL='MyEmailAddress@gmx.com'

# Email Subject (date and time is added at the end of this subject):

SUBJECT='Account Backup Date:'

# Delete the Zipped Backups Databases Older than n Day:

DAYS=1

#########################
#                                              #
#  Do NOT Change Under This Box   #
#                                              #
#########################

DATE=`date +\%Y-\%m-\%d-at-\%H:\%M:\%S`

/usr/bin/mysqldump --login-path=$DBNAME $DBNAME --single-transaction --routines --triggers --default-character-set=latin1 | gzip -9 > $WAYTO/$DBNAME-$DATE.sql.gz

openssl enc -aes-256-cbc -salt -a -in $WAYTO/$DBNAME-$DATE.sql.gz -out $WAYTO/$DBNAME-$DATE.sql.gz.ENC -pass pass:$OPENPASS

[ -f $ATROOT/sent.lock ] && rm $ATROOT/sent.lock

[ -f $ATROOT/sent ] && rm $ATROOT/sent

[ ! -f $ATROOT/sent ] && touch $ATROOT/sent

# At Pair.com :

# /usr/bin/mutt -s "$SUBJECT $DATE" -a $WAYTO/$DBNAME-$DATE.sql.gz.ENC -- $EMAIL > /dev/null

# At Hetzner.com :

/usr/bin/mutt -s "$SUBJECT $DATE" -a $WAYTO/$DBNAME-$DATE.sql.gz.ENC -- $EMAIL < $ATROOT/mailmessage.txt

[ -f $ATROOT/sent.lock ] && rm $ATROOT/sent.lock

[ -f $ATROOT/sent ] && rm $ATROOT/sent

[ ! -f $ATROOT/sent ] && touch $ATROOT/sent

rm $WAYTO/$DBNAME-$DATE.sql.gz.ENC

find $WAYTO/$DBNAME-*.sql.gz -mtime +$DAYS -exec rm {} \;

#############################################################################################################
#                                                                                                                                         #
#  To Decrypt From UNIX Terminal :                                                                                            #
#                                                                                                                                         #
# 1 - Open OpenSSL: openssl                                                                                            #
# 2 - enc -d -aes-256-cbc -salt -a -in $WAYTO/$DBNAME-$DATE.sql.gz.ENC -out $WAYTO/$DBNAME-$DATE.sql.gz -pass pass:$OPENPASS                            #
#                                                                                                                                         #
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------    #
#                                                                                                                                         #
# To Decrypt From Windows :                                                                                         #
#                                                                                                                                         #
# 1 - Download "OpenSSL for Windows 64bit version"                                                                                #
# 2 - Install "OpenSSL for Windows 64bit version" on this directory : "C:\Program Files\OpenSSL"                                                #
# 3 - Open the Command Prompt Windows console : "cmd.exe"                                                                        #
# 4 - Go from the Windows console to the directory "OpenSSL" of your desktop : "cd C:\Program Files\OpenSSL\bin"                                        #
# 5 - Open "openssl" from Windows console : "openssl"                                                                                #
# 6 - Run this command line :                                                                                                 #
#    enc -d -aes-256-cbc -salt -a -in C:\Users\My-Username\Downloads\$DBNAME-$DATE.sql.gz.ENC -out C:\Users\My-Username\Downloads\$DBNAME-$DATE.sql.gz -pass pass:$OPENPASS"        #
# 7 - Enter your password at the prompt: $OPENPASS                                                                                    #
#                                                                                                                                         #
#  ********************************************************************************************************************************************************************************    #
#                                                                                                                                         #
#  To Unzip from Hetzner :                                                                                            #
#                                                                                                                                         #
# gunzip $DBNAME-$DATE.sql.gz                                                                                             #
#                                                                                                                                         #
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------    #
#                                                                                                                                         #
# To Unzip from  Windows :                                                                                             #
#                                                                                                                                         #
# 1 - Open 7Zip                                                                                                     #
# 2 - From 7Zip...                                                                                                    #
# 3 - Open the archive on your Windows computer $DBNAME-$DATE.sql.gz                                                                     #
# 4 - If needed, open the SQL file with Notepad++ or SciTE, or...                                                                    #
# 5 - Reinstall your database on your Web server                                                                                    #
#                                                                                                                                         #
#############################################################################################################
```


----------

