1

I am trying to write a simple backup script that does the following:

#!/bin/bash
#backupScript

mysqldump mysql -uxxxxx -pxxxxx Database1 Database2 | gzip > "MainWordPress.gz"

time_stamp = `date +%Y%m%d.%H%M`
file_name = 'backup-{$time_stamp}.tar.gz'
sources = 'DIR/ MainWordPress.gz'
tar -cvf file_name sources

rm MainWordPress.gz

cp file_name some/destination

But the issue is that it doesnt like:

./runBackup.sh: line 6: time_stamp: command not found
./runBackup.sh: line 7: file_name: command not found
./runBackup.sh: line 8: sources: command not found
tar: sources: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

and i'm deeply confused ...

LogicLooking
  • 916
  • 1
  • 16
  • 32
  • Should `tar -cvf file_name sources` perhaps be `tar -cvf "$file_name" $sources`? (And similar for `cp`.) And `$time_stamp` won't be expanded in `'backup-{$time_stamp}.tar.gz'` (single quotes). Time for a Bash/shell scripting tutorial? – Biffen Jan 27 '16 at 13:54

2 Answers2

7

Remove the spaces around the equal sign:

#!/bin/bash
#backupScript

mysqldump mysql -uxxxxx -pxxxxx Database1 Database2 | gzip > "MainWordPress.gz"

time_stamp=$(date +%Y%m%d.%H%M)
file_name="backup-${time_stamp}.tar.gz"
sources='DIR/MainWordPress.gz'
tar -cvf file_name sources

rm MainWordPress.gz

cp file_name some/destination
opalenzuela
  • 3,139
  • 21
  • 41
2

Remove the spaces between the variable and the = :

#!/bin/bash
#backupScript

mysqldump mysql -uxxxxx -pxxxxx Database1 Database2 | gzip > "MainWordPress.gz"

time_stamp=`date +%Y%m%d.%H%M`
file_name='backup-{$time_stamp}.tar.gz'
sources='DIR/ MainWordPress.gz'
tar -cvf file_name sources

rm MainWordPress.gz

cp file_name some/destination
Asenar
  • 6,732
  • 3
  • 36
  • 49