Bash script to move files when drive >xx% until <xx%


Recommended Posts

I'm trying to get a script to transfer files from my cache to the remote mount, this should be triggered when the cache is >70% and then transfer files until cache is <70%.

I'm not very knowledgeable when it comes to this stuff so I've come up with this by studying others work on the mighty interwebs.

Below transfers 1 file and then the script completes.

Spoiler
#!/bin/bash

Local='/mnt/cache/lokalt/'
Remote='/mnt/remotes/Gdrive/Lagring/'
ExcludeDir1=rutorrent
ExcludeDir2=nzbget
CacheDrive='/mnt/cache'
Percent='70'

df -h | grep $CacheDrive | awk '{ print $5 }' | cut -d'%' -f1 | while read FSC; do
	if [ $FSC -ge $Percent ]; then
		find $Local -ctime +0 -type f -not -path $Local$ExcludeDir1'/*' -not -path $Local$ExcludeDir2'/*' -printf '%T@ %p\n' | sort -k1,1 -g | head -1 | sed 's/^[0-9.]\+ //' | cut -d'/' -f5,6,7,8,9 | while read OldestFile; do
		rclone move $Local"$OldestFile" $Remote"$OldestFile" --dry-run --progress --size-only --exclude .unionfs/** --exclude *fuse_hidden* --exclude *_HIDDEN --exclude .recycle** --exclude *.backup~* --exclude *.partial~* --exclude *.partial* --exclude *.Partial~* --exclude *.Partial* --delete-after --delete-empty-src-dirs
	done
fi
done

 

And this loops the script, but it continues even after the cache drive utilization is below 70%. I suspect that the mistake I've made is that the first string with "df -h ...." is stale and that the subsequent commands is using the initial value all the time?

Spoiler
#!/bin/bash

Local='/mnt/cache/lokalt/'
Remote='/mnt/remotes/Gdrive/Lagring/'
ExcludeDir1=rutorrent
ExcludeDir2=nzbget
CacheDrive='/mnt/cache'
Percent='70'

df -h | grep $CacheDrive | awk '{ print $5 }' | cut -d'%' -f1 | while read FSC; do
	while [ $FSC -ge $Percent ]; do
		find $Local -ctime +0 -type f -not -path $Local$ExcludeDir1'/*' -not -path $Local$ExcludeDir2'/*' -printf '%T@ %p\n' | sort -k1,1 -g | head -1 | sed 's/^[0-9.]\+ //' | cut -d'/' -f5,6,7,8,9 | while read OldestFile; do
		rclone move $Local"$OldestFile" $Remote"$OldestFile" --dry-run --progress --size-only --exclude .unionfs/** --exclude *fuse_hidden* --exclude *_HIDDEN --exclude .recycle** --exclude *.backup~* --exclude *.partial~* --exclude *.partial* --exclude *.Partial~* --exclude *.Partial* --delete-after --delete-empty-src-dirs --log-level INFO
		done
	done
done

 

Can anyone help me get this script working as I intend?

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.