Simple batch file backup

Published on August 18th, 2007 by DannyT. Filed under Best Practices, Hosting, Tutorials

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.

5 Responses to “Simple batch file backup”

  1. Mac

    August 23rd, 2007 at 8:01 pm

    This is good stuff… Thanks!

    Reply
  2. srinivas

    August 27th, 2007 at 11:23 am

    HI, I want to creat abatch file to up load a file to ftp.
    please help me

    Reply
  3. DannyT

    August 27th, 2007 at 11:27 am

    srinivas, the code above DOES upload a file to FTP. Re-read the section titled “FTP Script”

    Reply
  4. william

    April 7th, 2008 at 8:49 pm

    Thanks for the pointers. Couple of notes worth mentioning.

    1) Rename and Delete are really ren and del when one actually writes the script (or according to the documentation that I could find and what actually worked for me)

    2) your backup script has a bit of a flaw.
    I’d suggest this instead (rough draft)
    create newbackup.zip
    del 3
    rename 1 and 2
    rename new to 1
    ftp 1

    this way, if for some reason your backup fails, the batch will stop executing and you’ll still have something as a backup. Otherwise, you’ll be deleting and renaming before the script fails at the backup (and the subsequent upload). You’d hate to come back from a two week holiday with no backups.

    Reply
  5. Sohail

    June 5th, 2008 at 10:49 am

    thanx for the code.. hey man u are gr8 ..but i haev one more query?
    1. can we get the backup with time stamp and remove the older files?
    i mean instead of backup1.. can we have something like “backup_05june2008″
    “backup_12june2008″
    “backup_19june2008″
    “backup_26june2008″
    then
    backup_05june2008 should be deleted adn replaced by backup_2july2008

    can we get some suggestions

    Reply

Leave a Reply

About DannyT

The blog: danny-t.co.uk - General geek talk focusing on Rich Internet Applications, Microsoft and Adobe technologies and the web in general. The business: Moov2.com - RIA development agency Dan Thomas has been an Internet geek since circa 1994. He has been running Moov2 since 2003 and prior to that worked as a Flash developer for one of europes largest E-learning providers.

Copyright © 2010 Danny-T.co.uk

CSS Template By RamblingSoul | WordPress Theme by Theme Lab and Best Hosting.