0

I am using the following script to create a database backup and sending it to a storage location using scp.

What I noticed is that the resulting file very frequently delivers a seemingly correct file. For the most of the time, the file size is around the 3GiB mark, but sometimes the resulting file is some 900B, sometimes 2.2MiB.

The script is:

#!/bin/bash

# database credentials

DATABASEHOST=<host>
DATABASEUSER=<user>
DATABASEPASSWORD=<password>
DATABASESCHEMA=<schema>
DATABASEENV=<env>

# Local directory of mysqldump file
LOCALDIR=<localdir>

# Temporary directory for compressed file
TEMPDIR=<tempdir>

# Remote Directory for backups.
REMOTEDIR=<remote-dir>

# USERname to login as
BACKUPUSER=<backupuser>

# Backup host to login to
BACKUPHOST=<backuphost>

# mysqldump file
MYSQLDUMPFILE="$(date +%Y%m%d)"_bkp_"$DATABASESCHEMA".sql

# compressed file
COMPRESSEDFILE="$(date +%Y%m%d)"_"$DATABASEENV"_"$DATABASESCHEMA".tar.gz

#--- end config


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

#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 storage location"
scp "$TEMPDIR"/"$COMPRESSEDFILE" "$BACKUPHOST":"$REMOTEDIR"

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

rm "$LOCALDIR"/"$MYSQLDUMPFILE" > /dev/null 2>&1

rm "$TEMPDIR"/"$COMPRESSEDFILE" > /dev/null 2>&1

echo $(date +%H:%M)

Since most of the times the resulting file is ok, I'm thinking the problem may rely on the scp part.

How can I ensure the storage location generated file is the same as the original and, if they're different, try to scp again?

0

use rsync please. Rsync checks md5 hash of file.

rsync examples

  • While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. – terdon Apr 13 '17 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.