+ Post New Thread
Results 1 to 5 of 5
*nix Thread, SMB Backup in Technical; I have a Ubuntu Server and am wanting to have automatic backups of: 1) MySQL Database 2) Web directory (/var/www) ...
  1. #1

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241

    SMB Backup

    I have a Ubuntu Server and am wanting to have automatic backups of:

    1) MySQL Database
    2) Web directory (/var/www)

    These backups should run daily, automatically export the databases and then send the files to a SMB share on a NAS box I have.

    Can anybody provide a bit assistance in achieving this?

    EDIT: Preferably in a ZIP format of some kind and labelled with the date
    Last edited by Hightower; 10th March 2009 at 02:18 PM. Reason: Added request

  2. #2

    Geoff's Avatar
    Join Date
    Jun 2005
    Location
    Fylde, Lancs, UK.
    Posts
    11,802
    Thank Post
    110
    Thanked 583 Times in 504 Posts
    Blog Entries
    1
    Rep Power
    224
    Code:
    ### System Setup ###
    DIRS="/var/www/"
    BACKUP=/tmp/backup.$$
    NOW=$(date +"%d-%m-%Y")
    INCFILE="/root/zip-inc-backup.dat"
    DAY=$(date +"%a")
    FULLBACKUP="Sat"
    
    ### MySQL Setup ###
    MUSER="username"
    MPASS="**************"
    MHOST="localhost"
    MYSQL="$(which mysql)"
    MYSQLDUMP="$(which mysqldump)"
    ZIP="$(which zip)"
    
    ### SMB/CIFS Server Setup ###
    SMBD="/backup"
    SMBU="username"
    SMBP="**************"
    SMBS="terastation"
    SMBCLIENT="$(which smbclient)"
    
    ### Other stuff ###
    EMAILID="it_support@yourschool.sch.uk"
    
    ### Start Backup for file system ###
    [ ! -d $BACKUP ] && mkdir -p $BACKUP || :
    
    ### See if we want to make a full backup ###
    if [ "$DAY" == "$FULLBACKUP" ]; then
      SMBD='echo "$SMBD/full"'
      FILE="fs-full-$NOW.zip"
      $ZIP $BACKUP/$FILE $DIRS
    else
      i=$(date +"%Hh%Mm%Ss")
      FILE="fs-i-$NOW-$i.zip"
      zip $BACKUP/$FILE $DIRS -i @$INCFILE
    fi
    
    ### Start MySQL Backup ###
    # Get all databases name
    DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
    for db in $DBS
    do
     FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").zip
     $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $ZIP $FILE -@
    done
    
    ### Dump backup using FTP ###
    #Start FTP backup using ncftp
    smbclient -U"$SMBU" -P"$SMBP" $SMBS<<EOF
    mkdir $SMBD
    mkdir $SMBD/$NOW
    cd $SMBD/$NOW
    lcd $BACKUP
    mput *
    quit
    EOF
    
    ### Find out if backup failed or not ###
    if [ "$?" == "0" ]; then
     rm -rf $BACKUP
    else
     T=/tmp/backup.fail
     echo "Date: $(date)">$T
     echo "Hostname: $(hostname)" >>$T
     echo "Backup failed" >>$T
     mail  -s "BACKUP FAILED" "$EMAILID" <$T
     rm -f $T
    fi
    The above is a conversion of the tarball/ftp upload script I have here. I've only done it in my head but it probably works.

  3. Thanks to Geoff from:

    Hightower (10th March 2009)

  4. #3

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    Thanks for that Geoff. Can I ask for a little more help. I've changed the code as follows:

    Code:
    ### System Setup ###
    DIRS="/var/www/"
    BACKUP=/tmp/backup.$$
    NOW=$(date +"%d-%m-%Y")
    INCFILE="/root/zip-inc-backup.dat"    //IS THIS OK AS IS?
    DAY=$(date +"%a")
    
    ### MySQL Setup ###
    MUSER="username"
    MPASS="**************"
    MHOST="localhost"
    MYSQL="$(which mysql)"    //NOT SURE?
    MYSQLDUMP="$(which mysqldump)" //NOT SURE?
    ZIP="$(which zip)"    //NOT SURE?
    
    ### SMB/CIFS Server Setup ###
    SMBD="/backup"
    SMBU="username"
    SMBP="**************"
    SMBS="terastation"
    SMBCLIENT="$(which smbclient)"    //NOT SURE?
    
    ### Other stuff ###
    EMAILID="MY EMAIL ADDRESS"
    
    ### Start Backup for file system ###
    [ ! -d $BACKUP ] && mkdir -p $BACKUP || :
    
    SMBD='echo "$SMBD/full"'
    FILE="fs-full-$NOW.zip"
    $ZIP $BACKUP/$FILE $DIRS
    
    ### Start MySQL Backup ###
    # Get all databases name
    DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
    for db in $DBS
    do
     FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").zip
     $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $ZIP $FILE -@
    done
    
    ### Dump backup using SMB ###
    #Start SMB backup using ncftp
    smbclient -U"$SMBU" -P"$SMBP" $SMBS<<EOF
    mkdir $SMBD
    mkdir $SMBD/$NOW
    cd $SMBD/$NOW
    lcd $BACKUP
    mput *
    quit
    EOF
    
    ### Find out if backup failed or not ###
    if [ "$?" == "0" ]; then
     mail  -s "BACKUP SUCCESSFUL" "$EMAILID" <$T
     rm -rf $BACKUP
    else
     T=/tmp/backup.fail
     echo "Date: $(date)">$T
     echo "Hostname: $(hostname)" >>$T
     echo "Backup failed" >>$T
     mail  -s "BACKUP FAILED" "$EMAILID" <$T
     rm -f $T
    fi
    I've changed it so (i think) it will do a full backup every night. I've also put a few comments next to things I'm not sure about (//NOT SURE?). I'm just not sure what to put there so any more help would be appreciated.

    Also, what do I save this script as? A '.sh' extension?

  5. #4

    Geoff's Avatar
    Join Date
    Jun 2005
    Location
    Fylde, Lancs, UK.
    Posts
    11,802
    Thank Post
    110
    Thanked 583 Times in 504 Posts
    Blog Entries
    1
    Rep Power
    224
    That filename in the root home dir just tells zip which files have changed and it needs to backup for incrimental backups.

    the $(which somecommand) things are just an easy way of finding the executables.

    Yes, make it a .sh and executable.

  6. #5

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    Quote Originally Posted by Geoff View Post
    That filename in the root home dir just tells zip which files have changed and it needs to backup for incrimental backups.

    the $(which somecommand) things are just an easy way of finding the executables.

    Yes, make it a .sh and executable.
    Thanks for your help Geoff. I'm now sorted with a slightly adapted script. This version does a full backup each time (as opposed to a incremental), and also puts all files into an archive for easier storage.

    Code:
    ### System Setup ###
    DIRS="/var/www/"
    BACKUP=/var/backupscripts/backups/backup.$$
    NOW=$(date +"%d-%m-%Y")
    INCFILE="/root/tar-inc-backup.dat"
    DAY=$(date +"%a")
    
    ### MySQL Setup ###
    MUSER="USER"
    MPASS="PASSWORD"
    MHOST="localhost"
    MYSQL="$(which mysql)"
    MYSQLDUMP="$(which mysqldump)"
    GZIP="$(which gzip)"
    
    SMBCLIENT="$(which smbclient)"
    
    ### Start Backup for file system ###
    [ ! -d $BACKUP ] && mkdir -p $BACKUP || :
    SMBD="DIRECTORY ON SHARE"
    FILE="fs-full-$NOW.tar.gz"
    tar -zcvf $BACKUP/$FILE $DIRS
    
    ### Start MySQL Backup ###
    DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
    for db in $DBS
    do
     FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
     $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
    done
    
    ### Add all files to one archive for storage on NAS ###
    ### Call it by day name so it is overwritten each week ###
    tar -zcvf $DAY.tgz $BACKUP
    
    ### Dump backup using SMB ###
    smbclient //xxx.xxx.xxx.xxx/ShareName -U NETWORKUSER%PASSWORD -c "put $DAY.tgz"
    
    ### Delete backup files from local machine as they are now on NAS box ###
    rm -rf $BACKUP
    rm -rf $DAY.tgz

SHARE:
+ Post New Thread

Similar Threads

  1. Automount SMB share
    By CyberNerd in forum *nix
    Replies: 20
    Last Post: 10th October 2009, 01:42 PM
  2. Enable SMB on AD/DC Win Server2003
    By thegrassisgreener in forum Windows
    Replies: 1
    Last Post: 27th June 2008, 02:07 PM
  3. SMB Packets
    By AJT1 in forum Wireless Networks
    Replies: 6
    Last Post: 12th February 2008, 12:56 PM
  4. Replies: 4
    Last Post: 12th December 2006, 05:33 PM
  5. Weird SMB Error
    By MouseAT in forum Windows
    Replies: 18
    Last Post: 24th March 2006, 01:29 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •