aboutsummaryrefslogtreecommitdiff
path: root/files/sync-media
blob: bcd484846618783a7a00d691e31ca33c4cb5133a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
#
# sync-media assets using git-annex
#

DISK="$1"
VOLUME="/media/$DISK"
CACHE="/var/cache/media"
DOMAIN="`facter domain`"
MEDIA="media.$DOMAIN"
INCOMING="$CACHE/incoming"
WHOAMI="`whoami`"
OPTIONS="$*"

# 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_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..."
      sync_media_add
      git annex sync
      git annex get --auto --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