Friday 16 October 2015

Timehack 101 - Automated Folder Backup To Dropbox



Every thought to yourself - 'hey me, looking good today, my life is great but I only have one problem, I wish I could setup my system to automatically backup a folder on my hard drive to my Dropbox account', well you are in luck because today I will guide you step by step on how you can backup a folder full of whatever (photos, documents, source code, other folders)  at a period of your choosing (daily, weekly, next lunar eclipse) on system start up.



Requirement

  • Windows OS XP or newer (Tested on Windows 10)

  • Dropbox software/account (Link) (Tested on v3.10.7)

  • WinRAR/7zip software to archive folder (Link / Link)

  • Internet connection for syncing to cloud



Setup Dropbox

Lets begin, first thing we need to do is download Dropbox and install it onto your computer system. Next we need to create a Dropbox account and make sure it is working so once you have installed the Dropbox software check that a new folder is created in the following location and the Dropbox website allows you to login and browse your newly created Dropbox account files using a web browser.
 C:\users\<username>\dropbox   
The Dropbox sync folder location on my system is shown above where <username> is replaced with your computer username, this will be used later on when we create the bat file below.

We also need to disable Dropbox launching on startup by itself so to disable Dropbox startup right click on the Dropbox tray icon, click the options wheel icon and then preferences option. In the Dropbox preferences window under the general tab uncheck the 'start Dropbox on windows startup' option. The reason for this will be explained fully in the section below when we create the bat file.



Note: Other cloud services could be used instead of Dropbox if they have a similar folder syncing application.



Install WinRAR

Next step is to install a file compression software. The most popular commercial one right now is WinRAR but the free alternative 7zip should work just fine. Please note that the examples below are for WinRAR and 7zip examples will need to be created on their own by the reader. Helpful links are provided below for this purpose.



Create Windows Bat File

Next we need to create a windows script /bat file that will be run every time (or other period of time) your computer starts up. Bat files contain commands that are understandable by our operating system(Windows) and they allow it to perform tasks for us among other things when the file is run.  Our bat file starts the Dropbox application, waits for the application to load up correctly then compresses your chosen folder into a zip file and copies this zip file into the Dropbox system sync folder.

For simplicity lets assume we have a folder in our c: drive called myDocs and we would like to make a backup of this everyday to Dropbox. We now need to create a bat file which for convenience we will create in c: drive as well. This file contains the information for running our compression software and creating the zip file from our myDocs folder and then copying this file to our Dropbox folder.


 

The text below is the complete contents of the Bat file that we need to create on our system. Copy this into notepad, make the changes mentioned below to suit your username and applications installed locations and save this file to an ideal location such as C:\mybatfile.bat. When saving this file make sure that the file name includes the .bat extension at the end of the name and the save as type option is changed from text documents to all types.
 @setlocal  
 @echo off  
 set path="C:\Program Files\WinRAR\";%path%  
 start /d "C:\Users\Manu\AppData\Roaming\Dropbox\bin" Dropbox.exe  
 timeout /t 30  
 winrar.exe a -afzip -ibck -r c:\Users\Manu\Dropbox\myDocs.zip c:\myDocs  
 echo done  
Now lets take a closer look at what the important lines in the bat file do and what we need to change to customise it for your system.
  set path="C:\Program Files\WinRAR\";%path%   
Set path lets windows know where to look when we use a command, in our case the WinRAR.exe file. Check where WinRAR was installed on your system and copy the full path of the folder here. In the case above WinRAR is installed in C:\Program Files\WinRAR folder but it maybe different on your system so make sure you check where WinRAR is installed on your system and change the location in the bat file if necessary.
  start /d "C:\Users\Manu\AppData\Roaming\Dropbox\bin" Dropbox.exe   
 timeout /t 30   
Remember above we disabled the Dropbox application starting up when our computers starts, well we did that so that we could start Dropbox by ourselves using the bat file. This insures that Dropbox had started before we copy our zip file into the sync folder and the files are synced as soon as possible to the cloud. The two lines above are to start Dropbox and then wait for 30 seconds till Dropbox has finished loading up on our system.
The location of the Dropbox application is C:\Users\<username>\AppData\Roaming\Dropbox\bin on my system, you will need to change it to the location on your system by changing <username> to the username on your system. It is recommended that you check to make sure that the dropbox.exe file is in that directory on your system using windows explorer to avoid any errors/problems.
 winrar.exe a -afzip -ibck -r c:\Users\Manu\Dropbox\myDocs.zip c:\myDocs   
This final line is the most important line as it is where all the work is done. We start WinRAR and tell it to zip the second specified folder location (C:\myDocs) and to copy this zip file to the first folder location which if your remember in the section above is the folder Dropbox created for us when it was installed. In my case this is C:\Users\<username>\Dropbox, this will be different on your system and you will need to change the <username> to the username you are using on your system. We have also specified the name of the zip file which in my case is myDocs.zip, you can change this to whatever you like as long as the .zip extension at the end is not changed i.e. importantfiles.zip, 123.zip, backup.zip.
a -afzip -ibck -r
To find more information on what each of these 4 parameters mean please follow the provided links at the end of the post.

Note: You can test the bat file by double clicking on it in the windows explorer where it is saved, it should create the zip file and copy it into the Dropbox sync folder.


Add New Task to Task Scheduler

Finally the last step is to create a windows task that is run at our preferred time. Most modern Windows operating systems including XP, 7, 8, 10 and newer  support task scheduling facilities. This allows for basic all the way up to advanced tasks to be scheduled by the user for whatever purpose they see fit. A quick step by step list of how to create the task is presented below.


Steps:

  • Start the task scheduler from the control panel under Administration tools.
  • From the Action menu select 'Create Task'.
  • Under General tab give this task an appropriate name and check 'run with highest privileges'.
  • Under Triggers tab click New and in the New Trigger window under 'Begin the task' select 'At startup'.
  • Under Actions tab click New and in the New Action windows 'Action' should be set to 'Start a program', click browse and browse to the location where you saved your bat file and select it (C:\mybatfile.bat in my example above).
  • Check that our newly created task is shown on the main task scheduler window under 'Task Scheduler Library'.


Once this final section is completed and the task is created all we need to do is restart our system so that windows can run our task/bat file and create our new zip file for us. If you have any problems write me a message below and I will try to help.

Now that you have successfully solved this problem and cast this distraction out of your life for good, you can go back to the more important things in your life like admiring your own reflection and other things that make you feel good for short periods of time.



Related Resources:

WinRAR command line information
http://comptb.cects.com/using-the-winrar-command-line-tools-in-windows/
http://acritum.com/software/manuals/winrar/
http://en.helpdoc-online.com/winrar_4/source/html/helpcommandlinesyntax.htm
http://en.helpdoc-online.com/winrar_4/_command_line_mode.html

7zip command line information
http://www.dotnetperls.com/7-zip-examples
https://sevenzip.osdn.jp/chm/cmdline/

How to create a bat file
http://www.wikihow.com/Write-a-Batch-File

General
https://www.organicweb.com.au/15111/general-technology/windows8-batch-backup/
http://codeformatter.blogspot.com.au/