@@ -30,9 +30,11 @@ addition, they create an empty file in place of the original one. This
3030is useful for log rotation. ` backup() ` , ` backup_date() ` and
3131` backup_time() ` do the same but keep the original file.
3232
33- See the [ function
33+ rotor also includes a few utility functions for examining backups of a
34+ file: ` list_backups() ` , ` backup_info() ` , ` n_backups ` , ` newest_backup() ` ,
35+ ` oldest_backup() ` . See the [ function
3436reference] ( https://s-fleck.github.io/rotor/reference/index.html ) for
35- more details
37+ details.
3638
3739## Installation
3840
@@ -67,6 +69,8 @@ tf <- file.path(td, "mylogfile.log")
6769writeLines(" An important message" , tf )
6870```
6971
72+ ### Indexed backups
73+
7074` backup() ` makes a copy of a file and inserts an index between the
7175filename and the file extension. The file with the index ` 1 ` is always
7276the most recently made backup.
@@ -79,8 +83,8 @@ backup(tf, compression = TRUE)
7983
8084# display backups of a file
8185list_backups(tf )
82- # > [1] "/tmp/RtmplU947x /rotor/mylogfile.1.log.zip"
83- # > [2] "/tmp/RtmplU947x /rotor/mylogfile.2.log"
86+ # > [1] "/tmp/RtmppxEUB8 /rotor/mylogfile.1.log.zip"
87+ # > [2] "/tmp/RtmppxEUB8 /rotor/mylogfile.2.log"
8488```
8589
8690` rotate() ` also backs up a file, but replaces the original file with an
@@ -89,9 +93,9 @@ empty one.
8993``` r
9094rotate(tf )
9195list_backups(tf )
92- # > [1] "/tmp/RtmplU947x /rotor/mylogfile.1.log"
93- # > [2] "/tmp/RtmplU947x /rotor/mylogfile.2.log.zip"
94- # > [3] "/tmp/RtmplU947x /rotor/mylogfile.3.log"
96+ # > [1] "/tmp/RtmppxEUB8 /rotor/mylogfile.1.log"
97+ # > [2] "/tmp/RtmppxEUB8 /rotor/mylogfile.2.log.zip"
98+ # > [3] "/tmp/RtmppxEUB8 /rotor/mylogfile.3.log"
9599
96100# the original file is now empty
97101readLines(tf )
@@ -114,10 +118,10 @@ backup(tf, max_backups = 4)
114118backup(tf , max_backups = 4 )
115119
116120list_backups(tf )
117- # > [1] "/tmp/RtmplU947x /rotor/mylogfile.1.log"
118- # > [2] "/tmp/RtmplU947x /rotor/mylogfile.2.log"
119- # > [3] "/tmp/RtmplU947x /rotor/mylogfile.3.log"
120- # > [4] "/tmp/RtmplU947x /rotor/mylogfile.4.log.zip"
121+ # > [1] "/tmp/RtmppxEUB8 /rotor/mylogfile.1.log"
122+ # > [2] "/tmp/RtmppxEUB8 /rotor/mylogfile.2.log"
123+ # > [3] "/tmp/RtmppxEUB8 /rotor/mylogfile.3.log"
124+ # > [4] "/tmp/RtmppxEUB8 /rotor/mylogfile.4.log.zip"
121125```
122126
123127We can also use ` prune_backups() ` to delete old backups. Other than
@@ -129,17 +133,58 @@ delete all backups.
129133prune_backups(tf , max_backups = 0 )
130134```
131135
132- Besides creating backup up with an index, ** rotor** can also create
133- timestamped backups.
136+ ## Timestamped backups
137+
138+ ** rotor** can also create timestamped backups. ` backup_date() ` creates
139+ uses a Date (` yyyy-mm-dd ` ) timestamp, ` backup_time() ` uses a full
140+ datetime-stamp by default (` yyyy-mm-dd--hh-mm-ss ` ). The format of the
141+ timestamp can be modified with a subset of the formatting tokens
142+ understood by ` strftime() ` (within certain restrictions). Backups
143+ created with both functions are compatible with each other (but not with
144+ those created with
145+ ` backup_index() ` ).
134146
135147``` r
136- backup_date(tf )
137- rotate_time(tf )
138- list_backups(tf )
139- # > [1] "/tmp/RtmplU947x/rotor/mylogfile.2019-05-28--07-51-44.log"
140- # > [2] "/tmp/RtmplU947x/rotor/mylogfile.2019-05-28.log"
148+ # be default backup_date() only makes a backup if the last backups is younger
149+ # than 1 day, so we set `age` to -1 for this example
150+ backup_date(tf , age = - 1 )
151+ backup_date(tf , format = " %Y-%m" , age = - 1 )
152+ backup_time(tf )
153+ backup_time(tf , format = " %Y-%m-%d_%H-%M-%S" ) # Python logging
154+ backup_time(tf , format = " %Y%m%dT%H%M%S" ) # ISO 8601 compatible
155+
156+ backup_info(tf )
157+ # > path
158+ # > 1 /tmp/RtmppxEUB8/rotor/mylogfile.2019-05-30_23-15-33.log
159+ # > 2 /tmp/RtmppxEUB8/rotor/mylogfile.2019-05-30--23-15-33.log
160+ # > 5 /tmp/RtmppxEUB8/rotor/mylogfile.20190530T231533.log
161+ # > 3 /tmp/RtmppxEUB8/rotor/mylogfile.2019-05-30.log
162+ # > 4 /tmp/RtmppxEUB8/rotor/mylogfile.2019-05.log
163+ # > dir name sfx ext size isdir mode
164+ # > 1 /tmp/RtmppxEUB8/rotor mylogfile 2019-05-30_23-15-33 log 26 FALSE 664
165+ # > 2 /tmp/RtmppxEUB8/rotor mylogfile 2019-05-30--23-15-33 log 26 FALSE 664
166+ # > 5 /tmp/RtmppxEUB8/rotor mylogfile 20190530T231533 log 26 FALSE 664
167+ # > 3 /tmp/RtmppxEUB8/rotor mylogfile 2019-05-30 log 26 FALSE 664
168+ # > 4 /tmp/RtmppxEUB8/rotor mylogfile 2019-05 log 26 FALSE 664
169+ # > mtime ctime atime uid gid
170+ # > 1 2019-05-30 23:15:33 2019-05-30 23:15:33 2019-05-30 23:15:33 1000 1000
171+ # > 2 2019-05-30 23:15:33 2019-05-30 23:15:33 2019-05-30 23:15:33 1000 1000
172+ # > 5 2019-05-30 23:15:33 2019-05-30 23:15:33 2019-05-30 23:15:33 1000 1000
173+ # > 3 2019-05-30 23:15:33 2019-05-30 23:15:33 2019-05-30 23:15:33 1000 1000
174+ # > 4 2019-05-30 23:15:33 2019-05-30 23:15:33 2019-05-30 23:15:33 1000 1000
175+ # > uname grname timestamp
176+ # > 1 hoelk hoelk 2019-05-30 23:15:33
177+ # > 2 hoelk hoelk 2019-05-30 23:15:33
178+ # > 5 hoelk hoelk 2019-05-30 23:15:33
179+ # > 3 hoelk hoelk 2019-05-30 00:00:00
180+ # > 4 hoelk hoelk 2019-05-01 00:00:00
141181```
142182
183+ If we examine the “timestamp” column in the example above, we see that
184+ missing date information is always interpreted as the start of the
185+ period; i.e. so ` "2019-01" ` is equivalent to ` "2019-01-01--00--00--00" `
186+ for all intentds and purposes.
187+
143188``` r
144189prune_backups(tf , max_backups = 0 ) # cleanup
145190list_backups(tf )
0 commit comments