aboutsummaryrefslogtreecommitdiff
path: root/files/sync-media
diff options
context:
space:
mode:
Diffstat (limited to 'files/sync-media')
-rwxr-xr-xfiles/sync-media113
1 files changed, 113 insertions, 0 deletions
diff --git a/files/sync-media b/files/sync-media
new file mode 100755
index 0000000..f80f1e8
--- /dev/null
+++ b/files/sync-media
@@ -0,0 +1,113 @@
+#!/bin/bash
+#
+# sync-media assets using git-annex
+#
+
+VOLUME="/media/usb"
+CACHE="/var/cache/media"
+DOMAIN="`facter domain`"
+MEDIA="media.$DOMAIN"
+INCOMING="$CACHE/incoming"
+WHOAMI="`whoami`"
+
+# Set sudo config
+if [ "$WHOAMI" != 'root' ]; then
+ sudo="sudo"
+fi
+
+# Commit changes
+if [ -d "$CACHE" ]; then
+ # Fix cache permissions
+ #echo "Fixing $CACHE permissions..."
+ #$sudo find $CACHE -type f -exec chmod 644 {} \;
+ #$sudo find $CACHE -type d -exec chmod 755 {} \;
+
+ # Fix incoming permissions
+ if [ -d "$INCOMING" ]; then
+ echo "Fixing $INCOMING permissions..."
+ $sudo find $INCOMING -type f -exec chmod 664 {} \;
+ $sudo find $INCOMING -type d -exec chmod 775 {} \;
+ $sudo chown -R $WHOAMI $INCOMING/*
+ fi
+
+ # Add and update local repositories
+ for folder in `ls $CACHE`; do
+ if [ -d "$CACHE/$folder/.git/annex" ]; then
+ (
+ cd $CACHE/$folder
+ echo "Syncing $CACHE/$folder..."
+ git annex add .
+
+ # Adding hidden files and symlinks, find version
+ #find -name '.*' -exec git annex add {} \;
+ #find -type l -exec git add {} \;
+
+ # Adding hidden files and symlinks, git+for version
+ #for file in `git status --porcelain -u | sed -e 's/?? //'`; do
+ # if [ -h "$file" ]; then
+ # git add $file
+ # else
+ # git annex add $file
+ # fi
+ #done
+
+ # Adding hidden files and symlinks, git+while version
+ git status --porcelain -u | sed -e 's/?? //' | while read file; do
+ if [ -h "$file" ]; then
+ git add "$file"
+ else
+ git annex add "$file"
+ fi
+ done
+
+ git annex sync
+ git annex fsck --fast
+ git annex unused
+ git annex dropunused 1-1000
+ )
+ fi
+ done
+
+ # Check storage media
+ MOUNT="`mount | grep $VOLUME`"
+ if [ ! -z "$MOUNT" ]; then
+ DRIVE="$(basename `echo $MOUNT | awk '{ print $1 }'`)"
+ fi
+
+ if [ ! -z "$DRIVE" ]; then
+ if [ ! -d "$VOLUME/$MEDIA" ]; then
+ echo "Folder $VOLUME/$MEDIA does not exist..."
+ else
+ for folder in `ls $CACHE`; do
+ if [ -d "$CACHE/$folder/.git/annex" ]; then
+ if [ ! -d "$VOLUME/$MEDIA/$folder" ]; then
+ (
+ cd $VOLUME/$MEDIA
+ echo "Initializing $VOLUME/$MEDIA/$folder..."
+ git clone $CACHE/$folder && cd $folder && git annex init $DRIVE && \
+ cd $CACHE/$folder && git remote add $DRIVE $VOLUME/$MEDIA/$folder
+ )
+ fi
+ fi
+ done
+ fi
+ fi
+fi
+
+# Retrieve changes at media volumes
+if [ -d "$VOLUME/$MEDIA" ]; then
+ for folder in `ls $VOLUME/$MEDIA`; do
+ if [ -d "$VOLUME/$MEDIA/$folder/.git/annex" ]; then
+ (
+ cd $VOLUME/$MEDIA/$folder
+ echo "Syncing $VOLUME/$MEDIA/$folder..."
+ git annex sync
+ git annex get --auto --numcopies=3
+ git annex fsck --fast
+ git annex unused
+ git annex dropunused 1-1000
+ #git annex drop --auto --numcopies=2
+ )
+ fi
+ done
+fi