生成含年月日的mysql数据库备份文件
来源:互联网 发布:淘宝的水印设置在哪 编辑:程序博客网 时间:2024/06/01 09:41
想在linux系统上做一个计划任务,每天23:59分左右对数据库进行备份,备份的文件名中包含当前时间的年月日(如:mydb-back-auto-20161109.sql)。
计划任务对应的执行文件如果用shell script写,比较简单,但是,由于是script的原因,代码都是可见的,对数据库进行备份,script中必定包含登录数据的账号和密码,所以利用script简便,但不安全。下面用C++写了一个。
mysqlbackup.cpp文件:
/** filenmae:mysqlbackup.cpp*/#include <iostream>#include <ctime>#include <sstream>#include <string.h>#include<stdlib.h>using namespace std;string datetimestr( ){ time_t now = time(0); tm *ltm = localtime(&now); ostringstream datetimeostr; ostringstream osYear,osYear1; ostringstream osMonth,osMonth1; ostringstream osDay,osDay1; ostringstream osHour,osHour1; ostringstream osMin,osMin1; ostringstream osSec,osSec1; osYear<< 1900 + ltm->tm_year; osMonth<< 1 + ltm->tm_mon; osDay<< ltm->tm_mday ; osHour<< ltm->tm_hour ; osMin<< ltm->tm_min ; osSec<< ltm->tm_sec; //如果月、时、分、秒的位数少于2位,则在前面补0; if (strlen(osMonth.str().c_str())<2)osMonth1<<"0"<<osMonth.str().c_str(); else osMonth1<<osMonth.str().c_str(); if (strlen(osDay.str().c_str())<2)osDay1<<"0"<<osDay.str().c_str(); else osDay1<<osDay.str().c_str(); if(strlen(osHour.str().c_str())<2)osHour1<<"0"<<osHour.str().c_str(); else osHour1<<osHour.str().c_str(); if(strlen(osMin.str().c_str())<2)osMin<<"0"<<osMin.str().c_str(); else osMin1<<osMin.str().c_str(); if(strlen(osSec.str().c_str())<2)osSec1<<"0"<<osSec.str().c_str(); else osSec1<<osSec.str().c_str(); //datetimeostr<<osYear.str().c_str()<<osMonth1.str().c_str()<<osDay1.str().c_str()<<osHour1.str().c_str()<<osMin1.str().c_str()<<osSec1.str().c_str(); datetimeostr<<osYear.str().c_str()<<osMonth1.str().c_str()<<osDay1.str().c_str(); return datetimeostr.str();//返回当前年月日的字符串,如20161109}int main(){ ostringstream osfilename,oscommand; osfilename<<"mydb-back-auto-"<<datetimestr()<<".sql";//构造mysql备份文件的文件名;如:mydb-back-auto-20161109.sql oscommand<<"mysqldump -uroot -p123456 --compact mydb2 >"<<osfilename.str(); system(oscommand.str().c_str());}源码下载地址:https://github.com/yangboduan/mysqlbackup
0 0
- 生成含年月日的mysql数据库备份文件
- bat 命令行方式生成带有日期的MSSQL数据库备份文件
- 数据库oracle--PL/SQL的使用-禁止生成备份文件 .~sql
- mysql怎样利用对所有数据库进行备份的备份文件只恢复某一个数据库
- java如何指点年月日去生成时间,把生成的毫秒数存入到数据库
- 让linux每天定时备份MySQL数据库并删除五天前的备份文件
- linux用mysqldump每天定时备份MySQL数据库并删除五天前的备份文件
- 让linux每天定时备份MySQL数据库并删除五天前的备份文件
- linux定时备份MySQL数据库并删除七天前的备份文件
- 让linux每天定时备份MySQL数据库并删除五天前的备份文件
- linux定时备份MySQL数据库并删除七天前的备份文件
- linux定时备份MySQL数据库并删除七天前的备份文件
- 让linux每天定时备份MySQL数据库并删除五天前的备份文件
- linux定时备份MySQL数据库并删除七天前的备份文件
- Linux下每天定时备份mysql数据库并删除7天前的备份文件
- 导入数据库的备份文件(Data.mdf)
- 删除指定日期的MYSQL备份文件
- 提问:恢复Mysql的备份文件失败
- ffmpeg音频编码
- HDU 1175 连连看 DFS+搜索+方向
- Android项目版本管理浅析
- 表的内连接、外连接(左连接与右连接)
- linux下使用read函数实现的按行读取readline
- 生成含年月日的mysql数据库备份文件
- SVN 入门: 将已有文件目录加入svn版本控制
- [git]用pelican搞一个自己的blog(已完成)
- MySQL中的小技巧(不断更新中)
- 解决 Error:No suitable device found: no device found for connection "System eth0"
- C++命名空间
- JS DOM insertAfter方法
- redhat的安装
- debian 8.x 有用的源(163)