Sunday, April 17, 2016

Manage your local scripts via git on a shared directory of your NAS

Objective


I want to store all my scripts in a central place on my Synology NAS. I want to be able to easily add or alter any script on any computer within may local network. I want to be able to synchronize all my computer to have always the most recent version of any of my scripts installed. I don't want to use a public repository like GitHub or BitBucket for privacy reasons.

Motivation


At the moment, I use several computers to develop software. Those include a MacBookAir, a Linux workstation with several Virtual-Machines on it and a Laptop. On all those machines, virtual or physical I have a separate homeaccount with a bin folder where my scripts for daily work are located. I have a Synology NAS in my network where I backup all those scripts manually in a so called reference folder. I also synchronize my scripts manually, which is time consuming and kind a painful, because sometimes I even don't remember which of my local machines has the most recent version of a script stored at a certain point of time.

Prerequisites


  • Linux Mint 17.3 Rose
  • Synology DS209+II
  • git v1.9.1
  • nfs-shared directory on NAS

Solution


Create a new bare git repository


On my client machine (laptop with Linux Mint) I changed to the mounted directory (automount) from my NAS wher I want to create the bare git repository:

$> cd /mnt/DiskStation/data/home/cschmidt/

At the moment I have my reference directory containing all my scripts already in the bin folder there.

$> ls -la

drwxr-xr-x 3 cschmidt users 4096 Apr 16 22:54 .
drwxr-xr-x 3 cschmidt users 4096 Apr 16 22:54 ..
drwxrwxrwx 2 cschmidt users 4096 Apr 16 22:54 bin

Now I create the bare git repository named bin.git

$> git init --bare bin.git

Initialized empty Git repository in /mnt/DiskStation/data/home/cschmidt/bin.git/

Create a non-bare git repository in the folder where the reference scripts are actually stored.


Now I change back to the folder where my scripts are currently stored

$> cd bin
$> ls -la

drwxrwxrwx 2 cschmidt users 4096 Apr 16 22:54 .
drwxr-xr-x 4 cschmidt users 4096 Apr 17 2016 ..
-rwxrwxrwx 1 cschmidt users 82 Jul 20 2008 listfoldersize.sh
-rwxr-xr-x 1 cschmidt users 2193 Apr 16 18:20 mkscript.sh
-rwxr--r-- 1 cschmidt users 886 Sep 5 2015 renfiles.rb
-rwxrwxrwx 1 cschmidt users 671 Dez 3 2009 synapticsOnOff.sh
-rwxr--r-- 1 cschmidt users 1626 Jul 12 2015 wav2mp3.sh

Here I create a non-bare repository

$> git init

Initialized empty Git repository in /mnt/DiskStation/data/home/cschmidt/bin/.git/

Now I add all the scripts I already have, to the working copy and commit them all.

$> git add .
$> git status

On branch master

Initial commit

Changes to be committed:
(use "git rm --cached ..." to unstage)

new file: listfoldersize.sh
new file: mkscript.sh
new file: renfiles.rb
new file: synapticsOnOff.sh
new file: wav2mp3.sh

$> git commit -m "initial bunch of scripts"

[master (root-commit) 17879d2] initial bunch of scripts
5 files changed, 805 insertions(+)
create mode 100755 listfoldersize.sh
create mode 100755 mkscript.sh
create mode 100755 renfiles.rb
create mode 100755 synapticsOnOff.sh
create mode 100755 wav2mp3.sh

Push the commited scripts into the bare repository


So, now I tried to push the committed scripts into my bare host repository

$> git push

fatal: No configured push destination.
Either specify the URL from the command-line or configure a remote repository using

git remote add

and then push using the remote name

git push

Ok, I admit ... I forgot to add the remote repository to push into, so do so now

$> git remote add origin /mnt/DiskStation/data/home/cschmidt/bin.git

and again

$> git push

oops ...


fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

git push --set-upstream origin master

Ok, I see I could name the repository every time I want to push something into or I'll add it as a upstream.
Let's do the latter ...

$> git push --set-upstream origin master

Counting objects: 7, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 32.23 KiB | 0 bytes/s, done.
Total 7 (delta 2), reused 0 (delta 0)
To /mnt/DiskStation/data/home/cschmidt/bin.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.

Synchronize your script repository with your client


So, I want to have my recently commited scripts synchronized to my client

I go to my home account on my laptop and clone the remote repository into my bin.
(If you have already a bin folder in your home account, delete it first.)

$> cd ~
$> git clone /mnt/DiskStation/data/home/cschmidt/bin.git bin

Cloning into 'bin'...
done.

$> cd bin
$> ls -la

drwxrwxrwx 2 cschmidt users 4096 Apr 16 22:54 .
drwxr-xr-x 4 cschmidt users 4096 Apr 17 2016 ..
-rwxrwxrwx 1 cschmidt users 82 Jul 20 2008 listfoldersize.sh
-rwxr-xr-x 1 cschmidt users 2193 Apr 16 18:20 mkscript.sh
-rwxr--r-- 1 cschmidt users 886 Sep 5 2015 renfiles.rb
-rwxrwxrwx 1 cschmidt users 671 Dez 3 2009 synapticsOnOff.sh
-rwxr--r-- 1 cschmidt users 1626 Jul 12 2015 wav2mp3.sh

Yeah, everything worked fine!

No comments:

Post a Comment