1

I make daily backups of the mysql database storing the contents of my online website, and I would like to automate those half-dozen commands into a single command.

I tried a naïve solution, by simply putting the list of commands into a Bash script. This fails, as the script stops once it's entered the remote host.

Any help appreciated.

My failed (bash script) attempt :

#! /bin/bash

  echo "Connecting to the remote host ...";
  ssh -p myport [email protected] ;
  echo "Making the backup on the remote host ...";
  mysqldump --databases --user=mysqluser --password=mypassword mydatabase > backup_copy.sql ;
  echo "Compressing the sql backup ...";
  gzip backup_copy.sql;
  echo "Leaving the remote host ...";
  exit;
  echo "Copying the backup from the remote host ...";
  scp -P myport [email protected]:backup_copy.sql.gz /my/location/to/store/backups ;
  echo "Task finished";
3

1 Answer 1

-1
#!/bin/bash

# database credentials
DATABASEHOST="host"
DATABASEUSER="user"
DATABASEPASSWORD="password"
DATABASESCHEMA="database"
DATABASEENV="backup"

# Local directory of mysqldump file
LOCALDIR=/home/user/db-bkp

# Temporary directory for compressed file
TEMPDIR=/home/user/tmp

# Remote Directory for backups.
REMOTEDIR=/home/user/db-bkp

# Backup host to login to
BACKUPHOST="otherhost"
#--- end config

echo $(date +%H:%M)
echo "Creating the MySQL dump" 
mysqldump --host="$DATABASEHOST" --user="$DATABASEUSER" --password="$DATABASEPASSWORD" --single-transaction "$DATABASESCHEMA" > "$LOCALDIR"/"$(date +%Y%m%d)_bkp_$DATABASESCHEMA.sql"

#echo "Generating md5sum"
md5sum "$LOCALDIR"/* > "$LOCALDIR"/checklist.chk

#echo "Compressing the dump and checklist"
tar -cvzf "$TEMPDIR"/$(date +%Y%m%d)"_"$DATABASEENV"_"$DATABASESCHEMA".tar.gz" "$LOCALDIR"/*

#echo "Sending the compressed file to d2"
## scp "$TEMPDIR"/"$DATABASESCHEMA".tar.gz "$BACKUPHOST":"$REMOTEDIR"

#echo "Removing generated files"
#rm "$LOCALDIR"/checklist.chk > /dev/null 2>&1

#rm "$LOCALDIR"/"$(date +%Y%m%d)_bkp_$DATABASESCHEMA.sql" > /dev/null 2>&1

#rm "$TEMPDIR"/"$DATABASESCHEMA".tar.gz > /dev/null 2>&1

rm "$LOCALDIR"/"$(date +%Y%m%d)_bkp_$DATABASESCHEMA.sql"
echo $(date +%H:%M)

replace host,user,password,database,LOCALDIR,BACLUPHOST,REMOTEDIR by your own parameters

1
  • This "answer" adds nothing to your comment. I'd rather you answered the question I asked you in the comments Feb 22, 2017 at 5:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .