A
A
Alexander Belikov2015-08-28 13:02:02
linux
Alexander Belikov, 2015-08-28 13:02:02

How to make a file rename loop in bash?

In general, the thing is, I am writing a script to create a backup, the trick is that it should have the name of the folder that it should backup and name the file in the $dir-backup.tar.gz style with the name of the folder and you need to pass it the maximum number of stored backups, now it it checks the number for me, if it is more then it deletes the oldest file and creates a new one with the name 0 at the end, how to make the 0 archive become 1, 1>2 , 2>3 and so on?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Azazel PW, 2015-08-28
@azazelpw

Isn't it easier to create an archive with a date? and specify the storage time?
You pack the archive and it looks like $date-$dir-backup.tar.gz
Or create a directory with the date and put the archives there.
The structure of the script is simple
#!/bin/bash
We declare variables.
And
for
do
done

S
Saboteur, 2015-08-28
@saboteur_kiev

But it's not recommended to do it just like this. You'll
have to constantly pull old folders, rename them (which changes the modification date).
It is more convenient to make the folder name not just a number, but a date. Then it is very easy to delete old folders by counting xx days from the current date. In addition, it is visually much easier to work with such backups.

A
Alexander Lebedev, 2015-08-28
@cawaleb

For a similar task, I wrote:
The files are located in subdirectories and are named:
XXX_2015_08_28_YYY_ZZZ.dmp.gz
3 new files are saved for each XXX .

O
Oleg Kleshchuk, 2015-08-28
@xenozauros

It seems to me that the easiest way is to fasten ldogrotate for such a task)) And what script to backup through pre-rotate.
But in general - as they wrote - so wrong. Create archive with date. And then nail the old ones by date.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question