Monday, July 26, 2010

Windows Batch Script to Write to a Daily Log File

Here is a Windows batch script to append a passed line of text to the specified file (prepending the text with a date/time stamp). The idea is to use it to capture your day's activities in a daily log format. It's especially useful when combined with a tool like Launchy.


@echo off
setlocal

set MYDOCSPATH=[replace]
set LOGSUBPATH=[replace]
set FILENAME=DailyLog
set FILEEXT=txt

:: Generate date/time stamp
for /F "tokens=1 delims=/ " %%G in ('echo %DATE%') do set WEEKDAY=%%G
for /F "tokens=2 delims=/ " %%G in ('echo %DATE%') do set MONTH=%%G
for /F "tokens=3 delims=/ " %%G in ('echo %DATE%') do set DAY=%%G
for /F "tokens=4 delims=/ " %%G in ('echo %DATE%') do set YEAR=%%G
set YMD=%YEAR%%MONTH%%DAY%
for /F "tokens=1 delims=:." %%G in ('echo %TIME%') do set HOUR=%%G
for /F "tokens=2 delims=:." %%G in ('echo %TIME%') do set MIN=%%G
for /F "tokens=3 delims=:." %%G in ('echo %TIME%') do set SEC=%%G
if %HOUR% LSS 10 set HOUR=0%HOUR%
set HMS=%HOUR%%MIN%%SEC%
set DATETIMESTAMP=%YMD%-%HMS%

set LOGFILE=%MYDOCSPATH%\%LOGSUBPATH%\%FILENAME%.%FILEEXT%
set MESSAGE=%WEEKDAY% %YEAR%-%MONTH%-%DAY% %HOUR%:%MIN%:%SEC% \\ %*
if "%*"=="" (
echo No log message provided. Displaying contents of log file.
) else (
echo Writing log message: %*

echo %MESSAGE% >> "%LOGFILE%"
)
echo ----------------------------------------------------------------------
type "%LOGFILE%"
echo ----------------------------------------------------------------------
pause

endlocal

Sunday, May 16, 2010

Ubuntu - Secure, Automated File Backup Using Dropbox and 7zip

Since switching to Ubuntu as my primary OS, I've been lax in setting up an automatic backup of important files. I finally got around to it this weekend, using a combination of cron (via Gnome Scheduler), 7zip, some simple shell scripting, and the Dropbox application for linux.

At this point, I'm not concerned about backing up every little thing. Just the file-based equivalent of "what would you grab if the house was on fire". So no music, videos, or large amounts of photos for this. And, of course, some of these files will be sensitive or personal in nature, so I want to make sure they're encrypted. Dropbox does a fine job of encrypting the files they store, but they're still just a password hack away from public access, so providing a second encryption password for sensitive data is at least a step towards more protection.

So let's get this set up...

First things first, I installed the Dropbox client for linux. Dropbox creates a folder in your home directory that is automatically backed up to their servers, and is synced among any other computers or devices connected to your account. They have clients for the three major platforms (Win/Mac/Lin) plus iPhone and Android apps. You can check out their website for instructions or more info.

Second, I installed 7zip (search "7zip" in the Ubuntu Software Center) to get the best compression rate. I don't want to eat up more of my limited Dropbox storage than I need to. I got some help on the 7zip command-line options from (what else?) the man page.

Next, I created a shell script to create a compressed and encrypted archive of any specified folder:

#!/bin/bash

if [ -z "$1" ]; then
echo usage: first parameter must be a target filename
exit
fi
if [ -z "$2" ]; then
echo usage: second parameter must be the folder to backup
exit
fi

