Hello
Im pretty new to scripting in linux but am keen to learn.
I'm trying to create a backup script similar to a batch script I developed for windows (dos) where the backup is to a usb drive. no problems with the backup process but what I would like to do is automatically remove old files if say less 20 gigabytes (or any hard coded number) is available.
What I did with the windows script was have a if statement check if 20g was available if not remove the oldest backup then check again then remove the oldest again etc. It used goto commands to loop which I know is ugly and there is no equivalent in the linux bash world.
I think i have the core parts of the script down pat which Ill list below but the loop part to go back and check im not sure about. should I be using a for loop?? or something else?
one liner to give me the free space available on usb drive
df |grep usb |awk '{print $4}'
one liner to remove oldest backup file (tgz file)
ls -S *.tgz |tail -n1 |xargs rm
one liner to check if space is less than hard code number (or ideally variable) using the above
if [ $(df |grep usb |awk '{print $4}') -lt 20000000 ]; then ls -S *.tgz |tail -n1 |xargs rm; fi
I know I should be building this all into functions. could someone help with this??
---------- Post updated at 12:05 PM ---------- Previous update was at 09:39 AM ----------
basically after ive sent the last command I posted (all in one line command) how do I then say go back and check again
check if 20G available
>> if not go back, remove another file and check again
>> if so, 20G is available carry on and do the backup
I used to use a goto command in the windows batch script I used but this isn't available in BASH.. I know its possible but am just wanting pointers or example scripts on how to loop this
doing it this way is quite nice as then im not reliant on whatever capacity drive a user may put in it will always make space on the device. with a few other checks in place as well.