dotfiles

Obligatory dotfiles repo
git clone git://git.danielmoch.com/dotfiles.git
Log | Files | Refs

cloudsync.sh (3003B)


      1 #!/bin/sh
      2 #
      3 # ~/lib/cron.avail/cloudsync.sh
      4 #
      5 
      6 echo "Beginning cloud sync at `date`"
      7 
      8 year=`/bin/date '+%Y'`
      9 month=`/bin/date '+%m'`
     10 lastrun_dir="$HOME/var/`basename $0`"
     11 lastrun_file="$lastrun_dir/lastrun"
     12 mobile_docs="$HOME/doc/mobile"
     13 notes="$HOME/doc/notes"
     14 server_fqdn="nextcloud.djmoch.org"
     15 openrsync_flags="--rsync-path=/usr/bin/openrsync -a"
     16 
     17 if [ -d "$lastrun_dir" ]
     18 then
     19     if [ -f "$lastrun_file" ]
     20     then
     21         lastrun_year=`cat $lastrun_file | cut -d ' ' -f 1`
     22         lastrun_month=`cat $lastrun_file | cut -d ' ' -f 2`
     23     else
     24         firstrun=1
     25     fi
     26 else
     27     firstrun=1
     28     mkdir -p "$lastrun_dir"
     29 fi
     30 echo "$year $month" > $lastrun_file
     31 
     32 [ ! -d "$mobile_docs" ] && mkdir -p "$mobile_docs"
     33 
     34 # Only do the following if the server is reachable
     35 if ping -c 1 $server_fqdn > /dev/null 2>&1
     36 then
     37     echo "$server_fqdn reachable. Proceeding."
     38 else
     39     echo "$server_fqdn NOT reachable. Exiting."
     40     exit -1
     41 fi
     42 
     43 LOCAL_PHOTOS="$HOME/doc/pix"
     44 
     45 # Mount the folder
     46 if doas /sbin/mount /mnt/nextcloud
     47 then
     48     # Rsync from WebDAV to ~/Photos
     49     echo "Syncing Photos from NextCloud to $LOCAL_PHOTOS"
     50     [ ! -d $LOCAL_PHOTOS/$year/$month ] && mkdir -p $LOCAL_PHOTOS/$year/$month
     51     openrsync $openrsync_flags /mnt/nextcloud/Photos/$year/$month/ $LOCAL_PHOTOS/$year/$month
     52     if [ -z "$firstrun" -a $lastrun_month -ne $month ]
     53     then
     54         # TODO: What if we're more than a month behind?
     55         if [ ! -d $LOCAL_PHOTOS/$lastrun_year/$lastrun_month ]
     56         then
     57             mkdir -p $LOCAL_PHOTOS/$lastrun_year/$lastrun_month
     58         fi
     59         openrsync $openrsync_flags /mnt/nextcloud/Photos/$lastrun_year/$lastrun_month/ $LOCAL_PHOTOS/$lastrun_year/$lastrun_month
     60     fi
     61 
     62     # Rsync from ~/Photos to NextCloud
     63     echo "Syncing Photos from $LOCAL_PHOTOS to NextCloud"
     64     [ ! -d /mnt/nextcloud/Photos/$year/$month ] && mkdir -p /mnt/nextcloud/Photos/$year/$month
     65     openrsync $openrsync_flags $LOCAL_PHOTOS/$year/$month/ /mnt/nextcloud/Photos/$year/$month
     66     if [ -z "$firstrun" -a $lastrun_month -ne $month ]
     67     then
     68         if [ ! -d /mnt/nextcloud/Photos/$lastrun_year/$lastrun_month ]
     69         then
     70             mkdir -p /mnt/nextcloud/Photos/$lastrun_year/$lastrun_month
     71         fi
     72         # TODO: What if we're more than a month behind?
     73         openrsync $openrsync_flags $LOCAL_PHOTOS/$lastrun_year/$lastrun_month/ /mnt/nextcloud/Photos/$lastrun_year/$lastrun_month
     74     fi
     75 
     76     # Rsync from ~/Documents/Mobile to NextCloud
     77     echo "Syncing mobile documents from $mobile_docs to NextCloud"
     78     openrsync $openrsync_flags "$mobile_docs"/ /mnt/nextcloud/Documents
     79 
     80     # Rsync from NextCloud to ~/Documents/Mobile
     81     echo "Syncing mobile documents from NextCloud to $mobile_docs"
     82     openrsync $openrsync_flags /mnt/nextcloud/Documents/ "$mobile_docs"
     83 
     84     # Finish by unmounting the WebDAV folder
     85     echo "Unmounting $server_fqdn"
     86     doas /sbin/umount /mnt/nextcloud
     87 else
     88     echo "NextCloud mount FAILED. Exiting."
     89     exit -2
     90 fi
     91 
     92 echo "Completed cloud sync at `date`"