Shell script to backup directories from Linux server to Windows 2000/NT Server

by on April 9, 2008 · 4 comments

  1. #!/bin/bash
  2. # Shell script to backup directories from Linux server to Windows 2000/NT Server.
  3. # Run it as follows
  4. # Scriptname /home backup abc123 //server2000/backup
  5. # Backup /home directory from Linux box to NT/2000 box called
  6. # 'server2000' in share called '/backup' with username
  7. # 'backup' and password 'abc123'
  8. # --------------------------------------------------------------------
  9. # This is a free shell script under GNU GPL version 2.0 or above
  10. # Copyright (C) 2005 nixCraft project
  11. # Feedback/comment/suggestions : http://cyberciti.biz/fb/
  12. # -------------------------------------------------------------------------
  13. # This script is part of nixCraft shell script collection (NSSC)
  14. # Visit http://bash.cyberciti.biz/ for more information.
  15. # -------------------------------------------------------------------------
  16.  
  17. # backup what?
  18. FROM=$1
  19. # NT Connection Info #
  20. # NT Username
  21. NTUSER=$2
  22. # NT Password
  23. NTPASSWD=$3
  24. # NT ShareName i.e //server/backup etc
  25. NTSHARE="$4"
  26.  
  27. # BackUpDir Name
  28. BACKDIR="$(hostname -s)"
  29.  
  30. # Local mount point
  31. MNT="/mnt/smbbox"
  32.  
  33. # Get date and time
  34. NOW=$(date +"%m-%d-%Y-%I_%M%P")
  35. # backup file name
  36. BAKFILE="backup.$NOW.tar.gz"
  37.  
  38. if [ "$#" != "4" ]; then
  39. echo "Syntax:"
  40. echo "$(basename $0) {Linux-directory} {NTusername} {NTpassword} {//NTserver/share-name}"
  41. exit 1
  42. fi
  43. # make sure $from do exits
  44. if [ ! -d $FROM ]; then
  45. echo "Backup source directory \"$FROM\" does NOT exist"
  46. exit 2
  47. fi
  48. #Create tar to backup first
  49. tar -czf /tmp/$BAKFILE $FROM
  50.  
  51. #Mount the smb to /mnt
  52. [ ! -d $MNT ] && mkdir -p $MNT || :
  53.  
  54. mount -t smbfs -o username=$NTUSER,password=$NTPASSWD $NTSHARE $MNT
  55.  
  56. [ ! -d $MNT/$BACKDIR ] && mkdir -p $MNT/$BACKDIR || :
  57.  
  58. # Copy new tar to ntbox
  59. cp /tmp/$BAKFILE $MNT/$BACKDIR
  60.  
  61. # Send sync aka force to write data before issuing umount
  62. sync
  63.  
  64. # issue umount
  65. umount $MNT


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • Mike Walsh

    [ ! -d $MNT/$BACKUPDIR ] && mkdir -p $MNT/$BACKDIR || :

    should be

    [ ! -d $MNT/$BACKDIR ] && mkdir -p $MNT/$BACKDIR || :

    no?

  • vivek

    Mike,

    Thanks for the heads up.

  • sergio

    I`ll look your web. Thanks for the script!

    Saludos!

  • Greg

    This post is kind of old. Are you still taking comments?

Previous Script:

Next Script: