mysqldump& gzip命令可使用crontab正确创建MySQL数据库的压缩文件 [英] mysqldump & gzip commands to properly create a compressed file of a MySQL database using crontab

查看:138
本文介绍了mysqldump& gzip命令可使用crontab正确创建MySQL数据库的压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使crontab正常工作时遇到问题.我想自动执行MySQL数据库备份.

I am having problems with getting a crontab to work. I want to automate a MySQL database backup.

设置:

  • Debian GNU/Linux 7.3(简陋)
  • MySQL服务器版本:5.5.33-0 + wheezy1(Debian)
  • 目录用户,backup和backup2具有755权限
  • MySQL db和Debian帐户的用户名相同

从外壳程序执行此命令

mysqldump -u user -p[user_password] [database_name] | gzip > dumpfilename.sql.gz

当我使用crontab -e将其放置在crontab中时

When I place this in a crontab using crontab -e

* * /usr/bin/mysqldump -u user -pupasswd mydatabase | gzip> /home/user/backup/mydatabase-backup-`date +\%m\%d_\%Y`.sql.gz >/dev/null 2>&1

每分钟都会在/home/user/backup目录中创建一个文件,但是有0个字节.

A file is created every minute in /home/user/backup directory, but has 0 bytes.

但是,当我将输出重定向到第二个目录backup2时,我注意到在其中正确压缩了正确的mysqldumpfile.我无法弄清楚我犯的错误是什么导致第一个目录中的文件为0字节,而第二个目录中的结果为预期的输出.

However when I redirect this output to a second directory, backup2, I note that the proper mysqldumpfile duly compressed is created in it. I am unable to figure what is the mistake that I am making that results in a 0 byte file in the first directory and the expected output in the second directory.

* * /usr/bin/mysqldump -u user -pupasswd my-database | gzip> /home/user/backup/mydatabase-backup-`date +\%m\%d_\%Y`.sql.gz >/home/user/backup2/mydatabase-backup-`date +\%m\%d_\%Y`.sql.gz 2>&1

我将不胜感激.

谢谢

推荐答案

首先执行mysqldump命令,并使用管道将生成的输出重定向.管道将标准输出发送到gzip命令中作为标准输入.在filename.gz之后是输出重定向操作符(>),它将继续重定向数据,直到最后一个文件名(保存数据的位置).

First the mysqldump command is executed and the output generated is redirected using the pipe. The pipe is sending the standard output into the gzip command as standard input. Following the filename.gz, is the output redirection operator (>) which is going to continue redirecting the data until the last filename, which is where the data will be saved.

例如,此命令将转储数据库并通过gzip运行它,数据最终将落入three.gz

For example, this command will dump the database and run it through gzip and the data will finally land in three.gz

mysqldump -u user -pupasswd my-database | gzip > one.gz > two.gz > three.gz

$> ls -l
-rw-r--r--  1 uname  grp     0 Mar  9 00:37 one.gz
-rw-r--r--  1 uname  grp  1246 Mar  9 00:37 three.gz
-rw-r--r--  1 uname  grp     0 Mar  9 00:37 two.gz

我的原始答案是将数据库转储重定向到许多压缩文件(不进行双重压缩)的示例. (因为我扫描了问题并严重错过了-对此感到抱歉)

My original answer is an example of redirecting the database dump to many compressed files (without double compressing). (Since I scanned the question and seriously missed - sorry about that)

这是重新压缩文件的示例:

This is an example of recompressing files:

mysqldump -u user -pupasswd my-database | gzip -c > one.gz; gzip -c one.gz > two.gz; gzip -c two.gz > three.gz

$> ls -l
-rw-r--r--  1 uname  grp  1246 Mar  9 00:44 one.gz
-rw-r--r--  1 uname  grp  1306 Mar  9 00:44 three.gz
-rw-r--r--  1 uname  grp  1276 Mar  9 00:44 two.gz

这是解释I/O重定向的好资源: http://www.codecoffee .com/tipsforlinux/articles2/042.html

This is a good resource explaining I/O redirection: http://www.codecoffee.com/tipsforlinux/articles2/042.html

这篇关于mysqldump& gzip命令可使用crontab正确创建MySQL数据库的压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