Community Forums
Connect with us on LinkedIn
+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    1

    Default Cron job to back up SQL databases regularly

    I would like to set up a cron job to backup my MySQL databases.

    Has anyone written a perl script that you are using to generate these backups that they are willing to share?

  2. #2
    Member mickalo's Avatar
    Join Date
    Apr 2002
    Location
    N.W. Iowa
    Posts
    753

    Default

    Quote Originally Posted by JenkinsLaw
    I would like to set up a cron job to backup my MySQL databases.

    Has anyone written a perl script that you are using to generate these backups that they are willing to share?
    Here's one we use to backup databases for many of our customers, executed via a cronjob each nite
    Code:
    #!/usr/bin/perl
    use warnings;
    use strict;
    
    # Set connection values
    my $DATABASE   = 'database_name';
    my $DUMP_DIR   = '/path/to/backup_folder/';
    my $MYSQLADMIN = '/usr/bin/mysqldump';
    my ($DUMP_FILE,$DUMP_SCRIPT);
    ######################################
    
      $DUMP_FILE = $DUMP_DIR . $DATABASE . ".sql";
      if(-e "$DUMP_FILE") { unlink("$DUMP_FILE"); }
      $DUMP_SCRIPT = $MYSQLADMIN . " --defaults-file=/home/<username>/.my.cnf" . ' --opt' . " $DATABASE > $DUMP_FILE";
      system("$DUMP_SCRIPT");
    
      exit(0);
    If your fimilar with Perl, you can adjust it for your needs I always use the ~/.my.cnf so we don't need to pass username/passwords.

    Hope it helps
    Mickalo

    Thunder Rain Internet Publishing

    Providing Internet Solutions that work!
    Custom Perl and Database Programming

  3. #3
    Member
    Join Date
    Jun 2004
    Posts
    8

    Default

    Code:
    #! /usr/bin/env python2
    
    import os
    import sys
    import time
    
    database = sys.argv[1]
    location = sys.argv[2]
    
    location = location.replace("DAY", time.strftime("%d"))
    location = location.replace("MONTH", time.strftime("%m"))
    location = location.replace("YEAR", time.strftime("%y"))
    location = location.replace("HOUR", time.strftime("%H"))
    location = location.replace("MIN", time.strftime("%M"))
    location = location.replace("SEC", time.strftime("%S"))
    
    command = "mysqldump "+database+" > "+location+".sql"
    print command
    os.system(command)
    command = "tar -czvf "+location+" "+location+".sql"
    print command
    os.system(command)
    command = "rm -f "+location+".sql"
    print command
    os.system(command)


    crontab


    0 4 * * * path to above file dbtoback /home/dbtobackbackup_DAY-MONTH-YEAR.tar.gz

  4. #4
    cPanel Partner NOC cPanel Partner NOC Badge
    Join Date
    Jun 2003
    Posts
    647

    Default

    One of our users created a pretty decent guide for backing up MySQL databases, check out:

    http://www.classicwebdesign.com/db_backup/

Similar Threads & Tags
Similar threads

  1. Cron Job Back Up!!!
    By semoweb in forum Data Protection
    Replies: 0
    Last Post: 02-12-2010, 02:00 AM
  2. Cron Job to back up MySQL database
    By BMR777 in forum New User Questions
    Replies: 1
    Last Post: 01-30-2010, 06:24 PM
  3. Cron job to back up SQL databases regularly
    By JenkinsLaw in forum cPanel and WHM Discussions
    Replies: 3
    Last Post: 07-14-2004, 05:14 PM
  4. Replies: 4
    Last Post: 07-30-2003, 02:37 PM
  5. Does rolling back to a previous version of SQL wipe databases?
    By Trel in forum cPanel and WHM Discussions
    Replies: 4
    Last Post: 07-30-2003, 02:37 PM
Linkedin       Facebook       Twitter       RSS       Flickr       YouTube