#!/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