{"id":314,"date":"2021-10-03T00:06:49","date_gmt":"2021-10-03T00:06:49","guid":{"rendered":"https:\/\/xuan.guru\/?p=314"},"modified":"2021-10-03T00:06:51","modified_gmt":"2021-10-03T00:06:51","slug":"php-script-to-email-a-sql-database-backup","status":"publish","type":"post","link":"https:\/\/xuan.co.uk\/?p=314","title":{"rendered":"PHP Script to email a SQL database backup"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\/\/ Create the mysql backup file\r\n\/\/ edit this section\r\nini_set(\"memory_limit\",\"40M\");\r\n$dbhost = \"xxx.com\"; \/\/ usually localhost\r\n$dbuser = \"username\";\r\n$dbpass = \"pass\";\r\n$dbname = \"db\";\r\n\r\n\/\/ don't need to edit below this section\r\n\r\n$backupfile = $dbname . date(\"d-m-Y\") . '.sql';\r\n$backupzip = $backupfile . '.tar.gz';\r\nsystem(\"mysqldump --opt --no-create-db -h $dbhost -u $dbuser -p$dbpass $dbname > $backupfile\");\r\nsystem(\"tar -czvf $backupzip $backupfile\");\r\n\r\n\r\n$fileatt = $backupzip; \/\/ Path to the file\r\n$fileatt_type = \"application\/octet-stream\"; \/\/ File Type\r\n$fileatt_name = $backupzip; \/\/ Filename that will be used for the file as the attachment\r\n\r\n$email_from = \"from_me@xxx.com\"; \/\/ Who the email is from\r\n$email_subject = \"Subject with date\".date(\"d-m-Y\"); \/\/ The Subject of the email\r\n$email_txt = \"\"; \/\/ Message that the email has in it\r\n\r\n$email_to = \"to_whom@xxx.com\"; \/\/ Who the email is too\r\n\r\n$headers = \"From: \".$email_from;\r\n\r\n$file = fopen($fileatt,'rb');\r\n$data = fread($file,filesize($fileatt));\r\nfclose($file);\r\n\r\n$semi_rand = md5(time());\r\n$mime_boundary = \"==Multipart_Boundary_x{$semi_rand}x\";\r\n\r\n$headers .= \"\\nMIME-Version: 1.0\\n\" .\r\n\"Content-Type: multipart\/mixed;\\n\" .\r\n\" boundary=\\\"{$mime_boundary}\\\"\";\r\n\r\n$email_message .= \"This is a multi-part message in MIME format.\\n\\n\" .\r\n\"--{$mime_boundary}\\n\" .\r\n\"Content-Type:text\/html; charset=\\\"iso-8859-1\\\"\\n\" .\r\n\"Content-Transfer-Encoding: 7bit\\n\\n\" .\r\n$email_message . \"\\n\\n\";\r\n\r\n$data = chunk_split(base64_encode($data));\r\n\r\n$email_message .= \"--{$mime_boundary}\\n\" .\r\n\"Content-Type: {$fileatt_type};\\n\" .\r\n\" name=\\\"{$fileatt_name}\\\"\\n\" .\r\n\/\/\"Content-Disposition: attachment;\\n\" .\r\n\/\/\" filename=\\\"{$fileatt_name}\\\"\\n\" .\r\n\"Content-Transfer-Encoding: base64\\n\\n\" .\r\n$data . \"\\n\\n\" .\r\n\"--{$mime_boundary}--\\n\";\r\n\r\n$ok = @mail($email_to, $email_subject, $email_message, $headers);\r\n\r\nif($ok) {\r\necho \"&lt;font face=verdana size=2>The file was successfully sent!&lt;\/font>\";\r\n} else {\r\ndie(\"Sorry but the email could not be sent. Please go back and try again!\");\r\n} \r\n\r\n\r\n\/\/ Delete the file from your server\r\nunlink($backupfile);\r\nunlink($backupzip);\r\n?><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[67,66],"class_list":["post-314","post","type-post","status-publish","format-standard","hentry","category-tech-tips","tag-database","tag-sql"],"_links":{"self":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=314"}],"version-history":[{"count":1,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/314\/revisions"}],"predecessor-version":[{"id":315,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/314\/revisions\/315"}],"wp:attachment":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}