FILENAME=$1
FILESTOCOMPRESS=$2/*
DESTINATION=~/Dropbox/Backup/[computername]
PASSWORD=[password]

7z a -mhe=on -mx=9 -p"$PASSWORD" ~/$FILENAME $FILESTOCOMPRESS
mv ~/$FILENAME $DESTINATION/$FILENAME

Note the use of the "-mhe=on" switch to encrypt the archive header, which includes the list of files in the archive. And also the use of "-mx=9" to enforce the highest level of compression.

I also created another script which calls this one multiple times, once for each folder I want backed up. I'm quite the shell script novice, so there are probably better ways to do this. Perhaps with a function and by reading the filename and a list files to archive out of a text file. I'll get around to it eventually (maybe).

Finally, I set up this script to run daily using Gnome Scheduler (a GUI frontend for cron). Search "Gnome Scheduler" in the Ubuntu Software Center. Here's a nice overview of the tool. For those who wish to apply their command-line kung fu directly to cron and the crontab, this page from the Ubuntu Community Documentation may be of use.

There are some things to keep in mind with this backup option. There are certainly more secure backup options available. Although Dropbox uses high-end encryption when it stores your files on their servers, anyone with your Dropbox account password can access your files through the Dropbox website. So your files are only as secure as your account password. Adding a second level of encryption with 7zip is a step in the right direction, but of course you should be sure not to use the same password for 7zip encryption and for your Dropbox account. All in all, though, this seems like a decent trade-off between security and ease-of-use.

Saturday, January 16, 2010

Setting up Eclipse with Android Development Tools in Ubuntu

To get set up for Android App development, I'm following the instructions from Google here:
http://developer.android.com/sdk/installing.html

I installed Eclipse through the Ubuntu Software Center (searched for "eclipse" and clicked install).

Also had to install eclipse-jdt (Java Development Tools) through the Synaptic Package Manager.
Also had to install eclipse-pdt (Plugin Development Tools) through Package Manager.
(Edit 11/11/2010: When going through this process again with Ubuntu 10.4, I used the Ubuntu Software Center instead of the Synaptic Package Manager. Also, I must have meant eclipse-pde - Plugin Development Environment, as I can't find eclipse-pdt listed as an available package.)

Through Eclipses "Help -> Install New Software" dialog, I had to add the basic software source for eclipse (don't know why this isn't set up by default) which is:
http://download.eclipse.org/releases/galileo/
and install "WST Server Adapters" which is apparently a prerequisite for the ADT plugin. Learned that from this site: http://groups.google.com/group/android-developers/browse_thread/thread/1da899d34557f2e8?pli=1

Now to try installing the android plugin (ADT) by adding:
https://dl-ssl.google.com/android/eclipse/
to the eclipse list of software sources.

Looks like that did it! ADT is installed... (now what's next?)

Tuesday, December 15, 2009

Launching Windows Explorer in a Specific Directory

Here's something I've wanted to do for ages in Windows: starting Explorer in a specific directory from a shortcut. The command-line is this:
C:\WINDOWS\explorer.exe /n,/e,c:\somepath

Note that the commas are necessary. Refer to this knowledge base article for info on the commane-line switches.

Sunday, December 6, 2009

Launch a VirtualBox Virtual Machine from a Shortcut

Found this link about creating a shortcut to launch a VirtualBox machine directly without having to first load the VirtualBox GUI. In short, the linux command-line is:
VBoxManage startvm <your virtual machine name>

Saturday, November 21, 2009

The Bigisi Project - High-quailty Themes for Ubuntu

Just discovered a website called the Bigisi Project, which provides some really high-quality themes for Gnome/Ubuntu. Check it out.

Saturday, November 14, 2009

ubuntu-restricted-extras

After mucking around with Amarok for about half an hour, I remembered that by default, Ubuntu doesn't include support for "non-free" codecs like mp3 or dvd playback. You have to install the ubuntu-restricted-extras package to enable these abilities. Full terminal command is:
sudo apt-get install ubuntu-restricted-extras
It's easy enough to do, but annoying that this isn't included by default. Still, it's a small price to pay to use free (as in speech) software. Especially when it's also free (as in beer).