{"id":679,"date":"2010-08-12T10:03:11","date_gmt":"2010-08-12T08:03:11","guid":{"rendered":"http:\/\/www.netexpertise.eu\/en\/?p=679"},"modified":"2021-10-06T08:13:05","modified_gmt":"2021-10-06T06:13:05","slug":"archive-directory-when-modified","status":"publish","type":"post","link":"http:\/\/www.netexpertise.eu\/en\/systems\/linux\/archive-directory-when-modified.html","title":{"rendered":"Backup a Directory when Modified"},"content":{"rendered":"\n<p>I use this script to keep a full local backup of my tftp directory hosted on <a href=\"\/en\/category\/systems\/linux\">Linux<\/a>. It keeps the archive only if some of the files were modified. It lets me restore in a quicker way than using a heavy backup software, which can be used beside to secure the <a href=\"\/en\/category\/systems\/backup\">backup<\/a> elsewhere.<br>The shell script can be run for any directory indeed. There are the files produced on my TFTP server:<br>\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">[root@tftp_server backup]$ ls -l\n-rw-r----- 1 tftp tftp   124200 May  1 04:04 tftp-20100501.tgz\n-rw-r----- 1 tftp tftp  2108159 May 13 04:04 tftp-20100513.tgz\n-rw-r----- 1 tftp tftp  2108165 May 18 04:04 tftp-20100518.tgz\n-rw-r----- 1 tftp tftp  2108442 May 20 04:04 tftp-20100520.tgz\n-rw-r----- 1 tftp tftp  2108545 Jun  1 04:04 tftp-20100601.tgz\n-rw-r----- 1 tftp tftp   126382 Jun  3 04:04 tftp-20100603.tgz\n-rw-r----- 1 tftp tftp   126426 Aug  5 04:04 tftp-20100805.tgz\n-rw-r----- 1 tftp tftp   126485 Aug  7 04:04 tftp-20100807.tgz\n-rw-r----- 1 tftp tftp   126486 Aug 11 04:04 tftp-20100811.tgz<\/code><\/pre>\n\n\n\n<p>&nbsp;<br>Set the script in a cron job and it&#8217;s fully automated to run once every day.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">#!\/bin\/bash\n\n# Change these settings to your needs\ndir_to_backup=\/tftpboot\n# Backup path and file names\nbackup_dir=\/tmp\/backup\nbackup_file=tftp\n# Zipped tar output file\nof=$backup_dir\/$backup_file-$(date +%Y%m%d).tgz\n# Backup file owner and groupe owner\nowner=tftp\ngowner=tftp\ntmpfile=\/tmp\/$backup_file.tar\n# Number of backup versions to keep\nbackup_number=24\n\n# Backup\ncd $dir_to_backup\n# Split tar and zip commands\n# to create identical archives with the same checksum\n# if files have not changed\ntar cf $tmpfile .\n# Archive is removed if checksum identical to last archive\n# Do not save files timestamp to keep checksum consistent accross the days\ngzip -cn $tmpfile &gt; $of\nrm -f $tmpfile\nchmod 640 $of\nchown $owner:$gowner $of\n\n# Remove new archive if last backup checksum is identical\n[ `ls -1t $backup_dir\/$backup_file-*.tgz \\\n  | head -2 \\\n  | xargs md5sum \\\n  | awk '{print $1}' \\\n  | uniq \\\n  | wc -l` -eq 1 ] \\\n&amp;&amp; rm -f $of\n\n# Remove older versions\n# Keep the last 30 files\nls -1rt $backup_dir\/$backup_file-*.tgz \\\n  | head -n -$backup_number \\\n  | xargs rm -f<\/code><\/pre>\n\n\n\n<p><br>Another way would be to retrieve the date of the last backup, search in the directory if some files have been modified since and create the zip file if the condition is true.<br><br>The script would need some minor adjustments to run on MacOS on which the checksum command is md5 instead of md5sum. MacOS md5 returns a different output.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I use this script to keep a full local backup of my tftp directory hosted on Linux. It keeps the archive only if some of the files were modified. It lets me restore in a quicker way than using a heavy backup software, which can be used beside to secure the backup elsewhere.The shell script [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[319,11],"tags":[58,386],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Netexpertise - Backup a Directory when Modified<\/title>\n<meta name=\"description\" content=\"Backup a directory every day only when files have been modified, then rotate zipped files to keep x last versions\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.netexpertise.eu\/en\/systems\/linux\/archive-directory-when-modified.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Netexpertise - Backup a Directory when Modified\" \/>\n<meta property=\"og:description\" content=\"Backup a directory every day only when files have been modified, then rotate zipped files to keep x last versions\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.netexpertise.eu\/en\/systems\/linux\/archive-directory-when-modified.html\" \/>\n<meta property=\"og:site_name\" content=\"Netexpertise\" \/>\n<meta property=\"article:published_time\" content=\"2010-08-12T08:03:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-10-06T06:13:05+00:00\" \/>\n<meta name=\"author\" content=\"dave\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@netexpertise\" \/>\n<meta name=\"twitter:site\" content=\"@netexpertise\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.netexpertise.eu\/en\/systems\/linux\/archive-directory-when-modified.html\",\"url\":\"http:\/\/www.netexpertise.eu\/en\/systems\/linux\/archive-directory-when-modified.html\",\"name\":\"Netexpertise - Backup a Directory when Modified\",\"isPartOf\":{\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#website\"},\"datePublished\":\"2010-08-12T08:03:11+00:00\",\"dateModified\":\"2021-10-06T06:13:05+00:00\",\"author\":{\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa\"},\"description\":\"Backup a directory every day only when files have been modified, then rotate zipped files to keep x last versions\",\"breadcrumb\":{\"@id\":\"http:\/\/www.netexpertise.eu\/en\/systems\/linux\/archive-directory-when-modified.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.netexpertise.eu\/en\/systems\/linux\/archive-directory-when-modified.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.netexpertise.eu\/en\/systems\/linux\/archive-directory-when-modified.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.netexpertise.eu\/en\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Backup a Directory when Modified\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#website\",\"url\":\"https:\/\/www.netexpertise.eu\/en\/\",\"name\":\"Netexpertise\",\"description\":\"Systems \/ Networks \/ DevOps\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.netexpertise.eu\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa\",\"name\":\"dave\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g\",\"caption\":\"dave\"},\"sameAs\":[\"http:\/\/www.netexpertise.eu\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Netexpertise - Backup a Directory when Modified","description":"Backup a directory every day only when files have been modified, then rotate zipped files to keep x last versions","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.netexpertise.eu\/en\/systems\/linux\/archive-directory-when-modified.html","og_locale":"en_US","og_type":"article","og_title":"Netexpertise - Backup a Directory when Modified","og_description":"Backup a directory every day only when files have been modified, then rotate zipped files to keep x last versions","og_url":"http:\/\/www.netexpertise.eu\/en\/systems\/linux\/archive-directory-when-modified.html","og_site_name":"Netexpertise","article_published_time":"2010-08-12T08:03:11+00:00","article_modified_time":"2021-10-06T06:13:05+00:00","author":"dave","twitter_card":"summary_large_image","twitter_creator":"@netexpertise","twitter_site":"@netexpertise","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/www.netexpertise.eu\/en\/systems\/linux\/archive-directory-when-modified.html","url":"http:\/\/www.netexpertise.eu\/en\/systems\/linux\/archive-directory-when-modified.html","name":"Netexpertise - Backup a Directory when Modified","isPartOf":{"@id":"https:\/\/www.netexpertise.eu\/en\/#website"},"datePublished":"2010-08-12T08:03:11+00:00","dateModified":"2021-10-06T06:13:05+00:00","author":{"@id":"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa"},"description":"Backup a directory every day only when files have been modified, then rotate zipped files to keep x last versions","breadcrumb":{"@id":"http:\/\/www.netexpertise.eu\/en\/systems\/linux\/archive-directory-when-modified.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.netexpertise.eu\/en\/systems\/linux\/archive-directory-when-modified.html"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.netexpertise.eu\/en\/systems\/linux\/archive-directory-when-modified.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.netexpertise.eu\/en"},{"@type":"ListItem","position":2,"name":"Backup a Directory when Modified"}]},{"@type":"WebSite","@id":"https:\/\/www.netexpertise.eu\/en\/#website","url":"https:\/\/www.netexpertise.eu\/en\/","name":"Netexpertise","description":"Systems \/ Networks \/ DevOps","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.netexpertise.eu\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa","name":"dave","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g","caption":"dave"},"sameAs":["http:\/\/www.netexpertise.eu"]}]}},"_links":{"self":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts\/679"}],"collection":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/comments?post=679"}],"version-history":[{"count":0,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts\/679\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/media?parent=679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/categories?post=679"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/tags?post=679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}