2

Problem

I would like to modify my backup script so that it automatically determines the correct date for the most recent backup. Currently, it just outputs the list of files on my internal and external drive, and I enter a date manually.

Here is what a typical session would look like. My input is in quotes.

$ sudo backup.sh
/dev/mapper/cr-root:
@             @-2016-07-27      @home-2016-06-24  @home-2016-08-01
@-2016-06-19  @-2016-07-29      @home-2016-06-27  @home-2016-08-04
@-2016-06-24  @-2016-08-01      @home-2016-07-06  @home-2016-08-07
@-2016-06-27  @-2016-08-04      @home-2016-07-09  @home-2016-08-13
@-2016-07-06  @-2016-08-07      @home-2016-07-11  @music
@-2016-07-09  @-2016-08-13      @home-2016-07-15  @music-2016-06-27
@-2016-07-11  @-2016-08-19      @home-2016-07-22  @music-2016-07-06
@-2016-07-15  @-clean           @home-2016-07-24  @music-2016-07-09
@-2016-07-22  @home             @home-2016-07-27
@-2016-07-24  @home-2016-06-19  @home-2016-07-29
-----------------------------------------------------------------------
/dev/sdb1:
@-2015-10-17  @-2016-07-27         @home-2015-12-30
@-2015-10-24  @-2016-07-29         @home-2016-01-01
@-2015-10-30  @-2016-08-01         @home-2016-01-03
@-2015-11-10  @-2016-08-04         @home-2016-01-07
@-2015-11-11  @-2016-08-07         @home-2016-01-10
@-2015-11-12  @-2016-08-13         @home-2016-01-12
@-2015-11-20  @-2016-08-19         @home-2016-01-14
@-2015-11-21  @-clean              @home-2016-01-21
@-2015-11-25  @Dropbox-2015-10-17  @home-2016-01-26
@-2015-12-01  @Dropbox-2015-10-24  @home-2016-01-28
@-2015-12-10  @Dropbox-2015-10-30  @home-2016-02-13
@-2015-12-14  @Dropbox-2015-11-10  @home-2016-02-15
@-2015-12-15  @Dropbox-2015-11-20  @home-2016-02-17
@-2015-12-19  @Dropbox-2015-11-25  @home-2016-02-18
@-2015-12-20  @Dropbox-2015-12-01  @home-2016-02-26
@-2015-12-30  @Dropbox-2015-12-10  @home-2016-03-01
@-2016-01-01  @Dropbox-2015-12-14  @home-2016-03-04
@-2016-01-03  @Dropbox-2015-12-19  @home-2016-03-07
@-2016-01-07  @Dropbox-2015-12-20  @home-2016-03-12
@-2016-01-10  @Dropbox-2015-12-30  @home-2016-03-18
@-2016-01-12  @Dropbox-2016-01-01  @home-2016-03-28
@-2016-01-14  @Dropbox-2016-01-03  @home-2016-03-29
@-2016-01-21  @Dropbox-2016-01-07  @home-2016-04-05
@-2016-01-26  @Dropbox-2016-01-10  @home-2016-04-06
@-2016-01-28  @Dropbox-2016-01-12  @home-2016-04-09
@-2016-02-04  @Dropbox-2016-01-14  @home-2016-04-15
@-2016-02-05  @Dropbox-2016-01-21  @home-2016-04-18
@-2016-02-06  @Dropbox-2016-01-26  @home-2016-04-22
@-2016-02-13  @Dropbox-2016-01-28  @home-2016-05-01
@-2016-02-15  @Dropbox-2016-02-04  @home-2016-05-06
@-2016-02-17  @Dropbox-2016-02-05  @home-2016-05-08
@-2016-02-18  @Dropbox-2016-02-06  @home-2016-05-24
@-2016-02-26  @Dropbox-2016-02-13  @home-2016-06-04
@-2016-03-01  @Dropbox-2016-02-15  @home-2016-06-08
@-2016-03-04  @Dropbox-2016-02-17  @home-2016-06-14
@-2016-03-07  @Dropbox-2016-02-18  @home-2016-06-17
@-2016-03-12  @Dropbox-2016-02-26  @home-2016-06-19
@-2016-03-18  @Dropbox-2016-03-01  @home-2016-06-24
@-2016-03-28  @Dropbox-2016-03-04  @home-2016-06-27
@-2016-03-29  @Dropbox-2016-03-07  @home-2016-07-06
@-2016-04-05  @Dropbox-2016-03-12  @home-2016-07-09
@-2016-04-06  @Dropbox-2016-03-18  @home-2016-07-11
@-2016-04-09  @Dropbox-2016-03-28  @home-2016-07-15
@-2016-04-15  @Dropbox-2016-03-29  @home-2016-07-22
@-2016-04-18  @Dropbox-2016-04-05  @home-2016-07-24
@-2016-04-22  @Dropbox-2016-04-06  @home-2016-07-27
@-2016-05-01  @Dropbox-2016-04-09  @home-2016-07-29
@-2016-05-06  @Dropbox-2016-04-15  @home-2016-08-01
@-2016-05-08  @Dropbox-2016-04-18  @home-2016-08-04
@-2016-05-24  @Dropbox-2016-04-22  @home-2016-08-07
@-2016-06-04  @home-2015-10-17     @home-2016-08-13
@-2016-06-08  @home-2015-10-24
@-2016-06-14  @home-2015-11-10
@-2016-06-17  @home-2015-11-11
@-2016-06-19  @home-2015-11-12
@-2016-06-24  @home-2015-11-20
@-2016-06-27  @home-2015-11-21
@-2016-07-06  @home-2015-11-25
@-2016-07-09  @home-2015-12-01
@-2016-07-11  @home-2015-12-10
@-2016-07-15  @home-2015-12-14
@-2016-07-22  @home-2015-12-19
@-2016-07-24  @home-2015-12-20
-----------------------------------------------------------------------
Backup @ [y/n]? "y"
Backup @home [y/n]? "y"
Backup @music [y/n]? "n"
Date of previous version: "2016-08-13"
...

For a given subvolume (such as @ or @home), I want to find the most recent dated snapshot that both have in common. In this case, I am looking for @-2016-08-13 and @home-2016-08-13, respectively. I am able to use find to output all of the files of the form @-date, for example, but I then need to compare the dates and find the most recent that both lists have. I know that a regex alone probably can't do this, but I'm not sure what will.

TL;DR

I want to find the most recent common date in a list of files without using timestamps. For instance, given these lists...

@-2016-07-24 @-2016-08-01 @-2016-08-13 @-2016-08-19

and

@-2016-07-24 @-2016-08-01 @-2016-08-13

I want to find the common file with the highest date, which is @-2016-08-13 in this case. I would prefer to use built-in UNIX utilities to do this.

  • Maybe join the sorted lists and tail the result? Probably needs your sort to have a -V (natural version) sort. The exact implementation will depend what you mean by a "list" (string? shell array? file?). – steeldriver Aug 19 '16 at 18:14
  • @steeldriver By list I just mean ls output. It can be in the ls -1 form or space-delimited. – Caleb Reister Aug 19 '16 at 18:17
1

To find @home and @ recent date

comm -12 <(grep -oP '@home-\K[0-9-]*' bkup.list | sort) \
         <(grep -oP '@-\K[0-9-]*' bkup.list | sort) |
tail -n 1
  • It works. I can modify it from there. – Caleb Reister Aug 29 '16 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.