Shell backup script modification


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell backup script modification
# 1  
Old 12-12-2017
Shell backup script modification

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.

Code:
#!/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

# 2  
Old 12-12-2017
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...
# 3  
Old 12-12-2017
Quote:
Originally Posted by 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 ----------

Quote:
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
# 4  
Old 12-12-2017
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.
# 5  
Old 12-12-2017
Quote:
Originally Posted by 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
Quote:
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?

Last edited by rbatte1; 01-08-2018 at 08:43 AM.. Reason: Added ICODE tags and corrected spelling and grammar
# 6  
Old 12-12-2017
Quote:
Originally Posted by 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:
Code:
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.


Quote:
. . . and can you tell me what braces you mean?
i have no knowledge of this
Try
Code:
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.
# 7  
Old 12-12-2017
Quote:
Originally Posted by RudiC
You can add several files / directories to go into the same archive at the end of the tar command:
Code:
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
Code:
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
Code:
/var
/etc

would I be correct to change
Code:
source_dir='/opt'

to
Code:
source_dir='/'

then
Code:
tar -cf "$backup_file" dailybackup*

to
Code:
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


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 12-12-2017 at 12:04 PM.. Reason: Added CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with Backup Shell Script

hello guys i am new and i am using Solaris 8 as operating system. i have some CD and i want to install the data from it. Package and patches. Which command shall i use? thanks (3 Replies)
Discussion started by: moh_abaloo
3 Replies

2. Shell Programming and Scripting

Shell script to call Oracle archive backup script when file system reaches threshold value

Hello All, I need immediate help in creating shell script to call archivebkup.ksh script when archive file system capacity reaches threshold value or 60% Need to identify the unique file system that reaches threshold value. ex: capacity ... (4 Replies)
Discussion started by: sasikanthdba
4 Replies

3. Shell Programming and Scripting

Needed shell script to read txt file and do some modification

Hi ...programmers... I need a shell script to perform some specific task.. my txt file looks like this netcdf new { dimensions: XAX1_11 = 11 ; variables: double XAX1_11(XAX1_11) ; XAX1_11:point_spacing = "even" ; XAX1_11:axis = "X" ; float DEPTH(XAX1_11) ;... (19 Replies)
Discussion started by: Akshay Hegde
19 Replies

4. Shell Programming and Scripting

Help with Backup Shell Script for Network Device Configuration backup

HI all, im new to shell scripting. need your guidence for my script. i wrote one script and is attached here Im explaining the requirement of script. AIM: Shell script to run automatically as per scheduled and backup few network devices configurations. Script will contain a set of commands... (4 Replies)
Discussion started by: saichand1985
4 Replies

5. Shell Programming and Scripting

Help with backup shell script

Hello, Need help with a script to backup a configuration file BSD Save the file / Firewall / ConfigFiles to a remote ftp server here is the script # / bin / sh Date = $ (date +% d-% Y-% m-H-M) tar-cvf ConfigFiles.tar / Firewall / ConfigFiles ConfigFiles.tar mv / Firewall-$... (11 Replies)
Discussion started by: telouet
11 Replies

6. Shell Programming and Scripting

Modification in shell script

Hello Team, I have prepared script which will check for listening message for ports 1199,1200 and 1201. I need modifcation in script in such a way that if port 1200 is not listening then it should message rmi port 1200 is not listening. Smap for port 1199 and 1201. kindly guide me to acheive... (4 Replies)
Discussion started by: coolguyamy
4 Replies

7. Shell Programming and Scripting

Help with Shell Script Modification

Hi all Iam very new to Shell Scripting, I have to modify a shell script looking at an existing one except that it will query against some table X in A database. Befor Spooling check if there are any reload files if there archive the files. The above scipt executes some abc.sql which will b a new... (2 Replies)
Discussion started by: Varunkv
2 Replies

8. UNIX for Dummies Questions & Answers

File comparision and modification using shell script

Hello everyone, I would like to know how to compare two files and modify any differences with some other data using shell script. I think it would be better understood with an example. I got two files named 'filex' and filey'. 'filex' is constant file without any changes in data. 'filey' is... (2 Replies)
Discussion started by: maddy81
2 Replies

9. Shell Programming and Scripting

crontab entry modification using shell script

hi Friends, iam trying to write a script which will grep the particular entry in crontab of root and enable/disable it .iam a novice in scripting. your suggestions are most welcome..please help Cheers!! (4 Replies)
Discussion started by: munishdh
4 Replies

10. UNIX for Dummies Questions & Answers

Shell script: last modification date for a file

Hi i have a ques in Shell scripting: ques: accept a filename as a command line argument. Validate the input and display the last modification date for that file. Help pls. (4 Replies)
Discussion started by: onlyc
4 Replies
Login or Register to Ask a Question