# Shell backup script modification

**URL:** <https://community.unix.com/t/shell-backup-script-modification/369796>\
**Category:** Shell Programming and Scripting\
**Created:** [December 12, 2017, 3:44am UTC](https://community.unix.com/t/shell-backup-script-modification/369796 "2017-12-12T03:44:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![galapagos8000](https://community.unix.com/letter_avatar/galapagos8000/32/6_449c533f586939b6e5f246632a46421f.png) [@galapagos8000](https://community.unix.com/u/galapagos8000)\
**Post date:** [December 12, 2017, 3:44am UTC](https://community.unix.com/t/shell-backup-script-modification/369796/1 "2017-12-12T03:44:43Z")

</div>

so I'm using this shell script someone made to backup data on a server in an archive (gpg encrypted) and upload to an FTP (meant to be run as a cron job daily). can one of the experts here confirm if the script is fine?

It is meant to backup the folder /opt and the sql data  
i want to know how I can add multiple folders to the backup? Like /var, /opt, /etc ?

and I cant find any sql authentication details in the script. Is it able to backup the whole sql without requiring sql login?

i have no experience with shell, sorry.

```nohighlight
#!/bin/sh

set -e
set -u

PATH='/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin'
export PATH

#
# Configuration:
#
source_dir='/opt' 
ftp_server='IP'
ftp_user='backup'
ftp_pass='pass'
gpg_pass='s3cret'

temp_dir="$(mktemp -d '/tmp/.daily-backup.XXXXXX')"
trap 'rm -rf "$temp_dir"' INT TERM EXIT
sql_file="$temp_dir/db.sql"
backup_file="$temp_dir/daily-$(date +'%F-%H-%M-%S').tar"

cd "$source_dir"
tar -cf "$backup_file" dailybackup*
cd "$OLDPWD"

mysqldump --quick --single-transaction --all-databases > "$sql_file"
tar -rf "$backup_file" -C "${sql_file%/*}" "${sql_file##*/}"

gpg --yes --batch --passphrase="$gpg_pass" -c $backup_file
rm -f "$backup_file"
backup_file="$backup_file.gpg"

#
# Remove the "-p" option if you don't want to use passive mode:
#
ftp -p -n <<-EOF
	open $ftp_server
	user $ftp_user $ftp_pass
	put $backup_file ${backup_file##*/}
	bye
EOF

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [December 12, 2017, 9:34am UTC](https://community.unix.com/t/shell-backup-script-modification/369796/2 "2017-12-12T09:34:03Z")

</div>

I can't find anything immediately wrong.  
One caveat is that you think it's backing up `/opt` but in fact it's `/opt/dailybackup*` , i.e. all files in `/opt` starting with that string.  
And, reassigning `backup_file` you may want to use braces to be on the safe side when expanding variables.  
For the sql authentication question I don't have an answer...

---

<div class="post-metadata">

**Author:** ![galapagos8000](https://community.unix.com/letter_avatar/galapagos8000/32/6_449c533f586939b6e5f246632a46421f.png) [@galapagos8000](https://community.unix.com/u/galapagos8000)\
**Post date:** [December 12, 2017, 9:55am UTC](https://community.unix.com/t/shell-backup-script-modification/369796/3 "2017-12-12T09:55:54Z")

</div>

> [@rudic](#):
>
> I can't find anything immediately wrong.  
> One caveat is that you think it's backing up `/opt` but in fact it's `/opt/dailybackup*` , i.e. all files in `/opt` starting with that string.  
> And, reassigning `backup_file` you may want to use braces to be on the safe side when expanding variables.  
> For the sql authentication question I don't have an answer...

can you tell me how I can change it to backup multiple directories like  
/var  
/opt  
/etc

can someone here pls help with sql problem

---------- Post updated at 09:55 AM ---------- Previous update was at 09:55 AM ----------

> [@](#):
>
> And, reassigning backup\_file you may want to use braces to be on the safe side when expanding variables.

and can you tell me what braces you mean?  
i have no knowledge of this

---

<div class="post-metadata">

**Author:** ![Peasant](https://community.unix.com/user_avatar/community.unix.com/peasant/32/2251_2.png) [@Peasant](https://community.unix.com/u/Peasant)\
**Post date:** [December 12, 2017, 10:17am UTC](https://community.unix.com/t/shell-backup-script-modification/369796/4 "2017-12-12T10:17:22Z")

</div>

Check your database configuration for sql login details.  
Probably the password is written in my.cnf file under user or globally in /etc/my.conf

I found this info online, i'm more of a postgresql user so take this with a grain of salt.

As for other directories, why would you want to use mysql backup script to backup /var /etc/ or similar ?  
If you want to backup other things, write a new script, leave mysql backup script to do what is has been written for.

Hope that helps  
Regards  
Peasant.

---

<div class="post-metadata">

**Author:** ![galapagos8000](https://community.unix.com/letter_avatar/galapagos8000/32/6_449c533f586939b6e5f246632a46421f.png) [@galapagos8000](https://community.unix.com/u/galapagos8000)\
**Post date:** [December 12, 2017, 10:39am UTC](https://community.unix.com/t/shell-backup-script-modification/369796/5 "2017-12-12T10:39:37Z")

</div>

> [@peasant](#):
>
> Check your database configuration for sql login details.  
> Probably the password is written in my.cnf file under user or globally in /etc/my.conf
> 
> I found this info online, i'm more of a postgresql user so take this with a grain of salt.
> 
> As for other directories, why would you want to use mysql backup script to backup /var /etc/ or similar ?  
> If you want to backup other things, write a new script, leave mysql backup script to do what is has been written for.
> 
> Hope that helps  
> Regards  
> Peasant.

Yes you are right about my.cnf thank you.  
Actually its not for just sql.

The server is running a small webserver with a little sql data that is critical for a project, so I'm backing up all files + sql since its not a lot of data.

I don't know bash but here the source dir is `source_dir='/opt' `

If I want to backup multiple directories. would it be this? `source_dir='/opt /var /etc'`

I don't know the syntax.

and Rudi mentioned above

> [@](#):
>
> One caveat is that you think it's backing up /opt but in fact it's /opt/dailybackup\*, i.e. all files in /opt starting with that string.

How am I supposed to change it to backup the whole folders instead of folders/files matching the string?

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [December 12, 2017, 10:43am UTC](https://community.unix.com/t/shell-backup-script-modification/369796/6 "2017-12-12T10:43:01Z")

</div>

> [@galapagos8000](#):
>
> can you tell me how I can change it to backup multiple directories like  
> /var  
> /opt  
> /etc

You can add several files / directories to go into the same archive at the end of the tar command:

```plaintext
tar cf arc /opt /var /etc

```

or you follow the route you already took in above: `cd` to the target directory, and `tar -r` the files in question. `man tar` is a very valuable source of info for you.

> [@](#):
>
> . . . and can you tell me what braces you mean?  
> i have no knowledge of this

Try

```plaintext
backup_file="${backup_file}.gpg"

```

Expansion will stop at the dot anyhow, but for you to be on the safe side in the future when there might be no dot use the braces.

---

<div class="post-metadata">

**Author:** ![galapagos8000](https://community.unix.com/letter_avatar/galapagos8000/32/6_449c533f586939b6e5f246632a46421f.png) [@galapagos8000](https://community.unix.com/u/galapagos8000)\
**Post date:** [December 12, 2017, 10:46am UTC](https://community.unix.com/t/shell-backup-script-modification/369796/7 "2017-12-12T10:46:27Z")

</div>

> [@rudic](#):
>
> You can add several files / directories to go into the same archive at the end of the tar command:
> 
> ```plaintext
> tar cf arc /opt /var /etc
> 
> ```
> 
> or you follow the route you already took in above: `cd` to the target directory, and `tar -r` the files in question. `man tar` is a very valuable source of info for you.
> 
> Try
> 
> ```plaintext
> backup_file="${backup_file}.gpg"
> 
> ```
> 
> Expansion will stop at the dot anyhow, but for you to be on the safe side in the future when there might be no dot use the braces.

in this script a source dir is specified first, then cd into it and then backup the mentioned folders.

so if I wanted to backup

```plaintext
/var
/etc

```

would I be correct to change

```plaintext
source_dir='/opt'

```

to

```plaintext
source_dir='/'

```

then

```plaintext
tar -cf "$backup_file" dailybackup*

```

to

```plaintext
tar -cf "$backup_file" opt var etc

```

???

(I removed '/' before opt, var, etc because its cd into same folder)

am i right here? sorry im noob

---

<div class="post-metadata">

**Author:** ![galapagos8000](https://community.unix.com/letter_avatar/galapagos8000/32/6_449c533f586939b6e5f246632a46421f.png) [@galapagos8000](https://community.unix.com/u/galapagos8000)\
**Post date:** [December 16, 2017, 11:26am UTC](https://community.unix.com/t/shell-backup-script-modification/369796/8 "2017-12-16T11:26:06Z")

</div>

someone help me here

---

<div class="post-metadata">

**Author:** ![jgt](https://community.unix.com/user_avatar/community.unix.com/jgt/32/10703_2.png) [@jgt](https://community.unix.com/u/jgt)\
**Post date:** [December 16, 2017, 12:09pm UTC](https://community.unix.com/t/shell-backup-script-modification/369796/9 "2017-12-16T12:09:51Z")

</div>

```nohighlight
tar -cf "$backup_file" opt var etc

```

Will create an archive file called whatever the value of $backup\_file is, containing the directories "opt" "var" and "etc" and all subdirectories.

---

<div class="post-metadata">

**Author:** ![galapagos8000](https://community.unix.com/letter_avatar/galapagos8000/32/6_449c533f586939b6e5f246632a46421f.png) [@galapagos8000](https://community.unix.com/u/galapagos8000)\
**Post date:** [January 7, 2018, 3:54am UTC](https://community.unix.com/t/shell-backup-script-modification/369796/10 "2018-01-07T03:54:13Z")

</div>

thank you
