Functions starting with backup
create backups of a file
, while functions
starting with rotate
do the same but also replace the original file
with an empty one (this is useful for log rotation)
Note:: rotate()
and co will not work reliable on filenames that contain
dots but have no file extension (e.g. my.holiday.picture.jpg
is OK but
my.holiday.picture
is not)
prune_backups()
physically deletes all backups of a file
based on max_backups
prune_backups()
physically deletes all backups of a file
based on max_backups
rotate( file, size = 1, max_backups = Inf, compression = FALSE, dir = dirname(file), create_file = TRUE, dry_run = FALSE, verbose = dry_run ) backup( file, size = 0, max_backups = Inf, compression = FALSE, dir = dirname(file), dry_run = FALSE, verbose = dry_run ) prune_backups( file, max_backups, dir = dirname(file), dry_run = FALSE, verbose = dry_run ) prune_identical_backups( file, dir = dirname(file), dry_run = FALSE, verbose = dry_run ) rotate_date( file, age = 1, size = 1, max_backups = Inf, compression = FALSE, format = "%Y-%m-%d", dir = dirname(file), overwrite = FALSE, create_file = TRUE, now = Sys.Date(), dry_run = FALSE, verbose = dry_run ) backup_date( file, age = 1, size = 1, max_backups = Inf, compression = FALSE, format = "%Y-%m-%d", dir = dirname(file), overwrite = FALSE, now = Sys.Date(), dry_run = FALSE, verbose = dry_run ) rotate_time( file, age = -1, size = 1, max_backups = Inf, compression = FALSE, format = "%Y-%m-%d--%H-%M-%S", dir = dirname(file), overwrite = FALSE, create_file = TRUE, now = Sys.time(), dry_run = FALSE, verbose = dry_run ) backup_time( file, age = -1, size = 1, max_backups = Inf, compression = FALSE, format = "%Y-%m-%d--%H-%M-%S", dir = dirname(file), overwrite = FALSE, now = Sys.time(), dry_run = FALSE, verbose = dry_run )
file |
|
---|---|
size | scalar (if |
max_backups | maximum number of backups to keep
In addition for timestamped backups the following value are supported:
|
compression | Whether or not backups should be compressed |
dir |
|
create_file |
|
dry_run |
|
verbose |
|
age | minimum age after which to backup/rotate a file; can be
(if |
format | a scalar
|
overwrite |
|
now | The current |
file
as a character
scalar (invisibly)
backup()
, backup_date()
, and backup_time()
may create files (if the
specified conditions are met). They may also delete backups, based on
max_backup
.
rotate()
, rotate_date()
and rotate_time()
do the same, but in
addition delete the input file
, or replace it with an empty file if
create_file == TRUE
(the default).
prune_backups()
may delete files, depending on max_backups
.
prune_backups()
may delete files, depending on max_backups
.
In rotor, an interval is a character string in the form
"<number> <interval>"
. The following intervals are possible:
"day(s)"
, "week(s)"
, "month(s)"
, "quarter(s)"
, "year(s)"
.
The plural "s"
is optional (so "2 weeks"
and "2 week"
are equivalent).
Please be aware that weeks are
ISOweeks
and start on Monday (not Sunday as in some countries).
Interval strings can be used as arguments when backing up or rotating files, or for pruning backup queues (i.e. limiting the number of backups of a single) file.
When rotating/backing up "1 months"
means "make a new backup if the last
backup is from the preceding month". E.g if the last backup of myfile
is from 2019-02-01
then backup_time(myfile, age = "1 month")
will only
create a backup if the current date is at least 2019-03-01
.
When pruning/limiting backup queues, "1 year"
means "keep at least most
one year worth of backups". So if you call
backup_time(myfile, max_backups = "1 year")
on 2019-03-01
, it will create
a backup and then remove all backups of myfile
before 2019-01-01
.
# setup example file tf <- tempfile("test", fileext = ".rds") saveRDS(cars, tf) # create two backups of `tf`` backup(tf) backup(tf) list_backups(tf) # find all backups of a file#> [1] "/tmp/RtmpOlTNDu/test23637d58757c.1.rds" #> [2] "/tmp/RtmpOlTNDu/test23637d58757c.2.rds"# If `size` is set, a backup is only created if the target file is at least # that big. This is more useful for log rotation than for backups. backup(tf, size = "100 mb") # no backup becuase `tf` is to small list_backups(tf)#> [1] "/tmp/RtmpOlTNDu/test23637d58757c.1.rds" #> [2] "/tmp/RtmpOlTNDu/test23637d58757c.2.rds"# If `dry_run` is TRUE, backup() only shows what would happen without # actually creating or deleting files backup(tf, size = "0.1kb", dry_run = TRUE)#>#>#>#>#># rotate() is the same as backup(), but replaces `tf`` with an empty file rotate(tf) list_backups(tf)#> [1] "/tmp/RtmpOlTNDu/test23637d58757c.1.rds" #> [2] "/tmp/RtmpOlTNDu/test23637d58757c.2.rds" #> [3] "/tmp/RtmpOlTNDu/test23637d58757c.3.rds"#> [1] 0#> [1] 289 289 289# prune_backups() can remove old backups prune_backups(tf, 1) # keep only one backup list_backups(tf)#> [1] "/tmp/RtmpOlTNDu/test23637d58757c.1.rds"# rotate/backup_date() adds a date instead of an index # you should not mix index backups and timestamp backups # so we clean up first prune_backups(tf, 0) saveRDS(cars, tf) # backup_date() adds the date instead of an index to the filename backup_date(tf) # `age` sets the minimum age of the last backup before creating a new one. # the example below creates no new backup since it's less than a week # since the last. backup_date(tf, age = "1 week") # `now` overrides the current date. backup_date(tf, age = "1 year", now = "2999-12-31") list_backups(tf)#> [1] "/tmp/RtmpOlTNDu/test23637d58757c.2999-12-31.rds"# backup_time() creates backups with a full timestamp backup_time(tf) # It's okay to mix backup_date() and backup_time() list_backups(tf)#> [1] "/tmp/RtmpOlTNDu/test23637d58757c.2999-12-31.rds"#> [1] TRUE