{"id":1231,"date":"2018-07-20T10:47:45","date_gmt":"2018-07-20T15:47:45","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=1231"},"modified":"2018-07-20T10:47:45","modified_gmt":"2018-07-20T15:47:45","slug":"bash-date-usage-for-naming","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/bash-date-usage-for-naming\/","title":{"rendered":"Bash Date Usage For Naming"},"content":{"rendered":"
I am recording some scripting I used to create backup classification\/retention naming. It can be simplified into one function easily but I kept it like this so I can copy and paste easier which function I need. Script is pretty self explanatory but basically it takes today's date and name my eventual backup file name based on some logic.<\/p>\n
\r\n# cat test_class.sh \r\nHOSTNAME=$(hostname -s)\r\nBACKUP_CLASSIFICATION="UNCLASSIFIED"\r\n\r\nfunction retention_date() {\r\n MM=`date -d ${1} +%m`\r\n DD=`date -d ${1} +%d`\r\n DAY=`date -d ${1} +%u`\r\n\r\n if [ $DD == 01 ]\r\n then\r\n if [ $MM == 01 ]\r\n then\r\n BACKUP_CLASSIFICATION="YEARLY"\r\n else\r\n BACKUP_CLASSIFICATION="MONTHLY"\r\n fi\r\n else\r\n if (($DAY == 7)); then\r\n BACKUP_CLASSIFICATION="WEEKLY"\r\n else\r\n BACKUP_CLASSIFICATION="DAILY"\r\n fi\r\n fi\r\n\r\n}\r\n\r\nfunction retention_today() {\r\n MM=`date '+%m'`\r\n DD=`date '+%d'`\r\n DAY=`date +%u`\r\n \r\n if [ $DD == 01 ]\r\n then\r\n if [ $MM == 01 ]\r\n then\r\n BACKUP_CLASSIFICATION="YEARLY"\r\n else\r\n BACKUP_CLASSIFICATION="MONTHLY"\r\n fi\r\n else\r\n if (($DAY == 7)); then\r\n BACKUP_CLASSIFICATION="WEEKLY"\r\n else\r\n BACKUP_CLASSIFICATION="DAILY"\r\n fi\r\n fi\r\n\r\n}\r\n\r\necho "TEST TODAY"\r\nDATE=`date +%Y-%m-%d`\r\nretention_today\r\necho $HOSTNAME-$BACKUP_CLASSIFICATION-$DATE\r\n \r\necho \r\necho "TEST SPECIFIC DATES"\r\ntestD=(\r\n '2018-01-01'\r\n '2018-02-02'\r\n '2018-03-01'\r\n '2018-02-06'\r\n '2018-07-14'\r\n '2018-07-15'\r\n)\r\n\r\nfor D in "${testD[@]}"\r\ndo\r\n DATE=`date -d ${D} +%Y-%m-%d`\r\n retention_date $D\r\n echo $HOSTNAME-$BACKUP_CLASSIFICATION-$DATE\r\ndone\r\n<\/pre>\nRun and output.<\/p>\n
\r\n# .\/test_class.sh \r\nTEST TODAY\r\noci04-DAILY-2018-07-20\r\n\r\nTEST SPECIFIC DATES\r\noci04-YEARLY-2018-01-01\r\noci04-DAILY-2018-02-02\r\noci04-MONTHLY-2018-03-01\r\noci04-DAILY-2018-02-06\r\noci04-DAILY-2018-07-14\r\noci04-WEEKLY-2018-07-15\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"I am recording some scripting I used to create backup classification\/retention naming. It can be simplified into one function easily<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,10],"tags":[],"class_list":["post-1231","post","type-post","status-publish","format-standard","hentry","category-backups","category-bash"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1231","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=1231"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1231\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=1231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=1231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=1231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}