标签:
#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
标签:
原文地址:http://www.cnblogs.com/qk2015/p/5124218.html