Running fsck with other program and UDEV in bash - bash

After much trouble I got the UDEV rule to run after inserting an USB. It runs a program to convert the names of pictures and movies. I use {} & to run the program in the background: The only thing is that by unplugging the usb it is easy corrupted. So I would like to also run fsck. Does anybody has an idea?
Here is the UDEV rule:
CTION=="add", SUBSYSTEM=="block", ATTRS{idVendor}=="14cd", ATTRS{idProduct}=="121f", RUN+="/home/pi/bashtest.sh"
Here is the program:
#!/bin/bash
sudo umount /dev/sda1
sudo fsck -y /dev/sda1
{
dd=1234567890aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ
sleep 5
sudo mount -t vfat /dev/sda1 /media/usb1
cd /media/usb1/DCIM/Camera
sudo find /media/usb1/DCIM/Camera -regextype posix-egrep -regex ".*[^/]{13}.JPG"|
for i in *.JPG
do
ddate=$(exiv2 "${i}"|grep timestamp)
SPEC=$ddate
read X X YEAR MONTH DAY HOUR MINUTE SECOND <<<${SPEC//:/ }
d1=${YEAR:2}
d2=${dd:(10#$MONTH-1):1}
d3=${dd:(10#$DAY-1):1}
d4=${dd:(10#$HOUR-1):1}
d5=${dd:(10#$MINUTE-1):1}
d6=${dd:(10#$SECOND-1):1}
d7=0
sudo cp -nrv --preserve=all "$i" /media/usb1/DCIM/"${d1}${d2}${d3}${d4}${d5}${d6}${d7}.JPG"
find . -name '*.JPG' -size -1 -delete
done
for i in *.MP4
do
#exiftool -createdate -S -s 20140308_133017.MP4
dddate=$(exiftool "${i}" |grep "Media Create Date" | awk -F':' '{print $2, $3, $4, $5, $6, $7}')
SPEC=$dddate
read YEAR MONTH DAY HOUR MINUTE SECOND <<<${SPEC//:/ }
d1=${YEAR:2}
d2=${dd:(10#$MONTH-1):1}
d3=${dd:(10#$DAY-1):1}
d4=${dd:(10#$HOUR-1):1}
d5=${dd:(10#$MINUTE-1):1}
d6=${dd:(10#$SECOND-1):1}
d7=0
sudo cp -nrv --preserve=all "$i" /media/usb1/DCIM/"${d1}${d2}${d3}${d4}${d5}${d6}${d7}.MP4"
done
sudo umount -l /media/usb1
sleep 5
sudo shutdown -h now
} &
Probably the code can be written better, but it works for me.

Using {} && for fsck and the rest {} & let it all work on the background and helped me to get it to work!

Related

inotifywait adding multiple files at ones

this shell script should add everything put in the folder to transmission. With one folder it works fine, but when i add more then one folder at the same moment it ignores the second one.
while true;
do
file=$(inotifywait -e moved_to --format %f /srv/watchfolderfilme)
file="/srv/watchfolderfilme/$file"
transmission-create -o $file.torrent -s 16384 -t http://0.0.0.0:6969/announce $file
mv $file /srv/downloads
chmod 0777 $file.torrent
cp $file.torrent /srv/newtorrentfiles
mv $file.torrent /srv/watchfoldertorrents
done
Rethough my solution and found a better one that works fine for multiple adds
inotifywait -m /srv/watchfolderfilme -e create -e moved_to |
while read path action file; do
# echo "The file '$file' appeared in directory '$path' via '$action'"
chmod 0777 $path$file
transmission-create -o /srv/newtorrentfiles/$file.torrent -s 16384 -t http://0.0.0.0:6969/announce $path$file
mv $path$file /srv/downloads
chmod 0777 /srv/newtorrentfiles/$file.torrent
cp /srv/newtorrentfiles/$file.torrent /srv/watchfoldertorrents
done

SD corrupted / data loss when renaming files with Raspberry

For my parents I try to convert the names of files on a (micro-)SD-card with a Raspberry. But many times the SD-card get corrupted or big files get removed from the card while renaming the file. This is the UDEV rule I use:
ACTION=="add",
SUBSYSTEM=="block",
ATTRS{idVendor}=="14cd",
ATTRS{idProduct}=="121f",
RUN+="/home/pi/bashtest.sh"
And this is the code in bash on the Raspberry:
#!/bin/bash
{
sudo umount /dev/sda1
sudo fsck -y /dev/sda1
} &
{
dd=1234567890aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ
sleep 5
sudo mount -t vfat /dev/sda1 /media/usb1
cd /media/usb1/DCIM/Camera
sudo find /media/usb1/DCIM/Camera -regextype posix-egrep -regex ".*[^/]{13}.JPG"|
for i in *.JPG
do
ddate=$(exiv2 "${i}"|grep timestamp)
SPEC=$ddate
read X X YEAR MONTH DAY HOUR MINUTE SECOND <<<${SPEC//:/ }
d1=${YEAR:2}
d2=${dd:(10#$MONTH-1):1}
d3=${dd:(10#$DAY-1):1}
d4=${dd:(10#$HOUR-1):1}
d5=${dd:(10#$MINUTE-1):1}
d6=${dd:(10#$SECOND-1):1}
d7=0
/media/usb1/DCIM/"${d1}${d2}${d3}${d4}${d5}${d6}${d7}.JPG"
sudo mv -u "$i" /media/usb1/DCIM/"${d1}${d2}${d3}${d4}${d5}${d6}${d7}.JPG"
done
for i in *.MP4
do
dddate=$(exiftool "${i}" |grep "Media Create Date" | awk -F':' '{print $2, $3, $4, $5, $6, $7}')
SPEC=$dddate
read YEAR MONTH DAY HOUR MINUTE SECOND <<<${SPEC//:/ }
d1=${YEAR:2}
d2=${dd:(10#$MONTH-1):1}
d3=${dd:(10#$DAY-1):1}
d4=${dd:(10#$HOUR-1):1}
d5=${dd:(10#$MINUTE-1):1}
d6=${dd:(10#$SECOND-1):1}
d7=0
sudo mv -u "$i" /media/usb1/DCIM/"${d1}${d2}${d3}${d4}${d5}${d6}${d7}.MP4"
/media/usb1/DCIM/"${d1}${d2}${d3}${d4}${d5}${d6}${d7}.MP4"
done
sudo umount -l /media/usb1
sleep 5
sudo shutdown -h now
} &
With the first program I let a copy in an other map, but that took too much space of the SD-card. Any ideas how I could improve the code?
This is a trail of a previous program:
https://vimeo.com/86546119
A common problem with raspberry pi. Solutions range from changing to better power supply, using a different SD card, and reducing CPU clock, especially if overclocked. Problem with "charger" style power supplies is often that their response to pulse load is poor, causing very short brownouts during flash writes.

Permission Issue: Creating postgres back as postgres user

I have following postgres backup script, its a shell script and written to run ans postgres user.
But the problem is postgres user doesn't have permission to write these directories. I as a user don't have sudo on these machines but I have changed the directory to has 755 and added to one of the group that has major permission to do read-write-execute. Since postgres user isn't part of the unix user group I guess I am running into this issue.
My goal is to put this in the cron-tab but prior to that I need to get the script running with proper permission:
#!/bin/bash
# location to store backups
backup_dir="/location/to/dir"
# name of the backup file has the date
backup_date=`date +%d-%m-%Y`
# only keep the backup for 30 days (maintain low storage)
number_of_days=30
databases=`psql -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d'`
for i in $databases; do
if [ "$i" != "template0" ] && [ "$i" != "template1" ]; then
echo Dumping $i to $backup_dir$i\_$backup_date
pg_dump -Fc $i > $backup_dir$i\_$backup_date
fi
done
find $backup_dir -type f -prune -mtime +$number_of_days -exec rm -f {} \;
Before doing this be sure to login as a super user (sudo su) and try executing these:
useradd -G unix postgres (Add postgres user to unix group)
su postgres (Login as postgres user)
mkdir folder (Go to the directory where postgres needs to write files)
***From this line down is my answer to #find-missing-semicolon question
Just to illustrate an example with a shell script, you can capture the password using the read command and put it to a variable. Here I stored the password in password and echoed it afterwards. I hope this helps.
`#!/bin/bash`
`read -s -p "Password: " password`
`echo $password`

Retrieving only name of user who owns folder

I'm making a bash script that has to create subfolders and expand files into a mounted folder. My Problem is that I can't create subfolders as ROOT, i need to the commands in my script as:
su - UnknownUser -c "mkdir MAKEDir"
So my question is, how can I only retrieve the user name when doing commands like
ls -l
Thanks for any input!
Use stat -c with a format of %U for the textual name of the owner, or %u for the uid of the owner (or %G/%g for group)
stat -c %U <filename>
The stat command varies greatly by implementation, but the following will work
# GNU stat
# -c may be used in place of --format
$ stat --format %U file.ext
# BSD (Mac OS X, anyway) stat
$ stat -f %Su file.ext
use awk:
for the user of file foo:
ls -l foo | awk '{print $3}'
or, for user and filename together:
ls -l | awk '{print $3, $9 }'
If "parent" is the parent directory...
user=$(ls -ld ${parent} | awk '{print $3}')
sudo -u $user mkdir ${parent}/child1
sudo -u $user mkdir ${parent}/child2
sudo -u $user mkdir ${parent}/child1/grandchild1
sudo -u $user mkdir ${parent}/child1/grandchild2

Downloading and automatically installing a tgz file

#!/bin/bash
mkdir /tmp
curl -O http://www.mucommander.com/download/nightly/mucommander-current.app.tar.gz /tmp/mucommander.tgz
tar -xvzf /tmp/mucommander.tgz */mucommander.app/*
cp -r /tmp/mucommander.app /Applications
rm -r /tmp
I'm trying to create a shell script to download and extract muCommander to my applications directory on a Mac.
I tried cd into the tmp dir, but then the script stops when I do that.
I can extract all using the -C argument, but the current tgz path is muCommander-0_9_0/mucommander.app, which could change on later builds, so I'm trying to keep it generic.
Can anyone give me pointers where I'm going wrong?
Thanks in advance.
Strip the first path component when you untar the archive, from tar(1):
--strip-components count
(x mode only) Remove the specified number of leading path ele-
ments. Pathnames with fewer elements will be silently skipped.
Note that the pathname is edited after checking inclusion/exclu-
sion patterns but before security checks.
Update
Here is a working bash example of how to, fairly generically, copy the contents of the tgz file to /Applications.
shopt -s nocaseglob
TMPDIR=/tmp
APP=mucommander
TMPAPPDIR=$TMPDIR/$APP
mkdir -p $TMPAPPDIR
curl -o $TMPDIR/$APP.tgz http://www.mucommander.com/download/nightly/mucommander-current.app.tar.gz
tar --strip-components=1 -xvzf $APP.tgz -C $TMPAPPDIR
mv $TMPAPPDIR/${APP}* /Applications
# rm -rf $TMPAPPDIR $TMPDIR/$APP
The rm command is commented out for now, verify that it does no harm before you use it.
The following will update your muCommander.
#for the safety, remove old temporary extraction from the /tmp
rm -rf /tmp/muCommander.app
#kill the running mucommander - you dont want replace the runnung app
ps -ef | grep ' /Applications/muCommander.app/' | grep -v grep | awk '{print $2}' | xargs kill
#download, extract, remove old, move new, open
#each command run only when the previous ended with success
curl http://www.mucommander.com/download/nightly/mucommander-current.app.tar.gz |\
tar -xzf - -C /tmp --strip-components=1 '*/muCommander.app' && \
rm -rf /Applications/muCommander.app && \
mv /tmp/muCommander.app /Applications && \
open /Applications/muCommander.app
Beware, after the '\' must following new line, and not any spaces...

Resources