{"id":3030,"date":"2020-04-16T18:22:47","date_gmt":"2020-04-16T23:22:47","guid":{"rendered":"http:\/\/www.incredigeek.com\/home\/?p=3030"},"modified":"2020-04-16T18:22:47","modified_gmt":"2020-04-16T23:22:47","slug":"librenms-backup-script","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/librenms-backup-script\/","title":{"rendered":"LibreNMS backup script"},"content":{"rendered":"\n<p>You should be able to copy and paste the following in a backup.sh file and then execute from cron.  Should work out of the box, but you can change the backup directory and the teams.sh path if needed\/wanted.<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"php\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">#!\/bin\/bash\n\n# LibreNMS backup script\n# Jan 1, 2019\n\nlDate=`date +%Y%m%d-%H%M`       # local date + hour minute\ndDate=`date +%Y%m%d`            # todays date\n\n# If you have the teams.sh script, you can trigger a backup notification\nALERT=\"\/home\/admin\/teams.sh -b\"\n\n# Directory to backup to\nbDir=\"\/backup\"\nbName=\"librenms_backup\"\n\n# MySQL settings for tar and sqldump\nsqlDir=\"\/var\/lib\/mysql\"\nsqlDB=\"librenms\"\nsqlUN=\"root\"\nsqlPW=\"\"\nLOG=\"${bDir}\/${lDate}-${bName}.log\"\n\n# Directory that contains data\ndDir=\"\/opt\/librenms\"\n\n# tar LibreNMS dir\n# tar SQL dir \"the whole thing with the innode files\n# sql dump of the db for extra redundancy\n\nif [ -d ${bDir} ]; then\necho \"backup dir exist, starting to backup\"\nelse\n        echo \"backup dir not available.  Quiting\"\n        exit 1\nfi\n\n${ALERT} \"Starting backup for ${bName} - `date`\"\n\nsystemctl stop mariadb httpd\n# LibreNMS data backup\ntar -zcvf ${bDir}\/${lDate}-${bName}.tgz ${dDir}\nif [ $? -eq 0 ]; then\n        echo \"Tar succesfully backed up ${bDir}\"\nelse\n        echo \"Tar failed while trying to backup ${dDir}\"\n        echo \" ${lDate} - Tar failed while trying to backup ${dDir}\" >> ${LOG}\n        ${ALERT} \"${lDate} - Tar failed while trying to backup ${dDir}\"\nfi\n\n# MySQL data backup\ntar -zcvf ${bDir}\/${lDate}-${bName}-mysql.tgz ${sqlDir}\nif [ $? -eq 0 ]; then\n        echo \"Tar succesfully backed up ${sqlDir}\"\nelse\n        echo \"Tar failed while trying to backup ${sqlDir}\"\n        echo \" ${lDate} - Tar failed while trying to backup ${sqlDir}\" >> ${LOG}\n        ${ALERT} \"${lDate} - Tar failed while trying to backup ${sqlDir}\"\nfi\n\nsystemctl start mariadb httpd\nsleep 5\n\n # SQL dump\nmysqldump -u ${sqlUN} -p'4rfvBHU8!' ${sqlDB} > ${bDir}\/${lDate}-${bName}.sql\nif [ $? -eq 0 ]; then\n        echo \"MySQL DB dumped\"\nelse\n        echo \"Ran into error while doing sql dump\"\n        echo \"${lDate} - Ran into error while doing sql dump\" >> ${LOG}\n        ${ALERT} \"${lDate} - Ran into error while doing sql dump\"\nfi\n\necho \"Removing old backups\"\nif ( ls ${bDir} | grep -q ${dDate} );then\n        find ${bDir}\/* -prune -mtime +31 -exec rm {} \\;\nelse\n        echo \"Looks like there are no backup files!  Aborting!!!\"\n        ${ALERT} \"${lDate} - Error: find failed to find any backup files in backup dir.  Aborting!!!\"\nfi\n\n${ALERT} \"Finished backup for ${bName} - `date`\"<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You should be able to copy and paste the following in a backup.sh file and then execute from cron. Should work out of the box, but you can change the backup directory and the teams.sh path if needed\/wanted.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,266],"tags":[75,211,353,173],"class_list":["post-3030","post","type-post","status-publish","format-standard","hentry","category-linux","category-scripts","tag-backup","tag-bash","tag-librenms","tag-script"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/3030","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=3030"}],"version-history":[{"count":2,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/3030\/revisions"}],"predecessor-version":[{"id":3231,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/3030\/revisions\/3231"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=3030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=3030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=3030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}