Friday, March 6, 2015

Automatically backup your files as soon as UBD Flash or HDD is connected

One of a great tool available to sync file is SyncTool from Microsoft. You my download a copy free of cost and installation & use is self explanatory.

Now, I don’t want to open the sync tool every time manually when I connect the HDD. My objective is to take the backup as soon as the USB HDD or flash drive is connect just like time machine backup in MAC.

I wrote a powershell script which I scheduled with task scheduler while booting. This script keeps on running like a deamon and keep on checking for the USB connection. Once the correct USB is connect with predefined name, it starts the sync tool and start syncing for each partnership sequentially.

One more benefit is that this script keeps on running in background and when the backup is started and completed, a balloon notification is shown to the user along with the path to log files. You may need to change the line number as per your case.

 

clear
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
$TARGETDIR = $dir + "\log"


if(!(Test-Path -Path $TARGETDIR ))
{
    New-Item -ItemType directory -Path $dir -Name "log"
}
$logFile = $TARGETDIR + "\sync.log"


function baloon-notify($logpath, $title_tip, $title)
    {   write-host "logpath : " $logpath
        write-host "titl tip : "  $title_tip  
        write-host "Title : " $title
        [void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)#Remove any registered events related to notifications
        Remove-Event BalloonClicked_event -ea SilentlyContinue
        Unregister-Event -SourceIdentifier BalloonClicked_event -ea silentlycontinue
        Remove-Event BalloonClosed_event -ea SilentlyContinue
        Unregister-Event -SourceIdentifier BalloonClosed_event -ea silentlycontinue
        $notification = New-Object System.Windows.Forms.NotifyIcon
        $notification.Icon = [System.Drawing.SystemIcons]::Information
        $notification.BalloonTipTitle = $title_tip
        $notification.BalloonTipIcon = “info”
        $notification.BalloonTipText = $title + $logpath
        $notification.Visible = $True
        register-objectevent $notification BalloonTipClicked BalloonClicked_event `
        -Action {Invoke-Item $logpath} | Out-Null
        $notification.ShowBalloonTip(600)
    }


 



function Run-SyncToy ($backupPlan)
{
    $Exe = "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe"
    & $Exe -R #$backupPlan
}


Register-WmiEvent -Class win32_VolumeChangeEvent -SourceIdentifier volumeChange -ea silentlycontinue 
write-host (get-date -format s) " Beginning script..."
do{
    $newEvent = Wait-Event -SourceIdentifier volumeChange
    $eventType = $newEvent.SourceEventArgs.NewEvent.EventType
    $eventTypeName = switch($eventType)
    {
        1 {"Configuration changed"}
        2 {"Device arrival"}
        3 {"Device removal"}
        4 {"docking"}
    }
    write-host (get-date -format s) " Event detected = " $eventTypeName
    if ($eventType -eq 2)
    {
        $driveLetter = $newEvent.SourceEventArgs.NewEvent.DriveName
        $driveLabel = ([wmi]"Win32_LogicalDisk='$driveLetter'").VolumeName
        write-host (get-date -format s) " Drive name = " $driveLetter
        write-host (get-date -format s) " Drive label = " $driveLabel
        # Execute process if drive matches specified condition(s)
        if ($driveLetter -eq 'H:' -and $driveLabel -eq 'Ankit')
            {
            write-host (get-date -format s) " Starting task in 3 seconds..."
            start-sleep -seconds 3
            write-host (get-date -format s)" Sync Process started"
            $tip = "Auto Sync Started.Do not remove USB."
            $text = "SyncToy started syncing. Do not remove USB till next notification."
            baloon-notify $TARGETDIR $tip $text
            Run-SyncToy ("Media Sync") | out-file $logFile -Append -Force
            write-host (get-date -format s) " Sync Process Ended"
            $tip = "Auto Sync Completed"
            $text = "Path to log file ->"
            baloon-notify $logFile $tip $text
            }
    }
    Remove-Event -SourceIdentifier volumeChange
} while (1-eq1) #Loop until next event


Unregister-Event -SourceIdentifier volumeChange

 

Thursday, March 5, 2015

Updating Sickrage - PAIN

the other day I installed sickrage on my raspberry pi and today morning I found that there are 200+ commits are are not updated on my installation.

We I tried all sorts to update my installation but nothing worked out. Ultimately I decided to stay with the current version. BUT to my surprise, my sickrage installation stopped downloading any new episode.

I need to do something now. Below are the steps followed to fix the issue. (taking a backup of sickrage via its web UI is never a bad idea before starting.)



mv /home/pi/sickrage/ /home/pi/sickrage-backup

sudo git clone https://github.com/SiCKRAGETV/SickRage.git /home/pi/sickrage

sudo chown -R 777 /home/pi/sickrage

sudo chmod -R 777 /home/pi/sickrage

cp /home/pi/sickrage-backup/config.ini /home/pi/sickrage/

cp /home/pi/sickrage-backup/sickbeard.db /home/pi/sickrage/

mkdir /home/pi/sickrage/cache

cp -a /home/pi/sickrage-backup/cache/. /home/pi/sickrage/cache/

sudo service sickrage start



Followed by a system reboot the issue is resolved with all my data intact. 

Sunday, March 1, 2015

Installing sickrage on RASPBMC

sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install python-cheetah git-core -y
wget http://sourceforge.net/projects/bananapi/files/unrar_5.0.10-1_armhf.deb
sudo dpkg -i unrar_5.0.10-1_armhf.deb
sudo git clone https://github.com/SiCKRAGETV/SickRage.git /home/pi//sickrage
sudo chown -R 777 /home/pi/sickrage
sudo chmod -R 777 /home/pi/sickrage
python /home/pi/sickrage/SickBeard.py -d
sudo apt-get install upstart
sudo nano /etc/init/sickrage.conf
#author "HTPCGuides.com"
#description "Upstart Script to run SickRage as a service on Ubuntu/Debian based systems"

#Set username for the process. Should probably be what you use for logging in

start on runlevel [2345]
stop on runlevel [016]

respawn

exec su - pi -c "python /home/pi/sickrage/SickBeard.py"
sudo service sickrage start