Pre-archived server backup via SFTP/SSH with WinSCP

When reviewing WinSCP I had mentioned its scripting functions and how issues with Cobian Backup FTP backup grew on me. Fetching hundreds of directories and small files over FTP can be quite slow and I discovered that it simply fails to complete at times.

So I thought about making archive of files on remote server and downloading that single archive. All nicely set up and automated with WinSCP.

Task details

Basically we need to:

  1. archive folder with data on remote server;
  2. download that archive and delete it afterwards.

What you need

FTP protocol handles file downloads nicely, but it is all it can do. For more advanced stuff SFTP and SSH are needed. Availability depends on hosting, if you are unsure ask about it. On my previous host it was extra, current one had it outright.

Other than that you need WinSCP to run the process and something to schedule backup (native Windows scheduler will do, I just created task in Cobian Backup for myself).

Connection settings

First save your account credentials in WinSCP and connect at least once to ensure it works and encryption key is saved. Also try Commands > Open terminal (Ctrl+T) to ensure you have access to shell.

Write down full path to folder you want backed up and path to temporary folder. Latter makes sense to generate backup in and must be not a folder accessible from web – bad idea to leave backups where someone else can access them.

Backup script

WinSCP has console mode and a lot of operations can be done with that. We will need to automate stuff so write down commands in text file, backup.txt in my case.

option batch on
option confirm off
open LiquidWeb
cd /home/rarst/
call tar -cz --exclude=*cache* -f /home/rarst/tmp/FTP-backup-$(date +%Y-%m-%d).tgz ./public_html/
cd /home/rarst/tmp/
get -delete FTP-backup* "c:\INSTALL\My Dropbox\Backup\"
exit
  1. first two lines suppress possible questions to user so script runs full auto;
  2. open connects to account you had set up earlier;
  3. cd moves to folder one level higher that what we backup;
  4. call runs Linux shell command:
    • tar is commonly used Linux archiver;
    • exclude directive makes it skip anything with cache in name, so things like page and images caches aren’t included;
    • first path instructs it where to save backup and generates file name with current date;
    • second path tells it what folder to archive;
  5. cd moves to folder where archive was created;
  6. get downloads created backup to local folder (I use my Dropbox) and deletes it on server;
  7. exit exits.

Schedule backup

Now that you have script you can execute it with command line argument:

WinSCP.exe /console /script=”backup.txt”

To make it run silently drop /console.

Depending on how you call the command you may need to use full paths for executable and backup file. Stuff it in scheduler of your choice and verify it works. It failed for me first time because I was away from computer and Comodo Firewall didn’t like Cobian Backup calling WinSCP.

Overall

Fast (went from 40 minutes to 3), convenient and secure. What’s not to like? :)

Related Posts

1 Comment

Comments are closed.