#include #include #include #include /* mysql_user='******' mysql_pass='******' mysql='mysql' mysql_dump='mysqldump' config='config.txt' backup_base_dir='/backups' time=$(date +"%Y%m%d-%H%M%S") date=$(date +"%Y%m%d") year=$(date +"%Y") month=$(date +"%m") backup_dir="$backup_base_dir/$year/$month" mkdir -p $backup_dir $mysql_dump --no-create-db --disable-keys --add-drop-table --add-locks --create-options \ --quick --extended-insert --lock-tables --set-charset \ -h $host -u $mysql_user -p$mysql_pass $database $table | gzip > $backup_dir/$backup_file.sql.gz tend=$(date +%s) let tdiff=$tend-$tstart echo $(date +"%Y-%m-%d %H:%M:%S") " Backup of $database/$table on $host stored to $backup_file in $tdiff seconds." */ int load_conf( const char *filename, char **result ); int get_hosts( int s, char *data, char *hosts ); int get_dbs( int s, char *data, char *dbs ); char *replace(char *s, char old, char new); void chomp ( char *s ); void get_time( char *timestr ); int main( int argc, char **argv ) { char mdump[] = "mysqldump"; char dumpopts[] = "--no-create-db --disable-keys --add-drop-table --add-locks --create-options --quick --extended-insert --lock-tables --set-charset"; char dumpopts2[] = "-u ****** -p******"; char *cf; FILE *file; if ( argc != 2 ) /* argc should be 2 for correct execution */ { printf( "\n Usage: %s configfile\n\n", argv[0] ); return 1; } else { cf = argv[1]; } printf ("Loading config file: %s\n\n", cf); char *content; char hosts[1024]; char dbs[1024]; int size; int sizeh; int sized; size = load_conf( cf, &content ); sizeh = get_hosts ( size, content , hosts ); sized = get_dbs ( size, content, dbs ); // debug ! /* printf ("CONF CHARS FOUND: %d\n", size); printf ("HOST CHARS FOUND: %d\n", sizeh); printf ("DB CHARS FOUND: %d\n", sized); */ int a = 0; int b = 0; char *h[64]; char *d[64]; int i; for ( i=0; i<=sized-1; i++ ) { int x; char dline[128]; for ( x=0; dbs[i+x] != '\0'; x++ ) { dline[x] = dbs[i+x]; } dline[x] = '\0'; chomp(dline); d[b] = strdup(dline); b++; i = i + x; } for ( i=0; i<=sizeh-1; i++ ) { int x; char hline[128]; for ( x=0; hosts[i+x] != '\0'; x++ ) { hline[x] = hosts[i+x]; } hline[x] = '\0'; chomp(hline); h[a] = strdup(hline); a++; i = i + x; } int z; int y; for ( z = 0; z lines if ( strstr( *(d+y), *(h+z) ) != NULL ) { char *str; char *str1; char *dbname; char hostname[64]; char *p1; char *p2; str = strdup ( *(d+y) ); str1 = index( str, ':' ) + 1; p1 = str1; p2 = hostname; dbname = str1; while ( p1 <= index ( str1, ':' ) - 1 ) { *p2++ = *p1++; } *p2++ = '\0'; while ( index( str1, ':' ) != NULL ) { *dbname++ = *str1++; } char *dbtable = strdup( dbname ); char command[256]; int t = 1; for ( int c=0; c<= strlen ( dbtable ); c++ ) { if ( dbtable[c] == ',' ) { t++; } } while ( index ( dbtable, ',' ) !=NULL ) { replace ( dbtable, ',', '\0' ); } if ( t < 2 ) { char time_str[32]; get_time( time_str ); char *backup_file = strdup ( dbtable ); replace ( dbtable, '.', ' ' ); sprintf (command, "%s %s %s -h %s %s | gzip > %s.%s.%s.sql.gz", mdump, dumpopts, dumpopts2, hostname, dbtable, time_str, hostname, backup_file ); printf ("Total tables found: \"%d\"\n", t); printf ("RUNNING COMMAND: \"%s\"\n\n", command); system ( command ); } else { for ( ; t>0 ; t-- ) { char time_str[32]; get_time( time_str ); char *dbtable1 = dbname; char dbtable2[128], *pt1, *pt2; pt1 = dbtable1; pt2 = dbtable2; while( pt1 <= index( dbname, '\0' ) - 1 && *pt1 != ',' ) { *pt2++ = *pt1++; } *pt2++ = '\0'; char *backup_file = strdup ( dbtable2 ); replace ( dbtable2, '.', ' ' ); sprintf (command, "%s %s %s -h %s %s | gzip > %s.%s.%s.sql.gz", mdump, dumpopts, dumpopts2, hostname, dbtable2, time_str, hostname, backup_file ); printf ("Total tables found: \"%d\"\n", t); printf ("RUNNING COMMAND: \"%s\"\n\n", command); dbname = strchr ( dbname, ',' ) + 1; system ( command ); } } } } } } int load_conf( const char *filename, char **result ) { /* load config file into memory as a multi-dimensional array */ int size = 0; FILE *f = fopen( filename, "r" ); if ( f == NULL ) { *result = NULL; return -1; // -1 means file opening fail } fseek( f, 0, SEEK_END ); size = ftell( f ); fseek( f, 0, SEEK_SET ); *result = ( char * ) malloc( size+1 ); if ( size != fread( *result, sizeof( char ), size, f ) ) { free( *result ); return -2; // -2 means file reading fail } fclose( f ); ( *result )[size] = 0; return size; } int get_hosts( int s, char *data, char *hosts ) { /* parse all lines starting with INSTALL: */ int i; int z = 0; for ( i=0; i<=s; i++ ) { if ( data[i] == 'I' && data[i+1] == 'N' && data[i+2] == 'S' && data[i+3] == 'T' && data[i+4] == 'A' && data[i+5] == 'L' && data[i+6] == 'L' ) { int x; char line[128]; for ( x = 0; data[i+x] != '\n'; x++ ) { line[x] = data[i+x]; } line[x++] = '\0'; if ( strstr ( line, "mysql_backup" ) != NULL ) { char *subtext = index( line, ':' ) + 1; char subtext2[80], *p1, *p2; p1 = subtext; p2 = subtext2; while( p1 <= index( subtext, ':' ) - 1 ) { *p2++ = *p1++; } *p2++ = '\n'; *p2++ = '\0'; for ( int x=0; x<=strlen( subtext2 ); x++ ) { hosts[z] = subtext2[x]; z++; } } } } return z; } int get_dbs( int s, char *data, char *dbs ) { /* parse all lines begining with string MYSQL_BACKUP: */ int d = 0; int z; int y; for ( z=0; z<=s; z++ ) { char mline[128]; if ( data[z] == 'M' && data[z+1] == 'Y' && data[z+2] == 'S' && data[z+3] == 'Q' && data[z+4] == 'L' && data[z+5] == '_' && data[z+6] == 'B' && data[z+7] == 'A' && data[z+8] == 'C' && data[z+9] == 'K' && data[z+10] == 'U' && data[z+11] == 'P' ) { for ( y = 0; data[z+y] != '\n'; y++ ) { mline[y] = data[z+y]; } mline[y++] = '\0'; for ( int x=0; x<=strlen( mline ); x++ ) { dbs[d] = mline[x]; d++; } } } return d; } void chomp(char *s) { /* remove trailing carriage returns */ while(*s && *s != '\n' && *s != '\r') s++; *s = 0; } char *replace(char *s, char old, char new) { /* search and replace old with new in string s */ char *p = s; while(*p) { if(*p == old) *p = new; ++p; } return s; } void get_time( char *result ) { /* stores time in result formatted: YYYYmmdd_HHMMSS */ char buffer[256]; time_t curtime; struct tm *loctime; /* Get the current time. */ curtime = time (NULL); /* Convert it to local time representation. */ loctime = localtime (&curtime); strftime ( result, 256, "%Y%m%d_%H%M%S\n", loctime ); chomp( result ); }