On my work PC I have several important files and folders that I would regularly want to backup. These are usually some MySQL database backend for a website, the website content itself, my Outlook .pst file and a folder containing important work documents. I wanted a backup script that would automatically backup these files regularly, and preferably store them offsite. There are of course many 3-rd party applications that do this well, and indeed SonicWALL provides CDP (continuous data protection) which does an admirable job and actually does it really easily. However, I always prefer finding open-source, or freeware alternatives
As a sidenote, I recently ran across an open source backup platform called Zmanda which looked quite cool, and I actually did manage to setup their proposed system in about 15 minutes. Read about them here.
Also, note that I am not actually talking about baremetal recover (that will be a good topic for another blog post, stay tuned) but of backing up certain files and applications.
Back to the subject at hand. At work we use Windows XP computers, so the scripts I wrote had to be .BAT files which dont need any other programming environment other than that provided by the humble windows shell. To be able to backup offsite, I setup my SheevaPlug to be an FTP server and modified my backup script to automatically connect to that server and transfer all data there. Later, due to security concerns (dont forget FTP sends everything in clear text), I decided to transfer everything using SCP (secure copy protocol) which basically uses SSH to transfer over files, making it much more secure.
Then came the question of compression. My compression client of choice has always been 7-zip. Its free, opensource, and has a scriptable component, making it an ideal choice.
Last came the question of backing up my .pst Outlook file. Since while working I am constantly using outlook, simply trying to copy and compress the .pst file resulted in an error since the file is locked and in use. To get around this, I decided to use window’s VSS, or Volume Shadow Service, which allows you to backup files which are in use. To this end, I found the very good, free, scriptable utility that leverages VSS called Hobocopy.
Of course, everything I outlined above can simply be done using the windows backup utility, but that wouldnt be half as fun now would it? Anyways for all those who prefer using the windows NT backup client, I included it at the end of the blog post…
So, on to the scripts themselves. I will comment on each line behind the # symbol. First comes a simple script that uses FTP, with no VSS:
del ftpbackuplog.txt
# delete any pre-exisiting logs
#I wanted to inlcude “versioning” into my backup system, that is, I wnated to keep the most current, and the before-last versions of my files. Basically, I wanted to keep two versions of my files in a round-robin fashion. One backup gets written, then another, then the older backup gets overwritten, and so on. I accomplished this by simply creating a text file with a specific name. If “ftpbackupv1.txt” was present, then write to the first backup, if “ftpbackupv2.txt” was present, then write to the second backup
#In this section I implement the above by detecting the presence ofone of the text files and setting appropriate variables
if exist C:\FTPbackup\ftpbackupv1.txt (
set var=1
set prev=2
ren ftpbackupv1.txt ftpbackupv2.txt
) else (
set var=2
set prev=1
ren ftpbackupv2.txt ftpbackupv1.txt
)
del heccpc%prev%.zip
#delete the previous backup, if it exists
echo %date% > ftpbackuplog.txt
# for logging purposes, log the date the processes started into a logfile
7z a heccpc%var%.zip “C:\Documents and Settings\d.vassallo\Desktop\DaveKBase” >> ftpbackuplog.txt
# use 7zip to backup the contents of a folder into a ZIP archive
echo %date% >> ftpbackuplog.txt
7z a heccpc%var%.zip “C:\Documents and Settings\d.vassallo\Desktop\ForFTP” >> ftpbackuplog.txt
#same as above
echo %date% >> ftpbackuplog.txt
ftp -s:ftpscript.txt lisadave.dyndns.org >> ftpbackuplog.txt
# the above line calls a microsoft FTP script. MS built in FTP client allows you to run a series of FTP commands as specified in a file. Read the above line as saying: “Run the FTP commands in ftpscript.txt, towards the FTP server located at lisadave.dyndns.org, and log everything to ftpbackuplog.txt
That’s all for the first script. The actual ftp script commands are stored in a seperate txt file, and contain the commands used by the ftp client. The first line is the username, the second line is the password, followed by normal FTP commands (look them up here)
ftp script txt:
dvas
passwoed
cd ftproot
put c:\FTPbackup\heccpc*
bye
—————————————————-
The second script is similar to the first, so i’ll skip over the parts which are similar. However in this script I know uses SCP and VSS:
del ftpbackuplog.txt
if exist C:\FTPbackup\ftpbackupv1.txt (
set var=1
set prev=2
ren ftpbackupv1.txt ftpbackupv2.txt
) else (
set var=2
set prev=1
ren ftpbackupv2.txt ftpbackupv1.txt
)
del heccpc%prev%.zip
# the above section is the same as the previous script
echo %date% > ftpbackuplog.txt
7z u heccpc%var%.zip “C:\Documents and Settings\dvassallo\Desktop\DaveKBase” >> ftpbackuplog.txt
# same as before
if exist “C:\Documents and Settings\dvassallo\Desktop\ForFTP\personal.pst” del “C:\Documents and Settings\dvassallo\Desktop\ForFTP\personal.pst”
HoboCopy “c:\inbox backup” “C:\Documents and Settings\dvassallo\Desktop\ForFTP” personal.pst
# use HoboCopy to make a backup of my pst file using VSS. the previous line deletes any pre-existing backups I took becasue HoboCopy complains if a backup already exists
echo %date% >> ftpbackuplog.txt
7z u heccpc%var%.zip “C:\Documents and Settings\dvassallo\Desktop\ForFTP” >> ftpbackuplog.txt
echo %date% >> ftpbackuplog.txt
7z u heccpc%var%.zip “C:\temp” >> ftpbackuplog.txt
echo %date% >> ftpbackuplog.txt
7z u heccpc%var%.zip “C:\wamp\www” >> ftpbackuplog.txt
#same as before
echo Starting SCP transfer at %date% >> ftpbackuplog.txt
pscp -P 443 -pw password heccpc%var%.zip dvas@192.168.52.3:/var/ftproot/SNWLback%var%.zip
# here I use the freeware pscp program (from the same people that wrote Putty) to use SSH to transfer my file. The syntax for the above is:
pscp -P [port number] -pw [password] [source_file] [username]@[hostname_IP]:[destination_file]
—————————————————-
Last but not least comes an example of bacing up a Joomla website using simple windows NTBACKUP (this has been changed to WBADMIN for windows vista and above):
mysqldump -h 10.71.20.21 -u root –password=pass joomla > supportweb.sql
# mysqldump backs up a database to an SQL file. The syntax is:
# mysqldump -h [host IP] -u [username] –password=[password] [database_name] > [destination SQL file]
ntbackup backup “@W:\swamp\bin\mysql\mysql5.1.36\bin\supportweb.bks” /J “SuppportWeb EMEA Backup” /m normal /SNAP:on /l:s /f “\\10.71.20.1\Backups\supportweb_complete.bkf”
# the above line instructs NTBACKUP to backup the files listed in the catalog file “supportweb.bks” to a file on a network share. The syntax is:
ntbackup backup “@[path to bks file]” /J [job name] /m [type of backup: normal, copy, differential, incremental and so on] /SNAP [use VSS] /l [type of log file] /f [path to destination folder]
For more info on the NTbackup syntax, have a looksie here
Any of the above scripts can be saved as .BAT files. I then use windows scheduler (found in the Control Panel) to run the .BAT file periodically, for example twice a week.