0

I am trying to make a bash script to backup my sevrer, however it is creating empty tar archive and empty sql files and I don't know why. Can anyone see the problems here?

#!/bin/bash
SERVER_DIR="/var/www/vhosts/site.org"
DATE=$(date +"%d-%m-%Y")
BACKUP_DIR="/backups/$DATE"
NAME="full-$DATE"

MYSQLUSER="admin"
MYSQLPASS="pass"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"

mkdir -p $BACKUP_DIR
tar -zcvf $BACKUP_DIR/$NAME.tar.gz $SERVER_DIR
$MYSQLDUMP -u $MYSQLUSER -p$MYSQLPASS --all-databases | $GZIP -9 > $BACKUP_DIR/$NAME.sql
find /backup/ -mtime +31 -exec rm -rf {} \;
3
  • Have you tried outputting your defined variables to see if they are alright? and use their values to run the backup commands manually (Replaced by values) ? Commented Sep 2, 2011 at 0:39
  • and can you echo $BACKUP_DIR/$NAME to make sure it is actually what you want it to be? Commented Sep 2, 2011 at 0:40
  • Put set -xv at the top of your script to help debug it. This will print out each line that will be executed, and then the line with the various variable values filled in. That will help verify your assumptions. You should also test the status of various commands. For example, check $? after the tar command to make sure it executed correctly. Commented Sep 2, 2011 at 3:05

3 Answers 3

2

I think you are just missing a -c on the gzip line, try:

$MYSQLDUMP -u $MYSQLUSER -p$MYSQLPASS --all-databases | $GZIP -c9 > $BACKUP_DIR/$NAME.sql.gz
Sign up to request clarification or add additional context in comments.

1 Comment

Welcome @gbp. I agree with you analysis. Good luck.
0

Are you sure you have right permission to access SERVER_DIR="/var/www/vhosts/site.org", when you run you script.

Comments

0

You are creating in /backups but removing old backups from /backup. That hardly matters to the end result, but you will be running out of disk in a few months.

I'm not sure it's useful to have the date in both the directory name and the file names, but that's obviously up to you.

The SQL file name should perhaps have a .gz suffix?

It's hardly useful to put MYSQLDUMP and GZIP in a variable using which; if they can be found on the PATH, the shell will find them as well; or rather, if the shell can't find something, neither can which.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.