Automated Backup and Download with WinSCP
Introduction
WinSCP allows you to access your Linux server using SFTP and SCP connections allowing you to see files like an FTP client but also been able to run a few Linux commands as well.
This document talks you through backing up your server and then using WinSCP to download the backup file to your home machine. With the setup we’re going to be doing it will allow you to back up a /home/user site or the whole server.
In order to implement backing up with WinSCP you will require WinSCP from http://winscp.net/eng/download.php#download2
Setting up WinSCP
If you have just downloaded WinSCP you will need to install it, unless you are using the portable version.
The first we need to do is add the server with your username and password.
Note: Passwords can be easily recovered and are not encrypted. If your computer is not secure then create a backup account on your server with limited privileges.
Click on the “New” Button at the top of the right hand side.

Enter your Server IP Address or domain in to the Host name box and also fill in the User name box and the Password box for your server. Then click the Save button.

A Box will come up warning you about your saved password. As mentioned earlier about passwords been easily recovered. Click Ok if your happy with this.

You are now promoted to enter a name for your saved session; this is what we will use to access your server details with the automated script. So make sure there are no spaces in the name and remember it for later.

Once you’ve clicked okay, you will be returned to the first window, but your added server will be in the box.

The only thing left to do is click Login and just make sure your session works.
Next we need to create a folder for the automation script to go in to and a folder for the archives.
Create a folder on your C drive and call it “Server_Backup”. Inside that folder create another folder called “Archives”. The script that will run the server backup will sit in “Server_Backup” and the archive files will be downloaded in to “Archives”.
Save the following code in to c:\Server_Backup\run.bat”
@echo off rem WinSCP Server Automated Backup. rem Created for Quantact.com rem Settings. rem Server Login set session=serverbackup rem Folder Settings. set server_folder=/var/www/html/ set local_folder=c:\Server_Backup\Archives\ rem filename settings rem auto_name will set the archive file to dd_mm_yyyy=h_m eg. 12_02_2010-17_58 set auto_name=1 rem manual_name is for when auto_name is set to 0. set manual_name=server_backup rem Path to winscp. set winscp_path=c:\Progra~1\WinSCP3\WinSCP3.com rem Log File 1 or 0. set logfile=0 rem DO NOT EDIT BELOW HERE rem ------------------------------------------------------------------ set hour=%time:~0,2% if "%hour:~0,1%"==" " set hour=0%time:~1,1% set file_auto=%date:~0,2%_%date:~3,2%_%date:~6,4%-%hour%_%time:~3,2% IF "%auto_name%"=="0" (set filename=%manual_name%) else (set filename=%file_auto%) echo option batch on > script.tmp echo option confirm off >> script.tmp echo open %session% >> script.tmp rem this checks to see if source is root or a folder. IF %server_folder%==/ (set source=/) else (set source=%server_folder:~1,-1%) rem if folder not root add -c switch. IF NOT %server_folder%==/ (set switch=-C / ) else (set switch=) rem if folder root then add --exclude=/proc IF %server_folder%==/ (set exclude=--exclude=/proc) else (set exclude=) rem v swtich is required to stop echo call tar -cvzf %server_folder%%filename%.tar.gz %switch%%source% --exclude=%server_folder%%filename%.tar.gz %exclude% >> script.tmp echo get %server_folder%%filename%.tar.gz %local_folder%%filename%.tar.gz >> script.tmp echo rm %server_folder%%filename%.tar.gz >> script.tmp echo exit >> script.tmp IF "%logfile%"=="1" (set filelog=/log=%local_folder%%filename%.txt) else (set filelog=) rem Execute script %winscp_path% /script=script.tmp %filelog% rem Delete temporary script del script.tmp
There are a number of settings that you can edit.
This setting should be set to the name of the session you created earlier
set session=serverbackup
server_folder is the folder on the server that should be backed up, with leading /
If you wish to back the whole server up then just put / for server_folder and it will backup the whole server, excluding the proc folder.
If your folder is in the home directory then its /home/username/ and that will backup everything in your home directory.
local_folder is the folder on your computer where the archives should be downloaded too.
set server_folder=/var/www/html/ set local_folder=c:\Server_Backup\Archives\
auto_name with this setting enabled, the name of the archive will be dd_mm_yyyy-h_m.
If this setting is set to 0 then the name of the archive will fall to the manual_name (see below)
rem auto_name will set the archive file to dd_mm_yyyy-h_m eg. 12_02_2010-17_58 set auto_name=1
If auto_name is set to 0 then the filename of the archive will be set to to this.
rem manual_name is for when auto_name is set to 0. set manual_name=server_backup
This setting is the path to WinSCP3.com on your computer, if you have a newer version then the number maybe different, but the location should be the name unless you have changed it from the defualt.
rem Path to winscp. set winscp_path=c:\Progra~1\WinSCP3\WinSCP3.com
This setting enables the backup to be logged. The log file will appear in the Archives folder.
rem Log File 1 or 0. set logfile=0
While testing this script I have noticed that if you have some particually large files, the script may come up with some thing like this.
Host has not answered for 15 seconds. Wait for another 15 seconds? Pressing 'Abort' button will close session. (A)bort, (R)etry: Abort Terminated by user. Session 'serverbackup' closed. No session. No session. No session.
To fix this problem if you load up WinSCP and click serverbackup then the load button, if you dont have "Advanced options" checked, then check it and click on "Connection".
All you need to do is increase "Server response timeout" to a higher number, I changed it to 300 seconds, which is 5 mins and for me that stopped the problem. however depending on how big your files are and how many you have you may need to increase that time out higher.
Setting up Automation
I recommend that before you set up this script to run automatically you should test it a few times to make sure you don’t have any problems with the script.
First you need to open Scheduled Tasks. Start -> Programs -> Accessories -> System Tools->Scheduled Tasks. Click on "Add Scheduled Task"

Click Next

Click On Browse

Navigate to your Server Backup folder and select "run.bat" and click Open.

Put in the name "Server Backup", so it can be identified in Scheduled Tasks. Next Select one of the options under Perform this task, for me I’ve selected “Weekly” as “Daily” is too often and “Monthly” is too long.

Next select a time when the script should run, because my computer is all the time, I’ve chosen a time when I know I’m not going to be at my computer, so it wont interrupt anything I am doing. I have also selected Sunday out of the days, again a time when I know I won’t be sitting here.

If you have a password for your account on your computer you will need to enter it so that the script will run as you.

That is the Task setup, my Task will run at 4:00am every Sunday.

As you can see the task is sitting there ready. In order to make sure this is working right click on Server Backup and click "run" this will run the script and just make sure its all working as it should be.

The start of the Script

The script downloading the archive after compressing the files.

And a quick look in archives folder to make sure it has downloaded the archive.

- Login to post comments
