1

Every so often I erase the SD card of my RaspberryPI and start from scratch from the latest image. To make the initial configuration process faster, I have created a bash script that I can use to initialize the PI with many defaults. I'm having trouble creating a backup-database.sh bash script from within my startup bash script.

This step should create a new file called backup-database.sh.

printf "
#calculate the backup filename
backupFile=`date '+%Y-%m-%d--%H-%M-%S'`.sql
echo `date '+%Y-%m-%d--%H-%M-%S'`: \"Creating database backup\"
#create full backup of entire database, give it today's date
mysqldump -uroot -pNOT_MY_ACTUAL_PASSWORD --all-databases > /var/db-backups/\$backupFile

#upload the backup to dropbox
echo `date '+%Y-%m-%d--%H-%M-%S'`: \"Uploading backup to dropbox\"
/var/db-backups/dropbox_uploader.sh upload /var/db-backups/\$backupFile db/\$backupFile
" > /var/db-backups/backup-database.sh

The problem is, the contents of backup-database.sh look like this after I run the above command:

#calculate the backup filename
backupFile=2018-02-19--19-59-11.sql
echo 2018-02-19--19-59-11: "Creating database backup"
#create full backup of entire database, give it today's date
mysqldump -uroot -pNOT_MY_ACTUAL_PASSWORD --all-databases > /var/db-backups/$backupFile

#upload the backup to dropbox
echo 2018-02-19--19-59-11: "Uploading backup to dropbox"
/var/db-backups/dropbox_uploader.sh upload /var/db-backups/$backupFile db/$backupFile

I want backup-database.sh to look like this:

#calculate the backup filename
backupFile=`date '+%Y-%m-%d--%H-%M-%S'`.sql
echo `date '+%Y-%m-%d--%H-%M-%S'`: "Creating database backup"
#create full backup of entire database, give it today's date
mysqldump -uroot -pNOT_MY_ACTUAL_PASSWORD --all-databases > /var/db-backups/$backupFile
#upload the backup to dropbox
echo `date '+%Y-%m-%d--%H-%M-%S'`: "Uploading backup to dropbox"
/var/db-backups/dropbox_uploader.sh upload /var/db-backups/$backupFile db/$backupFile

All of the dates are being inserted when I CREATE the backup-database.sh file, but I need those date lines to be inserted literally so that the date is calculated when backup-database.sh is executed. I've tried escaping the backticks `, inserting double percents %%, etc.

How can I correctly escape the date command within the printf command so that it is written literally to the backup-database.sh file?

TwitchBronBron
  • 2,783
  • 3
  • 21
  • 45

2 Answers2

2

you have to escape the backticks and add a % before all format characters.

printf "
#calculate the backup filename
backupFile=\`date '+%%Y-%%m-%%d--%%H-%%M-%%S'\`.sql
echo \`date '+%%Y-%%m-%%d--%%H-%%M-%%S'\`: \"Creating database backup\"
#create full backup of entire database, give it today's date
mysqldump -uroot -pNOT_MY_ACTUAL_PASSWORD --all-databases > /var/db-backups/\$backupFile

#upload the backup to dropbox
echo \`date '+%%Y-%%m-%%d--%%H-%%M-%%S'\`: \"Uploading backup to dropbox\"
/var/db-backups/dropbox_uploader.sh upload /var/db-backups/\$backupFile db/\$backupFile
" > /var/db-backups/backup-database.sh

the backslashes are needed to escape the special meaning of the ` character and %% prints a literal % in printf instead of using it as a format string

2

I recommend using a here-document:

cat > /var/db-backups/backup-database.sh <<'EOF'
#calculate the backup filename
backupFile=`date '+%Y-%m-%d--%H-%M-%S'`.sql
echo `date '+%Y-%m-%d--%H-%M-%S'`: "Creating database backup"
#create full backup of entire database, give it today's date
mysqldump -uroot -pNOT_MY_ACTUAL_PASSWORD --all-databases > /var/db-backups/$backupFile
#upload the backup to dropbox
echo `date '+%Y-%m-%d--%H-%M-%S'`: "Uploading backup to dropbox"
/var/db-backups/dropbox_uploader.sh upload /var/db-backups/$backupFile db/$backupFile
EOF

Quoting the tag (EOF) ensures that the lines don't get expanded before the actual redirection so there is no need to quote them.

PesaThe
  • 7,259
  • 1
  • 19
  • 43