Bash 备份脚本
我正在尝试制作一个 bash 脚本来备份我的服务器,但是它正在创建空的 tar 存档和空的 sql 文件,我不知道为什么。有人能看到这里的问题吗?
#!/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 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 {} \;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您只是在 gzip 行上缺少 -c ,请尝试:
I think you are just missing a -c on the gzip line, try:
当您运行脚本时,您确定您有访问 SERVER_DIR="/var/www/vhosts/site.org" 的正确权限吗?
Are you sure you have right permission to access SERVER_DIR="/var/www/vhosts/site.org", when you run you script.
您正在
/backups
中创建,但从/backup
中删除旧备份。这对最终结果来说并不重要,但几个月后你的磁盘空间就会耗尽。我不确定在目录名和文件名中都包含日期是否有用,但这显然取决于您。
SQL 文件名也许应该有一个
.gz
后缀?使用
which
将 MYSQLDUMP 和 GZIP 放入变量中几乎没有用;如果可以在 PATH 中找到它们,shell 也会找到它们;或者更确切地说,如果 shell 找不到某些东西,那么which
也找不到。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 canwhich
.