Tuesday, April 29, 2014

Working With Golang Array Slices

In the Go language a Slice is equivalent to the way a Perl/Python developer might think of an Array.  Yes, there is an actual Array under the covers of a Slice, but you cannot do much with them. So in Go, most people works with a Slice.  So if you are new to Go and coming from Perl/Python or the like, when you think you want an Array, you really want a Slice.  

Example
package main

import (
 "fmt"
)

func main() {
 
 // This is how you would declare and initialize a Slice at the same time.
 var aSliceOfStrings1 = []string{"a", "b", "c"}
 fmt.Println(aSliceOfStrings1[1])
 // This will print "b"


 // This is a shorthand way of declaring and initializing a Slice at the same time.
 aSliceOfStrings2 := []string{"c", "d",}
 fmt.Println(aSliceOfStrings2[1])
 // This will print "d" 

 
 // To create a slice but not initialize it you would use the make function.  This allows to define the
 // size and the optional capacity (defaults to length) at the same time.
 aSliceOfStrings3 := make([]string, 5, 10)
 aSliceOfStrings3[0] = "a"
 aSliceOfStrings3[1] = "b"
 aSliceOfStrings3[2] = "c"
 aSliceOfStrings3[3] = "d"
 aSliceOfStrings3[4] = "e"
 fmt.Println(aSliceOfStrings3[0])
 // This will print "a"


 // Append a Slice with more data
 aSliceOfStrings3 = append(aSliceOfStrings3, "f", "g", "h")
 fmt.Println(aSliceOfStrings3[6])
 // This will print "g"

 // Get the length and capacity of a Slice
 l := len(aSliceOfStrings3)
 fmt.Println("Length: ", l)
 // This will print "Length: 8"

 c := cap(aSliceOfStrings3)
 fmt.Println("Capacity: ", c)
 // This will print "Capacity: 10"

 // Copy an element of a Slice
 i := aSliceOfStrings3[1]
 fmt.Println(i)
 // This will print "b"


 // Copy part of a Slice in to another Slice, end needs to be what you want + 1 so if we want [2:4] we need to say [2:5]
 // You can also say [:] to mean the entire Slice. 
 // [:2] means the start of the Slice to the second element. 
 // And [2:] to mean from the second element to the end of the Slice.
 anotherSliceOfStrings1 := aSliceOfStrings3[2:5]
 fmt.Println("Length: ", len(anotherSliceOfStrings1))
 fmt.Println("Capacity: ", cap(anotherSliceOfStrings1))
 fmt.Println(anotherSliceOfStrings1[0])
 fmt.Println(anotherSliceOfStrings1[2])
 // This will print "Length: 3"
 // This will print "Capacity: 8" since we removed the first 2 elements of the original Slice
 // This will print "c" and "e"

 anotherSliceOfStrings2 := aSliceOfStrings3[:2]
 fmt.Println("Length: ", len(anotherSliceOfStrings2))
 fmt.Println("Capacity: ", cap(anotherSliceOfStrings2))
 fmt.Println(anotherSliceOfStrings2[0])
 fmt.Println(anotherSliceOfStrings2[1])
 // This will print "Length: 2"
 // This will print "Capacity: 10" the start of the Slice did not change so the capacity is the same
 // This will print "a" and "b" 

 anotherSliceOfStrings3 := aSliceOfStrings3[4:]
 fmt.Println("Length: ", len(anotherSliceOfStrings3))
 fmt.Println("Capacity: ", cap(anotherSliceOfStrings3))
 fmt.Println(anotherSliceOfStrings3[0])
 fmt.Println(anotherSliceOfStrings3[1])
 // This will print "Length: 4"
 // This will print "Capacity: 6" the start of the Slice changed so the capacity did as well
 // This will print "e" and "f"
}




Passing Slices to Functions
It's important to understand that even though a slice contains a pointer, it is really a value that under the covers is a struct value holding a pointer and a length. It is not a pointer to a struct. 

This matters when you pass a Slice to a function.  When you pass a Slice to a function you can modify the values in the Slice in the function and they will keep, however,  you can not modify the header (aka the length and capacity) as the header was passed by value not as a pointer.  If you want to modify the header, then you need to pass the Slice as a pointer not as a value.    You pass a Slice as a pointer by preceding it with an “&”.  Here is an example to replicate the "shift" function in Perl.

package main

import ("fmt")

var DEBUG int = 0

// This function tries to replicate the shift function in Perl by removing the first
// element of an slice and returning it
func Shift (pToSlice *[]string) string {
    sValue := (*pToSlice)[0]
    if DEBUG == 1 {
        fmt.Println("The slice before the shift: ",  *pToSlice)
    }
    *pToSlice = (*pToSlice)[1:len(*pToSlice)]
    
    if DEBUG == 1 {
        fmt.Println("The slice after the shift: ",  *pToSlice)
    }
    return sValue    
}


