I am running openmediavault on my Raspberry and I would like to use it as a backup FTP server of snapshots taken from my IP cams.
So I get the network recorder to upload every 3 seconds a snapshot to the Raspberry. Everything works perfectly.
I would need now a simple script that checks the size of a directory (Backup_NVR) and if it exceeds for instance 3 GB, it deletes recursively all images and empty directories older than 60 minutes.
I would schedule this script as a job and it should be done.
Being new to shell scripting, although I have IT experience, can anyone please draft this script for me?
# get size of directory
sz=$(du -s --block-size G /fullpath/to/Backup_NVR | awk '{print int($0)}')
if [ $sz -ge 3 ] ; then # too big delete older files
find /path/to/Backup_NVR -type f -mmin +60 -exec rm {} \;
fi
Thanks everybody for helping.
I have written the entire script, inserting a pause to check the directory size before delete and to debug it.
Can anyone please check if it is correct?
Thanks.
QUOTE
#!/bin/bash
# delete files oder than MM minutes if the size exceeds GG gb
MM=60
GG=3
# get size of directory
sz=$(du -s --block-size G ./Backup_NVR | awk '{print int($0)}')
echo �Directory size: � $sz
read -p "Press [Enter] key to continue��
if [ $sz -ge $GG ] ; then # too big delete older files
find ./Backup_NVR -type f -mmin +$MM -delete
find ./Backup_NVR -mindepth 1 -depth -type d -empty -delete
fi
I don't see anything obviously wrong You have edited this in Microsoft Word which has inserted a few nasty "smart quotes" into it. Replace all smart quotes with ' and " in an actual text editor.
And definitely do a "dry run" first: Print filenames instead of deleting them.
I'm not sure how the pause helps you; once you hit it, it either deletes the right files or the wrong ones...
#!/bin/bash
# delete files oder than MM minutes if the size exceeds GG gb
MM=60
GG=3
# get size of directory
sz=$(du -s --block-size G ./Backup_NVR | awk '{print int($0)}')
echo "Directory size: " $sz
read -p "Press [Enter] key to continue��
if [ $sz -ge $GG ] ; then # too big delete older files
find ./Backup_NVR -type f -mmin +$MM -print
find ./Backup_NVR -mindepth 1 -depth -type d -empty -print
fi