Simple batch file backup

By admin on 18/08/2007

I've just setup a very simple backup system that works over FTP. Windows only I'm afraid but i'm sure the equivilent isn't too hard on other platforms.

Tools

Firstly I used two very simple free/open source command line tools:

SSLFTP - simple FTP tool that works in passive mode or over secure connections.

and

7-zip - an open source zip file archiver.

Download and install the above on the machine you wish to backup from.

FTP Script

I then wrote the following script file to handle the FTP (saved as ftpscript.txt):

open ftpusername:ftppassword@ftp.host.address

del backup7.zip

rename backup6.zip backup7.zip

rename backup5.zip backup6.zip

rename backup4.zip backup5.zip

rename backup3.zip backup4.zip

rename backup2.zip backup3.zip

rename backup.zip backup2.zip

put c:\backupscripts\backup.zip

bye

All this is doing is taking a copy of seven days worth of backups and deleting the oldest then uploading the newest backup.

Backup Script

To decrease the time taken to FTP everything I use the following batch file to keep three days worth of backups locally, zip the files I want to backup and call the above FTP script (saved as daily.bat):

delete backup3.zip

rename backup2.zip backup3.zip

rename backup.zip backup2.zip

7z a -tzip d:\backupscripts\backup.zip d:\inetpub\wwwroot\

7z a -tzip d:\backupscripts\backup.zip d:\DB_Backup\

7z a -tzip d:\backupscripts\backup.zip d:\other_folders_to_keep\

sslftp -run ftpscript.txt

The 7z lines mean the following:

7z - execute the 7-zip exe a - add files -tzip - archive file type of zip d:\backupscripts\backup.zip - archive to add to (will create if does not exist) d:\backupscripts\backup.zip d:\other_folders_to_keep\ folder/files to add to archive

And thats it! All you do then is setup a scheduled task (Start, Settings, Control Panel, Scheduled Tasks) to run every night and you're done!

What no FTP?

If you don't have any FTP space you can backup across a network/internet connection and setup a PC as an FTP server using the excellent FileZilla.

UPDATE: see comments for some notes from William who suggests some alternative syntax if you have issues with the above and a more logical sequence to protect against things going wrong mid-process.