![]() |
Need a fresh pair of eyes to review my backup+restore scripts
I've created a Backup script and a Restore script to backup my websites on a shared host (dreamhost) that I have SSH access to. The scripts should backup/restore my main website [domain.com], my subdomain [store.domain.com] of the website (which is a Magento Store), and lastly my Magento DB.
Backup Script: Code:
#!/bin/shCode:
#!/bin/shThanks in advance, -BassKozz |
Looks good to me. Only things I'd do is consistent and proper quoting of variables ("${STORESOURCE}" vs $STORESOURCE), making sure you're rooted in the right place (as in 'BACKUPDIR="/home/user/BACKUPS/${TODAYSDATE}"; mkdir "${BACKUPDIR}"; cd "${BACKUPDIR}" || exit 1'), append both stdout and stderr to the log (' 2>&1>> "${LOG}"') and pipe mysqldump output through gzip to get a compressed backup. Optionally you could use MD5 or SHA1 sums to be able to verify file integrity and use "getopts" to combine making, verifying and restoring backups from one script.
|
I would add some sanity checks, like:
Do files and directories exists before I create them. i.e. Code:
if [ ! -x "$TODAYSDATE" ] thenCode:
if [ ! -w "$LOG" ] thenDid the result of tar return an error i.e. Code:
tar cvpzfP "$STOREDESTINATION" "$STORESOURCE" >> $LOG |
1. -x = file is executable: you mean -e (exists) or -d (is a dir)
http://www.tldp.org/LDP/abs/html/fto.html 2. #? : meaningless; try $? |
Thanks for all the feedback everyone,
Quote:
And do I have to put quotes around it ( is it "${STORESOURCE}" or ${STORESOURCE} ) ? Quote:
Quote:
I am confused about how to output the stdout and stderr to the log, and what is "stdout" and "stderr" ? Quote:
Sorry, I am new at this :-P Quote:
Would this be able to verify the integrity of the MySQL DB also? Quote:
... Quote:
Quote:
|
The difference between
${var} and $var is that if $var is embedded in another string eg string$varstring2, the interpreter can't figure out where the varname ends, so use string${var}string2 and the interpreter knows the varname starts at '${' and stops at '}' (ignoring quote marks). Bookmark my hyperlink above and read it. stdin = standard input channel (aka '0' ie zero) stdout = standard output channel (aka '1') stderr = standard error channel (aka '2') By default, these 3 are automatically assigned to each process at creation. To capture stdout, stderr to same file /path/to/program 1> program.log 2>&1 The first mention of '1' can be assumed, so we get /path/to/program > program.log 2>&1 ie send output to channel 1 (log file), send chan 2 to chan 1 => logfile also |
...in addition, wrt "${var}" vs $var it's not only embedding but also safeguarding against default IFS probs, names with spaces.
Compressing a backup here is as simple as running 'mysqldump $youroptions | gzip > "${MYSQLDBDUMP}.gz"'. Hashing files can be done by looping over a list of files with md5sum or sha1sum, as in 'find /path/to/files -type f -print0|xargs -0 -iF md5sum 'F' > /path/of/files.md5', or recursively hashing them with md5deep or sha1deep: 'sha1deep -krs /path/to/files > /path/of/files.sha1'. Hashing a database dump doesn't make much sense unless you would want to verify the integrity of the package moving it between systems. I don't think MySQL supports internal hashing like Oracle does, maybe Maatkit's 'mk-table-checksum' can help. Checking integrity by cronjobbing 'mysqlcheck -s -u root -p --all-databases' could be a way except this locks the db while it is checking but unfortunately I'm not that deep in database forensics to suggest better/more efficient ways to hash or validate stuff. For getopts type 'help getopts' if you run BaSH, else the http://www.tldp.org/LDP/abs/ will definately help. |
| All times are GMT -5. The time now is 04:02 AM. |