Bash 备份脚本

发布于 2024-12-02 11:11:42 字数 520 浏览 0 评论 0原文

我正在尝试制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野却迷人 2024-12-09 11:11:42

我认为您只是在 gzip 行上缺少 -c ,请尝试:

$MYSQLDUMP -u $MYSQLUSER -p$MYSQLPASS --all-databases | $GZIP -c9 > $BACKUP_DIR/$NAME.sql.gz

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
雨轻弹 2024-12-09 11:11:42

当您运行脚本时,您确定您有访问 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.

别低头,皇冠会掉 2024-12-09 11:11:42

您正在 /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 can which.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文