cron表达式从数据库读取_我如何在没有Cron的情况下伪造计划的数据库转储

本文讲述了在Windows XP环境下,由于缺乏cron服务,作者如何利用MySQLDumper和用户登录时间来实现定时数据库备份。通过跟踪超级管理员用户的最后登录日期,如果登录发生在前一天,则执行mysqldump备份数据库。这种方法在没有Cron的情况下,巧妙地实现了类似的功能,同时确保敏感密码的安全。
摘要由CSDN通过智能技术生成

cron表达式从数据库读取

So there I was, stuck with a handful of problems. I had to find a way to use the platform I had available (Windows XP), keep sensitive passwords hidden from users, and enable periodic cron operations like backing up the database.

所以我在那里遇到了很多问题。 我必须找到一种方法来使用现有的平台(Windows XP),对用户隐藏敏感密码,并启用定期的cron操作,例如备份数据库。

My favorite server bundle XAMPP doesn’t come with cron services for Windows. I looked into UniServer, and though it looked pretty cool, it needed additional support to run on XP and I would need to change ionCube loaders for different platforms. Worse, as long as the user had access to the physical server, they could easily see the root MySQL password by clicking on the mysql tab in the UniServer GUI (and time was not on my hand to learn how to disable that).

我最喜欢的服务器捆绑包XAMPP没有Windows的cron服务。 我研究了UniServer ,尽管它看起来很酷,但它需要在XP上运行的额外支持,并且我需要为不同平台更改ionCube加载器。 更糟糕的是,只要用户可以访问物理服务器,他们就可以通过单击UniServer GUI中的mysql选项卡轻松地查看MySQL的根密码(并且没有时间来学习如何禁用该密码)。

I almost used WampServer, but then I remembered all the headaches it gave me the last time I used it.

我几乎用过WampServer ,但是后来我想起了上次使用它时给我带来的所有麻烦。

I came across an open-source program called MySqlDumper which I was happy to find, but which also wasn’t without its problems. When performing a database restoration for example, it was capable of displaying everything about all the databases on the server and offered options to drop them. The program was to be installed in various institutions and would use the same root password.

我遇到了一个名为MySqlDumper的开源程序,我很高兴找到它,但是它也不是没有问题。 例如,当执行数据库还原时,它能够显示服务器上所有数据库的所有内容,并提供删除它们的选项。 该程序将安装在各个机构中,并将使用相同的root密码。

I set my laptop aside and walked to the fridge, thinking of giving up on the cron idea during this first release of my project. But clients’ data backup can never be ignored. I opened the fridge and grabbed the closest bottle of juice; ignoring the glasses on the shelf, I took one big gulp of the juice that chocked me badly. As I turned looking around the boring room and trying to grasp my breath, suddenly an idea struck like lightening.

我把笔记本电脑放在一旁,走到冰箱前,想着在我的项目的第一个版本中放弃cron的想法。 但是,客户的数据备份永远不会被忽略。 我打开冰箱,拿起最近的一瓶果汁。 忽略架子上的眼镜,我取了一口大口的果汁把我塞得很厉害。 当我转过身在无聊的房间里试着屏住呼吸时,突然有一个想法像闪电一样突然出现。

My program required a accurate username and password to present its features to the user, and there is one predefined user who is idle most of the time and does just two things: wipe stale database entries and restore the database when needed. According to my login procedure, each time a user successfully logs in the system automatically updates the last login date to the current date. And that was the hint I desperately needed.

我的程序需要一个准确的用户名和密码来向用户展示其功能,并且有一个预定义的用户在大多数时间都是空闲的,并且仅做两件事:清除陈旧的数据库条目并在需要时还原数据库。 根据我的登录过程,每次用户成功登录系统都会自动将上次登录日期更新为当前日期。 这就是我迫切需要的提示。

The solution was to track the last login date of the idle superadmin user. If his last login was yesterday, then the system would call mysqldump to backup the database. So, I created the following code:

解决方案是跟踪空闲的超级管理员用户的最后登录日期。 如果他的上次登录是昨天,则系统将调用mysqldump备份数据库。 因此,我创建了以下代码:

<?php
define("MYSQL_DUMP", "C:\xampp\mysql\bin\mysqldump.exe");
define("MYSQL_DUMP_ARGS", "--user=user --password=pass --opt database");
define("MYSQL_DUMP_OUT", "..\system\" . date("l") . ".sql");

$result = $db->query("SELECT last_login FROM users WHERE username = 'superadmin'");
$check = $result->fetchColumn();
$result->closeCursor();

if ($check < strtotime(date("Y-m-d 00:00:00"))) {
    $backup = MYSQL_DUMP . " " . MYSQL_DUMP_ARGS . " > " .
        MYSQL_DUMP_OUT;
    exec($backup, $out);

    if ($_POST["username"] != "superadmin") {
        $db->query("UPDATE USER SET last_login = NOW() WHERE username = 'superadmin'");
    }
}

This handful of lines of code was my solution. The first part of the routine checks the the last login time of the superadmin account. If last login occurred today then the second part will be ignored. Otherwise, the backup is made and the last login time for the superadmin account is updated to today.

这几行代码是我的解决方案。 例程的第一部分检查超级管理员帐户的最后登录时间。 如果今天最后一次登录发生,则第二部分将被忽略。 否则,将进行备份并将超级管理员帐户的上次登录时间更新为今天。

In addition to possibly wiping out the entire database, the superadmin user also is able to restore backups, which is why I checked who was logging in before updating the last login time. When the superadmin account logs in regularly, his last login time will be updated by the login code, so either way the field will be updated for him.

除了可能清除整个数据库外,superadmin用户还能够还原备份,这就是为什么我在更新上次登录时间之前检查谁正在登录。 当超级管理员帐户定期登录时,其上次登录时间将由登录代码更新,因此无论哪种方式都将为他更新该字段。

It’s amazing how sometimes the most perplexing problems have a simple and elegant solution. One thing I do when I’m stack is to run through all of the processes of the program looking for the convenient hook or loophole. This particular solution may not be for everyone, but it works well enough for my problem and allowed me to use the existing platform that was available to me and still keep sensitive passwords out of site from general users. Thanks for letting me share, and if anything I hope it encourages you to find creative solutions in your own day-to-day programming problems.

有时最棘手的问题有一个简单而优雅的解决方案,真是令人惊讶。 当我使用堆栈时,我要做的一件事是在程序的所有过程中运行,以寻找方便的钩子或漏洞。 这个特定的解决方案可能并不适合每个人,但是它可以很好地解决我的问题,并允许我使用可用的现有平台,但仍使普通用户无法访问敏感密码。 感谢您与我分享,如果有什么希望,它鼓励您在自己日常的编程问题中找到有创意的解决方案。

Image via Africa Studio / Shutterstock

图片来自Africa Studio / Shutterstock

翻译自: https://www.sitepoint.com/faked-scheduled-dumps-without-cron/

cron表达式从数据库读取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值