{"id":57,"date":"2014-12-03T14:08:07","date_gmt":"2014-12-03T12:08:07","guid":{"rendered":"https:\/\/serverstuff.info\/wordpress\/?p=57"},"modified":"2014-12-04T10:36:08","modified_gmt":"2014-12-04T08:36:08","slug":"simple-off-site-mysql-backup","status":"publish","type":"post","link":"https:\/\/serverstuff.info\/wordpress\/2014\/12\/03\/simple-off-site-mysql-backup\/","title":{"rendered":"Simple off-site mysql and website backup"},"content":{"rendered":"<p>Here are two scripts I wrote that I needed to easily backup databases and some websites.<\/p>\n<p>Having the fact that I use SELinux .. with a custom data dir. I needed to this on my server:<\/p>\n<pre lang=\"bash\">\r\nyum install rssh\r\nmkdir \/backup\/.ssh\r\ncd \/backup\/.ssh\r\nssh-keygen -t rsa -f .\/backup\r\ncat backup.pub >authorized_keys\r\nsed -i 's\/#allowsftp\/allowsftp\/g' \/etc\/rssh.conf\r\nadduser -m backup -s \/usr\/bin\/rssh\r\nsemanage fcontext -at user_home_dir_t \/backup\/\r\nsemanage fcontext -at ssh_home_t \/backup\/.ssh\/\r\nsemanage fcontext -at ssh_home_t \/backup\/.ssh\/authorized_keys\r\nrestorecon -Rv \/backup\r\n<\/pre>\n<p>You just need to get \/backup\/.ssh\/backup private key file to the servers (make sure it&#8217;s chmod 0600 on the clients too ) you want to backup from so they can use it to connect to this server.<\/p>\n<p>I&#8217;ve put the following script on my mysql server<\/p>\n<pre lang=\"bash\">\r\nmkdir -p \/root\/scripts\r\ncat >\/root\/scripts\/backup.sh<<_EOF_\r\n#!\/bin\/bash\r\n\r\nUSER=\"root\"\r\nPASSWORD='l33tP4ssw0rd'\r\nHOST=\"localhost\"\r\nOUTPUT=\"\/backup\"\r\nPORT=5522\r\nKEY=\"\/root\/.ssh\/backup\"\r\nDEST=\"backup@192.168.1.1:\"\r\n\r\nmkdir \"${OUTPUT}\"\r\ndatabases=$(mysql --host=${HOST} --user=${USER} --password=${PASSWORD} --skip-column-names -s -N -e \"SHOW DATABASES;\")\r\n\r\nfor db in $databases; do\r\n        if [[ \"$db\" == \"information_schema\" ]] ; then\r\n                continue\r\n        fi\r\n        if [[ \"$db\" == \"performance_schema\" ]] ; then \r\n                continue\r\n        fi\r\n        if [[ \"$db\" != _* ]] ; then\r\n                file=sql_$db.`date +%Y%m%d_%s`.sql.gz\r\n                mysqldump --force --opt --host=${HOST} --user=$USER --password=${PASSWORD} --databases $db | gzip > $OUTPUT\/${file}\r\n                scp -oPort=${PORT} -i ${KEY} $OUTPUT\/${file} ${DEST}\r\n                rm -rf \"${OUTPUT}\/*\"\r\n        fi\r\ndone\r\n_EOF_\r\n\r\nchmod +x \/root\/scripts\/backup.sh\r\necho '0 2 * * * root nice \/root\/scripts\/backup.sh >\/dev\/null 2>&1' >> \/etc\/crontab\r\n<\/pre>\n<p>I&#8217;ve put the following script on my web server, feel free to adapt.<\/p>\n<pre lang=\"bash\">\r\nmkdir -p \/root\/scripts\r\ncat >\/root\/scripts\/backup.sh<<_EOF_\r\n#!\/bin\/bash\r\n\r\nOUTPUT=\"\/backup\"\r\nSOURCE=\"\/var\/www\/\"\r\nPORT=5522\r\nKEY=\"\/root\/.ssh\/backup\"\r\nDEST=\"backup@192.168.1.1:\"\r\n\r\nmkdir \"${OUTPUT}\"\r\n\r\nfor site in $(ls \"${SOURCE}\" | grep -Ev '(cgi-bin|html)')\r\ndo\r\n        file=site_$site.`date +%Y%m%d_%s`.tar.gz\r\n        tar -czf $OUTPUT\/${file} -C \/var\/www ${site}\r\n        scp -oPort=${PORT} -i ${KEY} $OUTPUT\/${file} ${DEST}\r\n        rm -rf \"${OUTPUT}\/*\"\r\ndone\r\nchmod +x \/root\/scripts\/backup.sh\r\necho '0 2 * * * root nice \/root\/scripts\/backup.sh >\/dev\/null 2>&1' >> \/etc\/crontab\r\n<\/pre>\n<p>You should probably do a scp connection to the server first so you can accept the newly learned key for the client.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here are two scripts I wrote that I needed to easily backup databases and some websites. Having the fact that I use SELinux .. with a custom data dir. I needed to this on my server: yum install rssh mkdir \/backup\/.ssh cd \/backup\/.ssh ssh-keygen -t rsa -f .\/backup cat backup.pub >authorized_keys sed -i &#8216;s\/#allowsftp\/allowsftp\/g&#8217; \/etc\/rssh.conf &hellip; <a href=\"https:\/\/serverstuff.info\/wordpress\/2014\/12\/03\/simple-off-site-mysql-backup\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Simple off-site mysql and website backup<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-57","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/serverstuff.info\/wordpress\/wp-json\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/serverstuff.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/serverstuff.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/serverstuff.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/serverstuff.info\/wordpress\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":5,"href":"https:\/\/serverstuff.info\/wordpress\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":62,"href":"https:\/\/serverstuff.info\/wordpress\/wp-json\/wp\/v2\/posts\/57\/revisions\/62"}],"wp:attachment":[{"href":"https:\/\/serverstuff.info\/wordpress\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/serverstuff.info\/wordpress\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/serverstuff.info\/wordpress\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}