View Single Post
  #5   (View Single Post)  
Old 1st March 2015
Mike-Sanders Mike-Sanders is offline
Fdisk Soldier
 
Join Date: Dec 2012
Posts: 52
Default

For quite awhile now I've wanted to backup files on my server -from my local Windows box-

Of course that requires logging into my BSD server, running a backup script, & then logging out. Here, that chore takes about 15mins. during which I'd typically read some news, or maybe play a hand of Golf solitaire. Not unpleasant, but it kept me glued to that spot for the duration... What I needed was a way to automate the process. Make the task totally 'hands-off', 'cuz hey - thats what computers are for no?

To do this I'd need to first run the remote backup script, & then (just to be safe), copy the remote backup to my Win box.

Here then is my strategy...
  • a host running sshd (secure shell)
  • the ability to run a command from my Win box on my server (ssh/scp)
  • lots of error checking to verify the integrity of the backup

Putty (a freeware suite of Windows tools including secure shell client, secure copy, secure ftp) is just the ticket. plink allows you to run a remote command from your localhost, while pscp allows you to copy files to/from a remote host.

With these tools ready, I only needed to copy the backup script to the server (first script), & then launch it from the Windows side (2nd script, 'remote-backup.cmd').

At anyrate, comments are always welcome =)

Save this snippet server-side...

Code:
#!/bin/sh

<<DOC

this script creates a dated tar/gzip archive
of all directories/files in your home directory
in the form of 'user-date.tgz' while leaving
older backup archives intact

to use...

- save script to your home directory as 'backup.sh'
- flip on script's exec bit 'chmod +x ~/backup.sh'
- run the script '~/backup.sh'

DOC

TGZ=$1
OLD=$USER-[0-9]*.tgz

[ -z $TGZ ] && {
   echo backup file not specified...
   exit 1
}

cd $HOME

tar cvzf $TGZ --exclude=$OLD ./ || {
   echo backup error
   exit 1
}

cd -

# eof
Save this snippet client-side...

Code:
@echo off

:: save script on windows as remote-backup.cmd
::
:: requires plink & pscp, available in the putty package at:
:: http://www.chiark.greenend.org.uk/~sgtatham/putty/
::
::
:: you'll need to change these 3 items...
::
:: - remote host address
:: - remote host login name
:: - remote host password

set HST=192.168.0.8
set USR=warby-parker
set PSW=chesseburger

:: note: date formats vary between locales...
:: this script uses the format: year, month, day
:: to determine your local date format invoke:
::
:: echo %date%
::
:: to parse the date follow this example...
::
:: set var=%date:~2,4%
:: echo %var%
::
:: where 'var' is a substring starting from the 2nd
:: character of the main-string, 4 characters wide

:: windows us-english date format *but double check*

set Y=%date:~10,4%
set M=%date:~4,2%
set D=%date:~7,2%
set TGZ=%USR%-%Y%%M%%D%.tgz

:: dont change anything below...

cls
echo REMOTE BACKUP INITIATED...
plink -pw %PSW% %USR%@%HST% backup.sh %TGZ%

if not errorlevel 0 (
   echo ERROR CREATING REMOTE BACKUP...
   exit /b 1
)

echo TRANSFERRING BACKUP TO LOCALHOST...
pscp -pw %PSW% %USR%@%HST%:%TGZ% c:/%TGZ%

if not errorlevel 0 (
   echo ERROR TRANSFERRING BACKUP TO LOCALHOST...
   exit /b 1
)

echo REMOTE BACKUP COMPLETE...

:: eof
__________________
www.tacoshack.xyz

Last edited by Mike-Sanders; 1st March 2015 at 03:44 PM.
Reply With Quote