You are not logged in.

#1 2021-06-04 07:07:14

Arciere
Member
Registered: 2020-10-07
Posts: 59

.

.

Last edited by Arciere (2025-04-30 08:35:59)

Offline

#2 2021-06-04 07:57:42

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: .

Sure it's possible. But you'd need to more clearly articulate your data model. Provide examples of the file names, and date formats, and how you'd like to determine a file is redundant.

See, for example: https://hg.sr.ht/~jasonwryan/shiv/brows … an?rev=tip


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2021-06-04 08:26:18

sabroad
Member
Registered: 2015-05-24
Posts: 242

Re: .

Arciere wrote:

I would like to append the dates to these backups that I'm going to do (thus keeping more than one save) and then automatically delete all previous backups except the last n in chronological order.
Is it possible to do this from the terminal, perhaps through an option of rm? Or maybe through a script? Or maybe via an rsync function?

rsync can do differential backups, eg.:

rsync -a --delete --quiet --inplace --backup --backup-dir=/location/to/backup/incr/$DAY /path/to/backup/ /location/to/backup/full/

Last edited by sabroad (2021-06-04 08:27:56)


--
saint_abroad

Offline

#4 2021-06-04 08:55:14

Arciere
Member
Registered: 2020-10-07
Posts: 59

Re: .

.

Last edited by Arciere (2025-04-30 08:35:47)

Offline

#5 2021-06-04 08:58:54

Arciere
Member
Registered: 2020-10-07
Posts: 59

Re: .

.

Last edited by Arciere (2025-04-30 08:35:39)

Offline

#6 2021-06-04 09:25:36

Arciere
Member
Registered: 2020-10-07
Posts: 59

Re: .

.

Last edited by Arciere (2025-04-30 08:35:32)

Offline

#7 2021-06-04 09:25:40

lambdarch
Member
From: France
Registered: 2021-01-10
Posts: 96
Website

Re: .

Arciere wrote:

I built the following formatting to attribute to the files for my backups:

filename\($(date +%d-%m-%Y_%H:%M:%S)\)

The result is the following:
filename(31-12-2020_17:46:59)
Could this be fine or should I format them in US format, for example?

You'd better prefix the filenames by a date in a naturally sortable format, like this:

$(date +%Y-%m-%d_%H:%M:%S)-filename

Then you work with an array:

