开发者

Bash backup script

开发者 https://www.devze.com 2023-04-01 05:25 出处:网络
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. Ca开发者_Python百科n anyone see the problems here?

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. Ca开发者_Python百科n 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 {} \;


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号