#!/bin/bash # Script to backup local files to stack with commandline # installation and more info: www.doornenbal.org # # Usage: stacksync.sh # stacksync.sh is using a csv file for source and destination mapping: # , # Use mapfilter when you want to use only selected targetfolder in config CSV # ###Set correct rights on sync db.. # sudo chown : //.sync_journal.db # # crontab -e # #sync local data to stack # 30 1,12,18 * * * /home/servacc/bin/stacksync.sh # filter_log2 () { echo "$(date +"%d-%m-%y %H:%M:%S") filtering the logs" REMOVE_URL=https://$stackdns/remote.php/dav/files/$stackuser/ REMOVE_URL2=https://$stackdns/remote.php/dav/uploads/$stackuser/ n=0 while read line; do #echo "$line" | cut -c1-50 #First get all warnings/errors, except ignored stuff.. if [[ $line == *" warning "* ]]; then if [[ $line == *"s hidden."* ]]; then ((hidden++)); continue; fi if [[ $line == *"Symbolic links"* ]]; then ((symlinks++)); continue; fi if [[ $line == *" ignore list."* ]]; then ((ignorelist++)); continue; fi echo $line >> $logshadow if [[ $line == *" propagation "* ]]; then echo $line | awk -v FPAT='([^ ]+)|"([^"]+)"' '{ print $1, $2, $4, "propagation of", $12, "failed :"$20 }' >> $log #exit elif [[ $line == *" sparse "* ]]; then echo $line | awk -v FPAT='([^ ]+)|"([^"]+)"' '{ print $1, $2, $4, "Upload of", $11, "failed :"$12 }' >> $log elif [[ $line == *" Missing permissions "* ]]; then #echo $line #echo echo $line | awk -v FPAT='([^ ]+)|"([^"]+)"' '{ print $1, $2, $4, "Missing Permissions for", $11 }' >> $log else echo $line fi ((n++)) elif [[ $line == *" error "* ]]; then echo $line elif [[ $line == *"Rename detected"* ]]; then echo $line elif [[ $line == *"Resuming"* ]]; then echo $line >> $logshadow #echo echo $line | awk -v FPAT='([^ ]+)|"([^"]+)"' '{ print $1, $2, $4, "Resuming", $8 }' >> $log elif [[ $line == *"FINISHED"* ]]; then if [[ $line == *"OK"* ]]; then continue; fi #This is a failed upload of a (partial) file.. echo $line >> $logshadow echo $line | awk -v remove_url="$REMOVE_URL2" -v FPAT='([^ ]+)|"([^"]+)"' '{gsub(remove_url, "", $9); gsub(",","",$10); print $1, $2, $4, $10, $9, $12, $13, $15 }' >> $log elif [[ $line == *"PUTFileJob"* ]]; then echo $line >> $logshadow if [[ $line == *"$REMOVE_URL2"* ]]; then #This is a pointer to partial blob #echo $line #echo echo $line | awk -v remove_url="$REMOVE_URL2" -v FPAT='([^ ]+)|"([^"]+)"' '{gsub(remove_url, "", $9); gsub(",","",$10); print $1, $2, $10, $9 }' >> $log #exit else echo $line | awk -v remove_url="$REMOVE_URL" -v FPAT='([^ ]+)|"([^"]+)"' '{gsub(remove_url, "", $9); gsub(",","",$11); print $1, $2, $11, $9 }' >> $log fi elif [[ $line == *"DeleteJob"* ]]; then echo $line >> $logshadow echo $line | awk -v remove_url="$REMOVE_URL2" -v FPAT='([^ ]+)|"([^"]+)"' '{gsub(remove_url, "", $9); gsub(",","",$10); print $1, $2, $10, $9 }' >> $log fi #if [[ $n -ge 3 ]]; then break; fi done <$tmplog echo $(date +"%d-%m-%y %H:%M:%S") Skipped hidden files: $hidden, Symbolic links: $symlinks, Ignore listed: $ignorelist >> $log } date=`date +%Y%m%d` tmplog=/tmp/stacksync.log scriptdir="$(dirname "$(realpath "$0")")" logdir=$scriptdir/log log=$logdir/${date}-stacksync.log logshadow=$logdir/${date}-stacksync-shadow.log logdays=31 shadowdays=7 stackuser=loginname stackpass=loginpass stackdns=dnsname.stackstorage.com #Bij mij is de dnsname gelijk aan de stackusername. if [[ "$stackuser" == *"loginname"* ]]; then echo "You need to configure this script, please change the stack-user/pass/dns names (line 85)" exit fi FILE="$scriptdir/stacksync.csv" # Change this to your CSV file FILTER=$(echo "$1" | tr '[:upper:]' '[:lower:]') #read filter to lowercase echo "Logging to: $log" echo "Loading file: $FILE (keeping logs for $logdays days)" while IFS=, read -r col1 col2; do #Read csv - columns by IFS=, - read in col1/col2 col2_lower=$(echo "$col2" | tr '[:upper:]' '[:lower:]') #to lowercase if [[ -z "$FILTER" || "$col2_lower" == *"$FILTER"* ]]; then #when no filter or filter=curent-row echo Syncing $col1 to stackfolder: $col2 echo $(date +"%d-%m-%y %H:%M:%S") Syncing $col1 to stackfolder: $col2 >> $log echo $(date +"%d-%m-%y %H:%M:%S") Syncing $col1 to stackfolder: $col2 >> $logshadow stackcmd $col1 https://$stackuser:$stackpass@$stackdns $col2 > $tmplog 2>&1 filter_log2 if [[ -n "$FILTER" ]]; then echo Saving raw log to: $logdir/${date}-stacksync-$col2.log! cat $tmplog > $logdir/${date}-stacksync-$col2.log fi fi done < "$FILE" #Cleaning up old logfiles find $logdir/*stacksync-shadow.log -type f -mtime +$shadowdays -delete find $logdir/*stacksync.log -type f -mtime +$logdays -delete echo $(date +"%d-%m-%y %H:%M:%S") Finished