Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env php
- <?php
- chdir(dirname($argv[0]));
- if (count($argv)<2) {
- help();
- }
- function help() {
- print "backup.php -ch -d <backupdirectory> [ -n <numberofbackups> ]\n";
- print "-h Help.\n";
- print "-c Instead of copying the rrd files, make a symlink in the tmp directory to the actual location.\n";
- print "-d <backupdirectory> Directory to write the backup tarballs, with optional rotation.\n";
- print "-n If no number of backups is given, there will be no rotation.\n";
- exit(1);
- }
- $options = getopt("chd:n:");
- if (isset($options['d'])) {
- $backupdirectory=$options['d'];
- } else {
- print "Need option -d <backupdirectory>\n";
- exit(1);
- }
- if (isset($options['n'])) {
- $maxnumberofbackups=$options['n'];
- } else {
- $maxnumberofbackups=0;
- }
- $backupfilename='observium-backup.tar.gz';
- $configphp='/opt/observium/config.php';
- $cronfile='/etc/cron.d/observium';
- include($configphp);
- if (substr($backupdirectory,-1)!='/') {
- # we need a / at the end of the directory name
- $backupdirectory.='/';
- }
- if (!file_exists($backupdirectory)) {
- print "Backup directory does not exist or is not a directory.\n";
- exit(1);
- }
- $backupfilepath=$backupdirectory.$backupfilename;
- print "Rotating the old backups.\n";
- $maxbackupnumber=$maxnumberofbackups-1; # of backups includes the numberless one that will be written in this run
- for ($backupnumber=$maxbackupnumber; $backupnumber>0; $backupnumber--) {
- if ($backupnumber<$maxbackupnumber) {
- $olderbackupnumber=$backupnumber+1;
- if (file_exists($backupfilepath.'.'.$backupnumber)) {
- system('mv ' . $backupfilepath.'.'.$backupnumber . ' ' . $backupfilepath.'.'.$olderbackupnumber);
- }
- }
- if ($backupnumber==1) {
- if (file_exists($backupfilepath)) {
- system('mv ' . $backupfilepath . ' ' . $backupfilepath.'.'.$backupnumber);
- }
- } else {
- $newerbackupnumber=$backupnumber-1;
- if (file_exists($backupfilepath.'.'.$newerbackupnumber)) {
- system('mv ' . $backupfilepath.'.'.$newerbackupnumber . ' ' . $backupfilepath.'.'.$backupnumber);
- }
- }
- }
- print "Creating tmp directory ";
- $tmpdir=system('mktemp -d --tmpdir='.$backupdirectory.' tmp-XXXXXXXX');
- if (!file_exists($tmpdir)) {
- print "Failed to create tmp directory.\n";
- exit(1);
- }
- print "Copying config.php...";
- system('cp -a ' . $configphp . ' ' . $tmpdir, $cpconfigphpexitstatus);
- if ($cpconfigphpexitstatus==0) {
- print "cp config.php succeeded.\n";
- } else {
- print "cp config.php failed.\n";
- exit(1);
- }
- print "Copying $cronfile...";
- system('cp -a ' . $cronfile . ' ' . $tmpdir, $cpcronfileexitstatus);
- if ($cpcronfileexitstatus==0) {
- print "cp $cronfile succeeded.\n";
- } else {
- print "cp $cronfile failed.\n";
- exit(1);
- }
- $mysqlcmd = 'mysqldump --add-drop-table';
- $mysqlcmd .= ' --result-file=' . $tmpdir . '/backup.sql';
- $mysqlcmd .= ' --host=' . $config['db_host'];
- $mysqlcmd .= ' --user=' . $config['db_user'];
- $mysqlcmd .= ' --password=' . $config['db_pass'];
- $mysqlcmd .= ' ' . $config['db_name'];
- print "Starting to dump observium db...";
- system($mysqlcmd, $mysqldumpexitstatus);
- if ($mysqldumpexitstatus==0) {
- print "dump succeeded.\n";
- } else {
- print "dump failed.\n";
- exit(1);
- }
- if (isset($options['c'])) {
- print "Symlinking " . $config['rrd_dir'] . " in the tmp directory.\n";
- system('ln -s ' . $config['rrd_dir'] . ' ' . $tmpdir.'/rrd');
- } else {
- print "Copying rrd/ directory...";
- system('cp -a ' . $config['rrd_dir'] . ' ' . $tmpdir.'/rrd', $cprrdexitstatus);
- if ($cprrdexitstatus==0) {
- print "cp rrd/ succeeded.\n";
- } else {
- print "cp rrd/ failed.\n";
- exit(1);
- }
- }
- $tarcmd = 'tar --dereference -cz';
- $tarcmd .= ' -f ' . $backupfilepath;
- $tarcmd .= ' -C ' . $tmpdir . ' .';
- print "Creating tar archive from tmp directory...";
- system ($tarcmd, $tarexitstatus);
- if ($tarexitstatus==0) {
- print "tar succeeded.\n";
- print "Deleting tmp directory...";
- system ('rm -r ' . $tmpdir, $rmtmpdirexitstatus);
- if ($rmtmpdirexitstatus==0) {
- print "rm succeeded.\n";
- } else {
- print "rm failed.\n";
- exit(1);
- }
- } else {
- print "tar failed.\n";
- exit(1);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement