# Storing a variable?

**URL:** <https://community.unix.com/t/storing-a-variable/183256>\
**Category:** Shell Programming and Scripting\
**Created:** [January 26, 2008, 5:43pm UTC](https://community.unix.com/t/storing-a-variable/183256 "2008-01-26T17:43:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hoover90](https://community.unix.com/user_avatar/community.unix.com/hoover90/32/3735_2.png) [@hoover90](https://community.unix.com/u/hoover90)\
**Post date:** [January 26, 2008, 5:43pm UTC](https://community.unix.com/t/storing-a-variable/183256/1 "2008-01-26T17:43:49Z")

</div>

I'm writing a bash shell script to backup several mysql databases. This script will run on a daily basis and send a copy to a remote FTP repository. The filenames are in the format DATE.backup.sql. How do I store the DATE variable so I can delete/move/etc the file on the FTP server the next time the script runs?

```nohighlight
#!/bin/bash

DATE=$(date +%d%b%y_%k.%M.%S)

cd /web/.hhome/mysql.backup/data
mysqldump -ubackup -ppassword --databases db1 db3 > $DATE.backup.sql

gzip -9 $DATE.backup.sql

crypt key < $DATE.backup.sql.gz > $DATE.backup.sql.enc.gz
rm -rf $DATE.backup.sql.gz

ftp -inv ftp.somehost.com<<ENDFTP
user hoover90 password
put $DATE.backup.sql.enc.gz
bye
ENDFTP

```

I can use pretty much any method, but it has to be done within this bash script.

Thanks!

---

<div class="post-metadata">

**Author:** ![lucaswoods](https://community.unix.com/letter_avatar/lucaswoods/32/6_449c533f586939b6e5f246632a46421f.png) [@lucaswoods](https://community.unix.com/u/lucaswoods)\
**Post date:** [January 26, 2008, 7:29pm UTC](https://community.unix.com/t/storing-a-variable/183256/2 "2008-01-26T19:29:18Z")

</div>

At the top of the script:  
read LASTDATE \< .last\_date

During the FTP session:  
del $LASTDATE.backup.sql.enc.gz

After the FTP session:  
echo $DATE \> .last\_date

Of course you will need some conditional logic there to deal with .last\_date not existing... you could create the first .last\_date file manually yourself. If you need help with that as well let us know.

---

<div class="post-metadata">

**Author:** ![hoover90](https://community.unix.com/user_avatar/community.unix.com/hoover90/32/3735_2.png) [@hoover90](https://community.unix.com/u/hoover90)\
**Post date:** [January 26, 2008, 8:03pm UTC](https://community.unix.com/t/storing-a-variable/183256/3 "2008-01-26T20:03:40Z")

</div>

Alright, everything you suggested works except for writing $DATE to .last\_date. From bash, I can use the same command format (replacing $DATE with $(date +%d%b%y\_%k.%M.%S), or just echo "asdfqwer1234blahblah" \> .last\_date), but it will not write $DATE to .last\_date when the script is executed.

```nohighlight
#!/bin/bash

read LASTDATE < .last_date
DATE=$(date +%d%b%y_%k.%M.%S)

cd /web/.hhome/mysql.backup/data
mysqldump -ubackup -ppassword --databases db1 db2 db3 > $DATE.backup.sql

gzip -9 $DATE.backup.sql
crypt asdfqwer1234 < $DATE.backup.sql.gz > $DATE.backup.sql.enc.gz
rm -rf $DATE.backup.sql.gz

ftp -inv ftp.somehost.com<<ENDFTP
user hoover90 password
del $LASTDATE.backup.sql.enc.gz
put $DATE.backup.sql.enc.gz
bye
ENDFTP

echo $DATE > .last_date

```

---

<div class="post-metadata">

**Author:** ![lucaswoods](https://community.unix.com/letter_avatar/lucaswoods/32/6_449c533f586939b6e5f246632a46421f.png) [@lucaswoods](https://community.unix.com/u/lucaswoods)\
**Post date:** [January 26, 2008, 8:24pm UTC](https://community.unix.com/t/storing-a-variable/183256/4 "2008-01-26T20:24:15Z")

</div>

That is because you are reading the .last\_date from your current working directory, then changing to /web/.hhome/mysql.backup/data before writing out the variable.

I'm guessing if you check /web/.hhome/mysql.backup/data/.last\_date it will be there.

I would move the "read LASTDATE \< .last\_date" to AFTER the cd /web/.hhome/mysql.backup/data.

---

<div class="post-metadata">

**Author:** ![hoover90](https://community.unix.com/user_avatar/community.unix.com/hoover90/32/3735_2.png) [@hoover90](https://community.unix.com/u/hoover90)\
**Post date:** [January 26, 2008, 8:39pm UTC](https://community.unix.com/t/storing-a-variable/183256/5 "2008-01-26T20:39:58Z")

</div>

You guessed right! It works!

Thank you!
