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
one liner to remove oldest backup file (tgz file)
one liner to check if space is less than hard code number (or ideally variable) using the above
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.
A couple of things come to mind looking at your examples.
First, the -S option sorts by size; you'll not be removing the oldest first, but the largest which is likely the most recent, and probably not what you want.
Secondly, I'd switch to the directory that has your backup files in it and use a dot (.) on the df command; this ensures that you are checking the correct device while still allowing it to be variable.
This is the way that I would go about it -- needs only one df and one ls call which will be more efficient:
This is just a basic example. With anything that deletes files I always like to have a 'no-exec' mode that lists what it might do until I'm comfortable that it works right. In any case, you might want to add something that limits the max number of files that it can delete or somesuch that prevents deleting all of the backup files.
The -B option on the df command causes it to list values in bytes; makes it easier. The -rlt option to ls lists files by time, showing size and in reverse order so that the oldest file (by modification time) is listed first. Both the ls and df commands are executed in a subshell so that the output can easily be pushed into the awk that does the real work.
yeah sorry copied wrong command.. ls command should have -t switch to sort by time.
this is a ubuntu lucid server box
ok to give some stuff im getting. a straight df command gives me
im only interested in the usb drive and the free space. (i was trying to maybe use percentage but that will be to complicated). I grep "usb" although I could make this more accurate. so this gives me.
which in the above example will give me
sweet the free space in kilobytes. using this I want to compare to an amount of free space I want to have available, say 20G or ~20000000 kilobytes (clearly its all good at the moment but will fill up)
now the file i want to remove can be identified with
on whichever the folder im interested in removing files.
Thanks for the example code but I really dont understand a lot of it. not sure what the "next" command is (or is it a variable). and dont see how it checks
the way I have done it before is to basically
: start
check if more than 20G available
>> if false remove oldest backup file and goto start
>> if true, out of loop, carry on with backup
so Ive got the checking if 20G down pat and ive got the removing oldest file down pat but i haven't got the looping.
ive looked at other examples of backup scripts but a lot are looking to just say keep only 4 backups on media etc not dynamically working out how much space is needed.
Hello All,
I need immediate help in creating shell script to call archivebkup.ksh script when archive file system capacity reaches threshold value or 60%
Need to identify the unique file system that reaches threshold value.
ex:
capacity
... (4 Replies)
HI all, im new to shell scripting. need your guidence for my script. i wrote one script and is attached here
Im explaining the requirement of script.
AIM: Shell script to run automatically as per scheduled and backup few network devices configurations. Script will contain a set of commands... (4 Replies)
Hi,
I need a script to assign variables the below paths
/appl/user_projects/domains/<xxx>/servers/<yyy>/logs
Where <xxx> can be any number and name of directories.
<yyy> can be another set of any number of names and directories.
I want to neglect all the <xxx> and <yyy>... (3 Replies)
Hi all
i am struggling in creating a restore of env files while doing applications clone.
the first file i created for copying the important configurations file
which is running perfect
now for reverting the changes i mean when i am restoring these files to its original places
i have to do... (7 Replies)
Hi Everyone,
we are running rsync with --backup mode, Are there any rsync options to remove backup folders on successful deployment?
Thanks in adv. (0 Replies)
First, I know that's a bad title. I couldn't think of anything short enough. ...
I wrote the following script to let me know when various parts of the network are down. It used to look like this before last weekend when I got over 500 emails about 1 host being down all weekend:
this is in the... (1 Reply)