Neutralise
Neutralise

Reputation: 329

Bash backup script

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 {} \;

Upvotes: 0

Views: 1031

Answers (3)

tripleee
tripleee

Reputation: 189357

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.

Upvotes: 0

Leon
Leon

Reputation: 2896

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

Upvotes: 0

gbp
gbp

Reputation: 46

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

Upvotes: 2

Related Questions