Please support VectorLinux!

Author Topic: Bash script to automate backups from windows pc's to my vector box  (Read 2257 times)

larkl

  • Member
  • *
  • Posts: 30
Bash script to automate backups from windows pc's to my vector box
« on: September 16, 2007, 11:37:41 am »
I use my vector box to backup selected folders from my two windows pc's.  I wanted to automate this a bit, copying everything in windows explorer is kind of a drag.  here it is, thought others might find it useful.  I'll try to just put it in as code.  You'll have to copy it into a file, then change the file to an executable.  All the variables are near the top, you'll need to change the folder names and the user/password for the shares.  This example backs up four folders found in two different pc's.  It first deletes the oldest folder in the /home/user/backups folder.  The smbclient recurses through all the subfolders. 
Code: [Select]
#!/bin/bash
#backup script using smbclient
# LLL 091607
#/home/user1/scripts/mySMBBackup

FRMDIR='pc1/my_docs pc1/shared_folder pc1/my_music pc2/school_work'
TODIR='/home/user1/backups'
USR=user1
PW=password1

#Move to backup folder
if cd $TODIR
then echo "$TODIR located"
else echo "$TODIR not located"
exit 0
fi

#remove oldest backup
OLD=`ls -Ftr|grep /|head -1`
if rm -rf $OLD
then echo "Folder $OLD deleted"
else echo "Folder $OLD not deleted"
fi

#Make new folder
FLDR=$TODIR/`date +%Y%b%d_%M`
if mkdir $FLDR
then echo "Created folder $FLDR"
else echo "Folder $FLDR not created"
exit 0
fi



#Do backups
for ITEM in $FRMDIR
do

#move to top folder
if cd $FLDR
then echo "Moved to $FLDR"
else echo "Move to $FLDR failed"
exit 0
fi

#mk subdir
if mkdir -p $ITEM
then echo "subfolder $ITEM created"
else echo "subfolder $ITEM not created"
exit 0
fi

cd $ITEM

#get files
if smbclient //$ITEM -U$USR%$PW -c "lowercase; prompt; recurse; mask *; mget *"
then echo "`ls -r *|wc -l` files copied from $ITEM"
else echo "smbclient failed at $ITEM"
exit 0
fi

done


kidd

  • Packager
  • Vectorian
  • ****
  • Posts: 682
Re: Bash script to automate backups from windows pc's to my vector box
« Reply #1 on: September 16, 2007, 12:07:58 pm »
larkl, thanks for the script.

I'm sure it'll be useful to someone (I've recently moved and I only have 1pc here(VL, of course)).

Maybe this would be a good code to paste in our new wiki.  Many of us have self-made scripts to solve our daily repetitive tasks that can be useful to other people.

What do you think guys?

Oh, and welcome to VL larkl