files=(/backup/dir/*)
n_files=${#files[@]}
for ((n = 0; n < n_files - n_to_keep; n++)); do
  rm "${files[-1-n]}"
done

where n_to_keep is the number of files you want to keep, to define (not tested: prefix rm with echo before trying this smile ).

Offline

#8 2021-06-04 09:33:07

Arciere
Member
Registered: 2020-10-07
Posts: 59

Re: .

.

Last edited by Arciere (2025-04-30 08:35:23)

Offline

#9 2021-06-04 09:53:41

lambdarch
Member
From: France
Registered: 2021-01-10
Posts: 96
Website

Re: .

Yes, that's it, that's how we usually do it.
For your questions about arrays, see man bash wink
The line

files=(/backup/dir/*)

is a way to set up an array.

Offline

#10 2021-06-04 09:56:56

Arciere
Member
Registered: 2020-10-07
Posts: 59

Re: .

.

Last edited by Arciere (2025-04-30 08:35:13)

Offline

#11 2021-06-04 10:01:47

lambdarch
Member
From: France
Registered: 2021-01-10
Posts: 96
Website

Re: .

The language is bash ^^
"most practical" I don't know: this is a (bash-builtin) way to do it when files are named correctly.

Offline

#12 2021-06-04 13:10:48

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,361
Website

Re: .

If you'd be happy deleting files older than a specific date or number of days old (rather than keeping 'n' files) just use find:

find $path_to_backups -mtime +$days

When you have replaced the path and days variables, understand what the command does, and are satisfied with the resulting list of deletion candidates, just run the command again appending the flag '-delete'.

I'd also suggest if you are going to continue using arch linux that you invest some time in learning about your system.  I'd strongly advise working through the "linux tutorial" and "bash scripting" guides here: https://ryanstutorials.net/

Last edited by Trilby (2021-06-04 13:12:14)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#13 2021-06-04 13:23:02

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: .

I just use rsnapshot.


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#14 2021-06-04 16:56:54

Arciere
Member
Registered: 2020-10-07
Posts: 59

Re: .

.

Last edited by Arciere (2025-04-30 08:35:04)

Offline

#15 2021-06-04 17:25:51

lambdarch
Member
From: France
Registered: 2021-01-10
Posts: 96
Website

Re: .

It's brittle to use ls: you don't control the output format, it's not made for that.
If you want to use an external command, use find instead:

find /backupFolder -print0 | sort -z | tail -z -n+11 | xargs -0 rm -rfv

Last edited by lambdarch (2021-06-04 17:36:21)

Offline

#16 2021-06-04 17:46:22

Arciere
Member
Registered: 2020-10-07
Posts: 59

Re: .

.

Last edited by Arciere (2025-04-30 08:34:55)

Offline

#17 2021-06-04 17:53:19

lambdarch
Member
From: France
Registered: 2021-01-10
Posts: 96
Website

Re: .

The filenames can contain spaces, special characters and so on: that's why ls is a bad idea (this is a little mentioned in the link you quote by the way).

In fact there is no need for find here:

printf '%s\0' /backupFolder/* | tail -z -n+11 | xargs -0 rm -rfv

EDIT: I didn't see the -d in your ls: in this case, it's probably better to use find as above, adding -type d, and maybe -maxdepth 1.
To summarize, this would give:

find /backupFolder -maxdepth 1 -type d -print0 | sort -z | tail -z -n+11 | xargs -0 rm -rfv

Last edited by lambdarch (2021-06-04 18:05:18)

Offline

#18 2021-06-06 07:22:39

Arciere
Member
Registered: 2020-10-07
Posts: 59

Re: .

.

Last edited by Arciere (2025-04-30 08:34:47)

Offline

#19 2021-06-06 07:58:14

Arciere
Member
Registered: 2020-10-07
Posts: 59

Re: .

.

Last edited by Arciere (2025-04-30 08:34:37)

Offline

#20 2021-06-06 10:40:51

lambdarch
Member
From: France
Registered: 2021-01-10
Posts: 96
Website

Re: .

Well, it depends on what you really want to do in the end: list directories or not, search depth, etc.
In any case, it is useless to concatenate commands like that: if you use find, filter as much as you can with its options, then sort the result (if needed and only once), select some lines (if needed and only once), and finally act on these lines.

As you describe your need in #18, it seems to me that find is unnecessary:

printf '%s\0' /backupFolder/* | tail -z -n+11 | xargs -0 rm -rfv

should do the job.

You can replace rm -rfv with printf '%s\n' to see what it gives.
On a small example:

$ ls -l ./
total 0
-rw-r--r-- 1 user user 0  6 juin  12:35 20210601-filemane
-rw-r--r-- 1 user user 0  6 juin  12:35 20210602-filemane
-rw-r--r-- 1 user user 0  6 juin  12:35 20210603-filemane
-rw-r--r-- 1 user user 0  6 juin  12:35 20210604-filemane
-rw-r--r-- 1 user user 0  6 juin  12:35 20210605-filemane
-rw-r--r-- 1 user user 0  6 juin  12:35 20210606-filemane
-rw-r--r-- 1 user user 0  6 juin  12:35 20210607-filemane
-rw-r--r-- 1 user user 0  6 juin  12:35 20210608-filemane
-rw-r--r-- 1 user user 0  6 juin  12:35 20210609-filemane
$ printf '%s\0' ./* | tail -z -n+5 | xargs -0 printf '%s\n'
./20210605-filemane
./20210606-filemane
./20210607-filemane
./20210608-filemane
./20210609-filemane
$ 

Offline

#21 2021-06-06 12:56:43

Arciere
Member
Registered: 2020-10-07
Posts: 59

Re: .

.

Last edited by Arciere (2025-04-30 08:34:26)

Offline

#22 2021-06-06 13:47:33

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,361
Website

Re: .

Arciere wrote:

But as for the -z option next to both sort and tail I'm not finding anything: what are they for?

How are you not finding anything?  You're clearly not looking.  It is very clearly covered in the man pages for each of these tools.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#23 2021-06-06 13:49:33

Arciere
Member
Registered: 2020-10-07
Posts: 59

Re: .

.

Last edited by Arciere (2025-04-30 08:34:16)

Offline

#24 2021-06-06 13:51:53

Arciere
Member
Registered: 2020-10-07
Posts: 59

Re: .

.

Last edited by Arciere (2025-04-30 08:34:09)

Offline

#25 2021-06-06 13:58:01

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,361
Website

Re: .

If the input you are `sort'ing or 'tail'ing is delineated by NULL rather than newline, then yes, that's precisely what that flag is for.  Otherwise `sort` will just receive one line of input, so there is nothing to sort and it will output exactly what it was given as input completely unchanged.

Of course, you could quite easily test this yourself rather than asking.

You are expected to read man pages, and make an effort to learn on your own.

Do not take on "help vampire" habits.

Last edited by Trilby (2021-06-06 13:59:15)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

Board footer

Powered by FluxBB