Comparing folders - autocopy script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing folders - autocopy script
# 1  
Old 04-15-2016
Comparing folders - autocopy script

Hey Guys,

first of all: I'm new to unix and im not very experienced.
I've bought a rapsberry pi and i want it to be the backup-machine for my SD-Cards while im travelling (attached ssd, card reader, a small touch-tft)

I managed to modify it a litte bit (its sending information bubbles to the desktop).

What this script is doing:

1. It starts copying photos and videos as soon as i plug in a card reader (automatically)



My problem:
I want the script to do a check. To compare the old folder and the new folder and to check if everything has been transfered properly.
I don't even know where to start. I can't do it by myself.

I don't even know what the script should compare? Filesize? Number of files? Filesize seems a little risky, the sd-cards partition could be NTFS...AND the script should check the ,,FILE_MATCH=".*\" formats only.

Can anyone help?
I would appreciate it! Smilie

Kind regards



Code:
#!/bin/bash
#
# sd-autocopy - Copies/Moves images automatically from inserted media
#
# Copyright (c) 2011 Lars Michelsen <lm@larsmichelsen.com>;
#
# License:
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# But this in curly braces and detach the whole block to background
# for not blocking udev during copying.
{

# 2016/04/15, Cubbi: ignore blockdevice
if [ "$UDISKS_PARTITION_TABLE" -eq "1" ]; then exit 0; fi

USER=root #unter welchen Rechten die Dateien gespeichert werden -- SMB USER!
TARGET=/media/ssd/_upload #Zielverzeichnis eintragen
HANDLED_SERIALS=TS-RDF5_SD_Transcend_000000000039-0:0
MOUNT=/media/cardreader #Ordner vorher erstellen
FILE_MATCH=".*\.(jpg|cr2|jpeg|pdf|mkv|avi|mp4|3gp|mpg|mov|mpeg|wma|wmv|vob|mts)$" #hier die Dateiformate eintragen
TRANSFER=cp #cp für kopieren, mv für verschieben
TIMESTAMP=$(date +%Y%m%d%H%M%S) #timestamp für Unterordner

# END OF CONFIGURATION ########################################################

# DEBUG:
# To enable debugging without udev events uncomment the lines below
#ID_SERIAL=0x000006d4
#DEVNAME=/dev/mmcblk0p1

# Catch unset but needed vars - terminate script
if [ -z "$ID_SERIAL" ] || [ -z "$DEVNAME" ]; then
    exit 1
fi

if ! echo "$HANDLED_SERIALS" | grep $ID_SERIAL >/dev/null 2>&1; then
    logger -t sd-autocopy "Card not accepted!"
        exit 1
fi

logger -t sd-autocopy "Karte akzeptiert. Starte Kopiervorgang..."
sudo -u pi DISPLAY=:0.0 notify-send 'start'

sleep 10

if [ ! -d "$MOUNT" ]; then
    mkdir -p "$MOUNT"
fi

mount $DEVNAME "$MOUNT"

LINES=$(find "$MOUNT" -regextype posix-egrep -iregex "$FILE_MATCH" -printf "%TY-%Tm-%Td_%p\n")
DATES=$(echo "$LINES" | cut -d_ -f1 | uniq)

# First create the directories when missing
for DATE in $DATES; do
    echo $DATE
    mkdir -p "$TARGET/$TIMESTAMP/$DATE"
done

# Move the images
NUM=0
for LINE in $LINES; do
    DATE=${LINE%%_*}
    FILE=${LINE#*_}
    logger -t sd-autocopy "Kopiere $FILE"
    $TRANSFER "$FILE" "$TARGET/$TIMESTAMP/$DATE"
    NUM=$(($NUM+1))
done

umount $DEVNAME
umount "$MOUNT"

# Fix the permissions for all directories recursive where
# files have been copied/moved to
for DATE in $DATES; do
    chown -R $USER.$USER "$TARGET/$TIMESTAMP"
chmod -R 775 /media/ssd
done

logger -t sd-autocopy "Fertig! $NUM Dateien kopiert."
sudo -u pi DISPLAY=:0.0 notify-send 'fertig'

} &
exit 0

# 2  
Old 04-15-2016
It would seem that after the line:
Code:
    $TRANSFER "$FILE" "$TARGET/$TIMESTAMP/$DATE"

you would want to run command something like:
Code:
    if ! cmp "$FILE" "$TARGET/$TIMESTAMP/$DATE" > /dev/null 2>&1
    then printf 'Attempt to copy file "%s" failed.\n' "$FILE"
    fi

presumably replacing the printf command with a call to logger. Instead of redirecting the cmp output to /dev/null you might want to save it in another file and also include the contents of that file with the data you send to logger.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 04-16-2016
Hey, thank you very much!

So It would be:

Code:
if ! cmp "$FILE" "$TARGET/$TIMESTAMP/$DATE" > /dev/null 2>&1


logger -t sd-autocopy "copying of $FILE  failed"
    fi

Do I have to include the part \n' "$FILE" in the logger part?

Could you please check is this is right?


And how would you do the part with the log file?


Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!

Last edited by RudiC; 04-16-2016 at 10:09 AM.. Reason: Added code tags.
# 4  
Old 04-16-2016
Quote:
Originally Posted by Eomer
Hey, thank you very much!

So It would be:

Code:
if ! cmp "$FILE" "$TARGET/$TIMESTAMP/$DATE" > /dev/null 2>&1


logger -t sd-autocopy "copying of $FILE  failed"
    fi

Do I have to include the part \n' "$FILE" in the logger part?

Could you please check is this is right?


And how would you do the part with the log file?


Moderator's Comments:
Mod Comment Please use code tags as require red by forum rules!
You don't need to use a printf style format string operand with logger, but you do need a then in your if statement.
Code:
    if ! tmp "$FILE" "$TARGET/$TIMESTAMP/$DATE" > /dev/null 2>&1; then
	logger -t sd-autocopy "copying of $FILE failed"
    fi

I think it would also be wise to keep track of whether or not any copies failed and include an indication of success or failure in the notify-send at the end of the background job part of your script.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Deleting Folders Script

Hi all new to the forums. Very beginner at shell scripting. What I'm trying to do is this: Is the directory empty prior to removal of it? If not, inform the user that the directory is not empty and if the user wants to remove it remove or move the files in it first. But I'm stuck.. It checks... (3 Replies)
Discussion started by: rduckett1456
3 Replies

2. Shell Programming and Scripting

Comparing two folders

Hi, Can you tell me how to compare two directories and write the differing files to some other folder? (5 Replies)
Discussion started by: arijitsaha
5 Replies

3. Shell Programming and Scripting

Why is my bash script merging folders?

I hope this thread is in the right forum... This is for a .deb package that will be installed on Cydia. Pre'note': I am installing a custom 'hosts' file and AdSheet.app directory. My problem is during a reinstall/upgrade. Cydia will run the entire installation process again, which means it... (1 Reply)
Discussion started by: thazsar
1 Replies

4. UNIX for Dummies Questions & Answers

script to create folders

hi again, having an issue with the code here because it doesnt work :D can someone point what and how to change, please. #!/bin/bash #create directory mylabs, inside create 6 directories named by user. DIR1="$1" DIR2="$2" if ; then echo -n " there is a folder named mylabs, what... (1 Reply)
Discussion started by: me.
1 Replies

5. UNIX for Dummies Questions & Answers

Searching for folders/parent folders not files.

Hello again, A little while back I got help with creating a command to search all directories and sub directories for files from daystart of day x. I'm wondering if there is a command that I've overlooked that may be able to search for / write folder names to an output file which ideally... (2 Replies)
Discussion started by: Aussiemick
2 Replies

6. Ubuntu

Moving folders with script

Hi All, I am new to Forums, as i am struggling for one script i am launched here. I need to move more than 60,000+ folders in 1,00,000 folders to another server. I have the list of folders which should be moved. can anybody help me in sharing with the script for the above requirement. ... (4 Replies)
Discussion started by: ghimakiran
4 Replies

7. Shell Programming and Scripting

Apply script to several archives in several Folders.

Hello. I'm here again. I have a script in python and bash, and I need execute this script over all files in all folders. Example: Folder: CMDB Subfolders: router1 router2 switch1 switch2 and in this folders exists a file called... (3 Replies)
Discussion started by: bobbasystem
3 Replies

8. Shell Programming and Scripting

Comparing Two Binary Folders

I am a beginner to all of this but undertand the basic principles. I am currently working on a large task and struggling with a the final part. I have two files, folder 1 contains a list of around 20 files in binary (possibly treated as arrays?) folder 2 contains several files and sub... (2 Replies)
Discussion started by: puzzler
2 Replies

9. Shell Programming and Scripting

listing folders and using in script

im a bit new to this and have been playing quite a bit and cant figure it out :( I have made a basic script: cd /folder/software1/bin/; echo "software1," >> /in/local/var/trace/davescripts/software.txt "\c"; ls -tm >> /in/local/var/trace/davescripts/software.txt; and this basically... (1 Reply)
Discussion started by: truCido
1 Replies

10. Shell Programming and Scripting

script to loop around folders

I have a folder called {homedata} Within this folder there are 12 subfolders 200601.......200612 Within each subfolder there are 8 sets of files Each filename commences with A B C D E F G or H, so {filename}* can be used. I am trying to write a script which will from the top level go... (2 Replies)
Discussion started by: grinder182533
2 Replies
Login or Register to Ask a Question