#!/bin/bash # # sync-media assets using git-annex # DISK="$1" VOLUME="/media/$DISK" CACHE="/var/cache/media" DOMAIN="`facter domain`" HOST="`facter hostname`" MEDIA="media.$DOMAIN" INCOMING="$CACHE/incoming" WHOAMI="`whoami`" OPTIONS="$*" # Fix identity function sync_media_identity { if [ -z "`git config user.email`" ] || [ -z "`git config user.name`" ]; then repo="$(basename `pwd`)" git config user.name "$repo asset manager" git config user.email "$repo@assset.manager" fi } # Add files into the annex function sync_media_add { 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 } # Set sudo config if [ "$WHOAMI" != 'root' ]; then sudo="sudo" fi # Set fsck config if echo $OPTIONS | grep -q -- "--fsck"; then FSCK="true" fi # Set unused config if echo $OPTIONS | grep -q -- "--dropunused"; then DROPUNUSED="true" 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..." sync_media_identity sync_media_add git annex sync if [ "$FSCK" == "true" ]; then git annex fsck --fast fi if [ "$DROPUNUSED" == "true" ]; then git annex unused git annex dropunused 1-1000 fi ) fi done if [ ! -z "$DISK" ]; then # 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 elif [ ! -d "$VOLUME/$MEDIA/$folder" ]; then echo "Syncing $VOLUME/$MEDIA/$folder..." rsync -av --delete $CACHE/$folder/ $VOLUME/$MEDIA/$folder/ fi done fi fi fi fi # Retrieve changes at media volumes if [ ! -z "$DRIVE" ] && [ -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..." # Check remote if ! git remote | grep -q "^$HOST$"; then git remote add $HOST $CACHE/$folder fi sync_media_identity sync_media_add git annex sync git annex get . --numcopies=3 if [ "$FSCK" == "true" ]; then git annex fsck --fast fi if [ "$DROPUNUSED" == "true" ]; then git annex unused git annex dropunused 1-1000 fi #git annex drop --auto --numcopies=2 ) elif [ -d "$CACHE/$folder" ]; then echo "Syncing $VOLUME/$MEDIA/$folder..." rsync -av --delete $CACHE/$folder/ $VOLUME/$MEDIA/$folder/ fi done fi