FTP? What year is it?! 😎
On Thu, May 6, 2021 at 8:52 AM Terry Stone via observium < observium@observium.org> wrote:
For what its worth I have written a small php script to provide a 7 day rolling backup of my observium server to an external server and to send a confirmation email when backup is complete:
#!/usr/bin/env php
<?php // Script to backup Observium data created by Terry Stone on 3rd. May 2021 include "config.php"; // Collect database credentials $sql_username = $config['db_user']; $sql_userpass = $config['db_pass']; // FTP server credentials $ftp_server = '[ftp server name]'; $ftp_username = '[ftp server username]'; $ftp_userpass = '[ftp server password]'; // Email credentials $mail_to = $config['email']['default']; $mail_header = 'From: '.$config['email']['from'] . "\r\n" . 'X-Mailer: PHP/' . phpversion(); $mail_subject = "Observium Server Backup"; $mail_message = "Observium server backups sent to ".$ftp_server." on ".date("l jS F Y").":\n\n"; // Files to backup $file[0]= "observium-rrd.tar.gz"; $file[1]= "observium-dump.sql"; $file[2]= "config.php"; // Prefix filename to be sent to FTP server with day of the week $counter=0; while ($counter < count($file)) { $backupfile[$counter]=date("N").$file[$counter]; $counter++; } // Assemble observium backup fils exec('tar zcvf backup/'.$file[0].' rrd'); exec('/usr/bin/mysqldump -u observium --user='.$sql_username.' --password='.$sql_userpass.' --databases observium --add-drop-table --extended-insert > backup/'.$file[1]); exec('cp config.php backup/'.$file[2]); // Connect and login to FTP server $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server"); $login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass); // Change to passive mode ftp_pasv($ftp_conn,true); // Upload files to FTP server $counter=0; while ($counter < count($file)) { if (ftp_put($ftp_conn, $backupfile[$counter], "backup/".$file[$counter],FTP_BINARY)) { exec('cp backup/'.$file[$counter].' backup/'.$backupfile[$counter]); $status = date("H:i:s")." Successfully backed up ".$file[$counter]." to ".$backupfile[$counter]."\n"; } else { $status = date("H:i:s")." Error backing up ".$file[$counter]." to ".$backupfile[$counter]."\n"; } $mail_message = $mail_message.$status; $counter++; } // Close FTP connection ftp_close($ftp_conn); // Add final line to message body $mail_message=$mail_message."\nPlease do not reply to this message, mail account is send only.\n"; // Send Completion message mail($mail_to,$mail_subject,$mail_message,$mail_header); ?>
It is set to run nightly with this Cron Job:
# Run nightly backup and send files to offshore FTP server 0 02 * * * root cd /opt/observium && php observiumbackup.php
The backups are also held on the Observium Server in /opt/observium/backups. You need to make sure the backup files have the correct permissions.
Hope this will help someone. An observium server that has run for years holds a very large amount of valuable historical data and servers can fail so its good to have that data in a safe place!
Terry Stone. _______________________________________________ observium mailing list observium@observium.org http://postman.memetic.org/cgi-bin/mailman/listinfo/observium