码迷,mamicode.com
首页 > 数据库 > 详细

mysql backup

时间:2016-01-12 15:29:45      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "string.h"
#include <fcntl.h>
#include <sys/types.h>

void backup(void) { char name[20]; char cmd[256]; time_t now; struct tm *b; memset(name,0,20); memset(cmd,0,256); time(&now); b = gmtime(&now); strcat(name,"backup-"); sprintf(name + strlen(name),"%d",1900+b->tm_year); sprintf(name + strlen(name),"%d",b->tm_mon+1); sprintf(name + strlen(name),"%d",b->tm_mday); //printf("%s",name); //printf("\n"); strcat(cmd,"mysqldump --user=hlkj --password=hlkj --lock-all-tables --all-databases > "); strcat(cmd,name); strcat(cmd,".sql\n"); strcat(cmd,"tar zcvf "); strcat(cmd,name); strcat(cmd,".tar.gz "); strcat(cmd,name); strcat(cmd,".sql\n"); strcat(cmd,"mv -f "); strcat(cmd,name); strcat(cmd,".tar.gz /var/www/html/MySQL_Backup\n"); strcat(cmd,"rm -f "); strcat(cmd,name); strcat(cmd,".sql"); //printf("%d,%s",strlen(cmd),cmd); //printf("\n"); system(cmd); } int checkOnly() { const char filename[] = "./_lock"; int fd = open (filename, O_WRONLY | O_CREAT , 0644); int flock = lockf(fd, F_TLOCK, 0 ); if (fd == -1) { return 0; } if (flock == -1) { return 0; } return 1; } int main(int argc, char *argv[]) { int i=0; if(checkOnly()) printf("this running...... !\n"); else { printf("this is running,plese closed and restart !\nexit this!\r\n") ; exit(0); } while(1) { i++; sleep(3600); if(i>24) { i=0; backup(); } } return 0; }

 
//mysqldump --user="hlkj" --password="hlkj" --lock-all-tables --all-databases > backup.sql
//tar zcvf name.tar.gz backup.sql
//rm -f backup.sql
//mv  -f name.tar.gz  /var/www/html/MySQL_Backup/name.tar.gz

mysql backup

标签:

原文地址:http://www.cnblogs.com/qk2015/p/5124218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!