func main() {
    var a = []string{"1", "2", "3", "4"}
    for i := range a {
        fmt.Println(a[i])
    }
    // This will print 1, 2, 3, 4


    test := Shift(&a)
    fmt.Println("This is the shifted value: ", test)
    // This will print 1


    test2 := Shift(&a)
    fmt.Println("This is the shifted value: ", test2)
    // This will print 2
    
    for i := range a {
        fmt.Println(a[i])
    } 
    // This will print 3, 4
}




 

Wednesday, April 23, 2014

Creating Snapshots in Linux / MacOSX

Here is a script I wrote back in the 1999 time frame to perform daily, weekly, monthly snapshots of critical directories.  This scripts is designed for filesystems that support the notion of hard links, aka Linux and MacOSX. (The Windows filesystems do not support hard links and thus this will not work on Windows).

This script is usually called from /etc/crontab and requires that you have rsync installed on the system.  At the top of the actual script code is a section that you will need to update to specify where you want the backups to go and what directories you want to backup.


#!/usr/bin/perl
#
#
####################################################################
# Ideas taken from Mike's handy rotating-filesystem-snapshot       #
# utility, http://www.mikerubel.org/computers/rsync_snapshots/     #
# and from code written by Eric Ross                               #
####################################################################
#
####################################################################
# This program is not guaranteed to work at all, and by using this #
# program you release the author of any an all liability.          #
#                                                                  #
# You may use this program so long as this notice, disclaimer and  #
# comment box remain intact and unchanged.                         #
#                                                                  #
# Please send me all bugs and patches.                             #
#                                                                  #
# Written by Bret Jordan                                           #
# jordan at open1x littledot org                                   #
####################################################################
#
#
#
# Example /etc/crontab
# Every night at 1:01 make rsync copy and then give plenty of time before snapshots
# 01 1  * * *     root  /local/bin/snapshot rsync
#
# Every night at 2:01 make snapshots
# 01 2  * * *     root  /local/bin/snapshot snap 7 daily
#
# Every Sunday Morning at 3:01 make weekly snapshot
# 01 3  * * 7     root  /local/bin/snapshot snap 4 weekly
#
# First day of every month at 4:01 make monthly snapshot
# 01 4  1 * *     root  /local/bin/snapshot snap 12 monthly


use 5.8.8;
use strict;
use warnings;

our $VERSION = "0.02";
$VERSION = eval $VERSION;

my $sType = shift;
my $iHistoryLength = shift; 
my $sFreq = shift;


########################
# BEGIN THINGS TO UPDATE

my $sSnapDir = "/backups/snapshots";
my $sCurrentBackupDir = "$sSnapDir/backup.current";
my @aDirsToBackup = qw (/data/home/);

# END THIGNS TO UPDATE
########################


unless ($sType eq "rsync" || $sType eq "snap")
{ &ShowSyntax(); }


if ($sType eq "snap")
{
    unless ($iHistoryLength =~ /^\d+$/) 
    { &ShowSyntax(); }

    unless ($sFreq eq "hourly" || $sFreq eq "daily" || $sFreq eq "weekly" || $sFreq eq "monthly") 
    { &ShowSyntax(); }
}


sub ShowSyntax
{
    print "Please run command with ./snapshot <rsync snap=""> <historylength> <freq name="">\n";
    print "Example: ./snapshot 7 daily\n";
    print "Example: ./snapshot 24 hourly\n";
    die;
}

# Lets make sure we are running at root
unless ( $< == 0 ) { die "This program must be run as root\n"; }

# Do we have write access to the snap directory?
unless ( -w $sSnapDir ) { die "This user does not have write access to $sSnapDir\n"; }


if ($sType eq "rsync") 
{
    #----------------------------------------
    # Rsync Command
    #----------------------------------------
    #-a = archive mode which is -rlptgoD (no -H,-A,-X)
    #
    #-r = recurse in to directories
    #-l = copy symlinks as symlinks
    #-p = preserve permissions
    #-t = preserve modification times
    #-g = preserve group
    #-o = preserve owner
    #-D = preserve device files and special files

    foreach (@aDirsToBackup) { system("rsync -vax --delete $_ $sCurrentBackupDir"); }

    # Touch the directory so the date time is right
    system("touch $sCurrentBackupDir");
}
elsif ($sType eq "snap")
{
    # Step 1: Delete oldest snap shot and increase the age of the rest
    my $i = $iHistoryLength;
    while ($i > 0)
    {
    my $sCurrentDir = "$sSnapDir/backup.$sFreq.$i";

    # If index equals the max length of the history, then delete that directory
    if ( $i == $iHistoryLength ) { system ("rm -rf $sCurrentDir"); }
    else 
    {  
        my $j = $i + 1;
        my $sNewDir = "$sSnapDir/backup.$sFreq.$j";
        if ( -d $sCurrentDir ) { system ("mv $sCurrentDir $sNewDir"); }
    }
    $i--;
    }

    # Step 2: Make a hard link from the backup.current to backup.daily.1
    unless ( -d $sCurrentBackupDir ) { die "The current backup directory does not exist\n"; }
    system("cp -al $sSnapDir/backup.current $sSnapDir/backup.$sFreq.1");
}