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.
#!/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
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...
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.
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
How am I supposed to change it to backup the whole folders instead of folders/files matching the string?
You can add several files / directories to go into the same archive at the end of the tar command:
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
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.