Riaan's SysAdmin Blog

My tips, howtos, gotchas, snippets and stuff. Use at your own risk!

BackupsBashrestic

Restic create backup and set tag with date logic

Also see previous post https://blog.ls-al.com/bash-date-usage-for-naming if you are interested. This post is similar but more specific to restic tagging.

Below is a test script and a test run. At the time of restic backup I create a tag in order to do snapshot forget based on tags.

# cat backup-tags.sh
#!/bin/bash

create_tag () {
  tag=daily
  if [ $(date +%a) == Sun ]; then tag=weekly ; fi
  if [ $(date +%d) == 01 ]; then 
   tag=monthly
   if [ $(date +%b) == Jan ]; then
     tag=yearly
   fi
  fi
}
create_tag
echo backup policy:  $tag

create_tag_unit_test () {
  for i in {1..95}
  do 
      tdate=$(date -d +$i day)
      tag=daily
      if [ $(date -d +$i day +%a) == Sun ]; then tag=weekly ; fi
      if [ $(date -d +$i day +%d) == 01 ]; then
      tag=monthly
        if [ $(date -d +$i day +%b) == Jan ]; then
          tag=yearly
        fi
      fi
  printf %s - %s - %s |  $(date -d +$i day +%d) $(date -d +$i day +%a) $tag 
  if [ $(( $i %5 )) -eq 0 ]; then printf \n; fi
  done
}
create_tag_unit_test

Run

# ./backup-tags.sh
backup policy:  daily
22 - Fri - daily      | 23 - Sat - daily      | 24 - Sun - weekly     | 25 - Mon - daily      | 26 - Tue - daily      | 
27 - Wed - daily      | 28 - Thu - daily      | 29 - Fri - daily      | 30 - Sat - daily      | 01 - Sun - monthly    | 
02 - Mon - daily      | 03 - Tue - daily      | 04 - Wed - daily      | 05 - Thu - daily      | 06 - Fri - daily      | 
07 - Sat - daily      | 08 - Sun - weekly     | 09 - Mon - daily      | 10 - Tue - daily      | 11 - Wed - daily      | 
12 - Thu - daily      | 13 - Fri - daily      | 14 - Sat - daily      | 15 - Sun - weekly     | 16 - Mon - daily      | 
17 - Tue - daily      | 18 - Wed - daily      | 19 - Thu - daily      | 20 - Fri - daily      | 21 - Sat - daily      | 
22 - Sun - weekly     | 23 - Mon - daily      | 24 - Tue - daily      | 25 - Wed - daily      | 26 - Thu - daily      | 
27 - Fri - daily      | 28 - Sat - daily      | 29 - Sun - weekly     | 30 - Mon - daily      | 31 - Tue - daily      | 
01 - Wed - yearly     | 02 - Thu - daily      | 03 - Fri - daily      | 04 - Sat - daily      | 05 - Sun - weekly     | 
06 - Mon - daily      | 07 - Tue - daily      | 08 - Wed - daily      | 09 - Thu - daily      | 10 - Fri - daily      | 
11 - Sat - daily      | 12 - Sun - weekly     | 13 - Mon - daily      | 14 - Tue - daily      | 15 - Wed - daily      | 
16 - Thu - daily      | 17 - Fri - daily      | 18 - Sat - daily      | 19 - Sun - weekly     | 20 - Mon - daily      | 

Below is the restic backup script setting a tag and then snapshot forget based on the tag.

As always this is NOT tested use at your own risk.

My policy is:

  • weekly on Sunday
  • 01 of every month is a monthly except if 01 is also a new year which makes it a yearly
  • everything else is a daily
# cat desktop-restic.sh 
#!/bin/bash
### wake up backup server and restic backup to 3TB ZFS mirror
cd /root/scripts
./wake-backup-server.sh

source /root/.restic.env

## Quick and dirty logic for snapshot tagging
create_tag () {
  tag=daily
  if [ $(date +%a) == Sun ]; then tag=weekly ; fi
  if [ $(date +%d) == 01 ]; then
   tag=monthly
   if [ $(date +%b) == Jan ]; then
     tag=yearly
   fi
  fi
}

create_tag
restic backup -q /DATA /ARCHIVE --tag $tag --exclude *.vdi --exclude *.iso --exclude *.ova --exclude *.img --exclude *.vmdk

restic forget -q --tag daily --keep-last 7
restic forget -q --tag weekly --keep-last 4
restic forget -q --tag monthly --keep-last 12

if [ $tag == weekly ]; then
  restic -q prune
fi

sleep 1m
ssh user@192.168.1.250 sudo shutdown now

admin

Bio Info for Riaan