like asking for a usb device with a space of 32gb and take an action when desctarla in linux

0

Good day I have the next task I need to copy files from a camera to an HDD both connected on the same computer I plan to make automatic backups when I enter the camera and synchronize them to the HDD that I have connected to the computer.

Searching the internet I found that rsync solves the part of the synchrony, on the other hand I do not know how to ask for the microSD its capacity is 32gb and its format is fat32.

I found a small script that says the following

#!/bin/bash
#
# -*- ENCODING: UTF-8 -*-
# Este programa es software libre. Puede redistribuirlo y/o
# modificarlo bajo los términos de la Licencia Pública General
# de GNU según es publicada por la Free Software Foundation,
# bien de la versión 2 de dicha Licencia o bien (según su
# elección) de cualquier versión posterior.
#
# Si usted hace alguna modificación en esta aplicación,
# deberá siempre mencionar al autor original de la misma.
#
# Copyleft 2012, DesdeLinux.net {Ciudad Habana, Cuba}.
# Autor: KZKG^Gaara <[email protected]> <http://desdelinux.net>

CONTROL=0
PLACE="/home/media/pi/toshiba"

mkdir $PLACE
chmod 777 -R $PLACE

while [ $CONTROL=0 ] ; do
    cat /etc/mtab | grep media >> /dev/null
    if [ $? -ne 0 ]; then
        CONTROL=0
    else
        CONTROL=1
        for USBDEV in 'df | grep media | awk -F / {'print $5'}' ;
        do
            USBSIZE='df | grep $USBDEV | awk {'print $2'}'
            if [ $USBSIZE -lt 15664800 ]; then
                USBNAME='echo $USBDEV | awk -F / {'print $3'}'
                mkdir $PLACE/$USBNAME
                rsync /media/$USBNAME/ $PLACE/$USBNAME/ -ahv --include-from=/opt/bash/usb-spy.files --exclude=*.* --prune-empty-dirs
            fi
        done
    fi
    sleep 5
done

exit 0

Copy the files perfectly but copy them to the local hdd (where the boot files are loaded, the home and those things), the problem is that when I enter the backup HDD when executing it from the terminal I get a message with the following "too many arguments" legend

Now I kept thinking and I understand that the IF cycles are to condition the next action what I do not give is like asking specifically for my microSD because I understand that it does not know what device to synchronize, I hope you can help me

    
asked by Hector Garnica Barrera 12.09.2017 в 00:20
source

1 answer

0

As I said in the comment, we are going to use udev to solve this task. The following commands must be done as root , since we are going to touch the system at a low level:

Part 1: Identify SD card

Insert the SD card into our computer and launch the following command:

fdisk -l

Most likely, if you have an internal hard drive ( sda ) and an external drive ( sdb ) permanently connected to your computer, identify the card as sdc fdisk will display information about the partitions of all of them. In any case, stay with the one that corresponds by size:

  

Disk / dev / sdc: 29.8 GiB, 34359738368 bytes, ...

NOTE : From now on I'm going to assume that sdc is the name your system assigns to that card.

Part 2: Configure udev

Once we are sure that our card is identified by the system as / dev / sdc, we unmount its partition (just in case):

umount /dev/sdc1

Next, let's keep the serial number of the card (it is the only element impossible to repeat). It will serve us a little later so that the copy script is only released when you put that SD and not another one:

udevadm info -a -n sdc | grep serial | head -n 1

It will throw an exit like the following:

  

ATTRS {serial} == "1234567890ABCDEF"

Write it down. Now we go to the following directory:

cd /etc/udev/rules.d/

With the text editor that we like the most (I will use nano ), we created a file called sd.rules (the name matters little, but ends in .rules ):

nano sd.rules

Once in that file, we'll add the following content (look at the serial number, it's the one we got before):

KERNEL=="sd*1", ATTRS{serial}=="1234567890ABCDEF", RUN+="/usr/local/bin/backup.sh"

To save and exit at the same time: in nano it is Ctrl+X , S , and the key Intro .

Part 3: Test it works

If you notice, in the previous section we have achieved that every time you insert the SD, launch a specific command. Before putting the backup script, let's try it:

nano /usr/local/bin/backup.sh

Within this new file, we introduce the following:

#!/bin/bash
#
# Script que añade el texto "Tarjeta SD insertada." (sin las comillas)
# al fichero /tmp/log_prueba.txt
echo "Tarjeta SD insertada." >> /tmp/log_prueba.txt
exit 0

To save and exit at the same time: in nano it is Ctrl+X , S , and the key Intro . Finally, we will give you execution permissions:

chmod +x /usr/local/bin/backup.sh

At this point, if you take out and put in your SD card, you should automatically create a file in /tmp called log_prueba.txt with the specified text. Before proceeding, try to extract the SD and enter it two or three times: you will notice that the file grows in size and adds a line in each action.

Part 4: Configure copy script

Here it is already to taste of each one, although I recognize that the following program should work without problems.

nano /usr/local/bin/backup.sh

Inside that file, we delete what we have and introduce the following:

#!/bin/bash
#
# Script que hace copia de seguridad entre dos carpetas del sistema.

# RUTAS:
SD="/dev/sdc1"
HDD="/dev/sdb1"
ORIGEN="/home/media/pi/sd/"
DESTINO="/home/media/pi/toshiba/"

# Creamos los puntos de montaje y les damos permisos:
mkdir -p $ORIGEN $DESTINO
chmod 777 -R $ORIGEN $DESTINO

# Montamos los dispositivos en sus puntos de montaje:
mount $SD $ORIGEN
mount $HDD $DESTINO

# Lanzamos la copia con rsync:
rsync -avzh --delete $ORIGEN $DESTINO

# Desmontamos los medios:
umount $ORIGEN $DESTINO

exit 0

You can adapt your taste, I hope this manual has served you.

    
answered by 13.09.2017 в 15:12