{"id":1421,"date":"2019-11-21T09:15:32","date_gmt":"2019-11-21T15:15:32","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=1421"},"modified":"2020-04-12T12:13:44","modified_gmt":"2020-04-12T17:13:44","slug":"restic-create-backup-and-set-tag-with-date-logic","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/restic-create-backup-and-set-tag-with-date-logic\/","title":{"rendered":"Restic create backup and set tag with date logic"},"content":{"rendered":"

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

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.<\/p>\n

# cat backup-tags.sh\n#!\/bin\/bash\n\ncreate_tag () {\n  tag=daily\n  if [ $(date +%a) == Sun ]; then tag=weekly ; fi\n  if [ $(date +%d) == 01 ]; then \n   tag=monthly\n   if [ $(date +%b) == Jan ]; then\n     tag=yearly\n   fi\n  fi\n}\ncreate_tag\necho backup policy:  $tag\n\ncreate_tag_unit_test () {\n  for i in {1..95}\n  do \n      tdate=$(date -d +$i day)\n      tag=daily\n      if [ $(date -d +$i day +%a) == Sun ]; then tag=weekly ; fi\n      if [ $(date -d +$i day +%d) == 01 ]; then\n      tag=monthly\n        if [ $(date -d +$i day +%b) == Jan ]; then\n          tag=yearly\n        fi\n      fi\n  printf %s - %s - %s |  $(date -d +$i day +%d) $(date -d +$i day +%a) $tag \n  if [ $(( $i %5 )) -eq 0 ]; then printf \\n; fi\n  done\n}\ncreate_tag_unit_test<\/code><\/pre>\n

Run<\/h3>\n
# .\/backup-tags.sh\nbackup policy:  daily\n22 - Fri - daily      | 23 - Sat - daily      | 24 - Sun - weekly     | 25 - Mon - daily      | 26 - Tue - daily      | \n27 - Wed - daily      | 28 - Thu - daily      | 29 - Fri - daily      | 30 - Sat - daily      | 01 - Sun - monthly    | \n02 - Mon - daily      | 03 - Tue - daily      | 04 - Wed - daily      | 05 - Thu - daily      | 06 - Fri - daily      | \n07 - Sat - daily      | 08 - Sun - weekly     | 09 - Mon - daily      | 10 - Tue - daily      | 11 - Wed - daily      | \n12 - Thu - daily      | 13 - Fri - daily      | 14 - Sat - daily      | 15 - Sun - weekly     | 16 - Mon - daily      | \n17 - Tue - daily      | 18 - Wed - daily      | 19 - Thu - daily      | 20 - Fri - daily      | 21 - Sat - daily      | \n22 - Sun - weekly     | 23 - Mon - daily      | 24 - Tue - daily      | 25 - Wed - daily      | 26 - Thu - daily      | \n27 - Fri - daily      | 28 - Sat - daily      | 29 - Sun - weekly     | 30 - Mon - daily      | 31 - Tue - daily      | \n01 - Wed - yearly     | 02 - Thu - daily      | 03 - Fri - daily      | 04 - Sat - daily      | 05 - Sun - weekly     | \n06 - Mon - daily      | 07 - Tue - daily      | 08 - Wed - daily      | 09 - Thu - daily      | 10 - Fri - daily      | \n11 - Sat - daily      | 12 - Sun - weekly     | 13 - Mon - daily      | 14 - Tue - daily      | 15 - Wed - daily      | \n16 - Thu - daily      | 17 - Fri - daily      | 18 - Sat - daily      | 19 - Sun - weekly     | 20 - Mon - daily      | <\/code><\/pre>\n

Below is the restic backup script setting a tag and then snapshot forget based on the tag.<\/p>\n

As always this is NOT tested use at your own risk.<\/p>\n

My policy is:<\/p>\n

    \n
  • weekly on Sunday<\/li>\n
  • 01 of every month is a monthly except if 01 is also a new year which makes it a yearly<\/li>\n
  • everything else is a daily<\/li>\n<\/ul>\n
    # cat desktop-restic.sh \n#!\/bin\/bash\n### wake up backup server and restic backup to 3TB ZFS mirror\ncd \/root\/scripts\n.\/wake-backup-server.sh\n\nsource \/root\/.restic.env\n\n## Quick and dirty logic for snapshot tagging\ncreate_tag () {\n  tag=daily\n  if [ $(date +%a) == Sun ]; then tag=weekly ; fi\n  if [ $(date +%d) == 01 ]; then\n   tag=monthly\n   if [ $(date +%b) == Jan ]; then\n     tag=yearly\n   fi\n  fi\n}\n\ncreate_tag\nrestic backup -q \/DATA \/ARCHIVE --tag $tag --exclude *.vdi --exclude *.iso --exclude *.ova --exclude *.img --exclude *.vmdk\n\nrestic forget -q --tag daily --keep-last 7\nrestic forget -q --tag weekly --keep-last 4\nrestic forget -q --tag monthly --keep-last 12\n\nif [ $tag == weekly ]; then\n  restic -q prune\nfi\n\nsleep 1m\nssh user@192.168.1.250 sudo shutdown now<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

    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<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,10,99],"tags":[],"class_list":["post-1421","post","type-post","status-publish","format-standard","hentry","category-backups","category-bash","category-restic"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=1421"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1421\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=1421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=1421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=1421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}