Advertisement
Guest User

Untitled

a guest
Sep 18th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.14 KB | None | 0 0
  1. #!/usr/bin/env php
  2. <?php
  3.  
  4. chdir(dirname($argv[0]));
  5.  
  6. if (count($argv)<2) {
  7.   help();
  8. }
  9.  
  10. function help() {
  11.   print "backup.php -ch -d <backupdirectory> [ -n <numberofbackups> ]\n";
  12.   print "-h Help.\n";
  13.   print "-c Instead of copying the rrd files, make a symlink in the tmp directory to the actual location.\n";
  14.   print "-d <backupdirectory> Directory to write the backup tarballs, with optional rotation.\n";
  15.   print "-n If no number of backups is given, there will be no rotation.\n";
  16.   exit(1);
  17. }
  18.  
  19. $options = getopt("chd:n:");
  20. if (isset($options['d'])) {
  21.   $backupdirectory=$options['d'];
  22. } else {
  23.   print "Need option -d <backupdirectory>\n";
  24.   exit(1);
  25. }
  26. if (isset($options['n'])) {
  27.   $maxnumberofbackups=$options['n'];
  28. } else {
  29.   $maxnumberofbackups=0;
  30. }
  31.  
  32. $backupfilename='observium-backup.tar.gz';
  33. $configphp='/opt/observium/config.php';
  34. $cronfile='/etc/cron.d/observium';
  35. include($configphp);
  36.  
  37. if (substr($backupdirectory,-1)!='/') {
  38.   # we need a / at the end of the directory name
  39.  $backupdirectory.='/';
  40. }
  41. if (!file_exists($backupdirectory)) {
  42.   print "Backup directory does not exist or is not a directory.\n";
  43.   exit(1);
  44. }
  45. $backupfilepath=$backupdirectory.$backupfilename;
  46.  
  47. print "Rotating the old backups.\n";
  48. $maxbackupnumber=$maxnumberofbackups-1; # of backups includes the numberless one that will be written in this run
  49. for ($backupnumber=$maxbackupnumber; $backupnumber>0; $backupnumber--) {
  50.  
  51.   if ($backupnumber<$maxbackupnumber) {
  52.     $olderbackupnumber=$backupnumber+1;
  53.     if (file_exists($backupfilepath.'.'.$backupnumber)) {
  54.       system('mv ' . $backupfilepath.'.'.$backupnumber . ' ' . $backupfilepath.'.'.$olderbackupnumber);
  55.     }
  56.   }
  57.  
  58.   if ($backupnumber==1) {
  59.     if (file_exists($backupfilepath)) {
  60.       system('mv ' . $backupfilepath . ' ' . $backupfilepath.'.'.$backupnumber);
  61.     }
  62.   } else {
  63.     $newerbackupnumber=$backupnumber-1;
  64.     if (file_exists($backupfilepath.'.'.$newerbackupnumber)) {
  65.       system('mv ' . $backupfilepath.'.'.$newerbackupnumber . ' ' . $backupfilepath.'.'.$backupnumber);
  66.     }
  67.   }
  68. }
  69.  
  70. print "Creating tmp directory ";
  71. $tmpdir=system('mktemp -d --tmpdir='.$backupdirectory.' tmp-XXXXXXXX');
  72. if (!file_exists($tmpdir)) {
  73.   print "Failed to create tmp directory.\n";
  74.   exit(1);
  75. }
  76.  
  77. print "Copying config.php...";
  78. system('cp -a ' . $configphp . ' ' . $tmpdir, $cpconfigphpexitstatus);
  79. if ($cpconfigphpexitstatus==0) {
  80.   print "cp config.php succeeded.\n";
  81. } else {
  82.   print "cp config.php failed.\n";
  83.   exit(1);
  84. }
  85.  
  86. print "Copying $cronfile...";
  87. system('cp -a ' . $cronfile . ' ' . $tmpdir, $cpcronfileexitstatus);
  88. if ($cpcronfileexitstatus==0) {
  89.   print "cp $cronfile succeeded.\n";
  90. } else {
  91.   print "cp $cronfile failed.\n";
  92.   exit(1);
  93. }
  94.  
  95. $mysqlcmd = 'mysqldump --add-drop-table';
  96. $mysqlcmd .= ' --result-file=' . $tmpdir . '/backup.sql';
  97. $mysqlcmd .= ' --host=' . $config['db_host'];
  98. $mysqlcmd .= ' --user=' . $config['db_user'];
  99. $mysqlcmd .= ' --password=' . $config['db_pass'];
  100. $mysqlcmd .= ' ' . $config['db_name'];
  101.  
  102. print "Starting to dump observium db...";
  103. system($mysqlcmd, $mysqldumpexitstatus);
  104. if ($mysqldumpexitstatus==0) {
  105.   print "dump succeeded.\n";
  106. } else {
  107.   print "dump failed.\n";
  108.   exit(1);
  109. }
  110.  
  111. if (isset($options['c'])) {
  112.   print "Symlinking " . $config['rrd_dir'] . " in the tmp directory.\n";
  113.   system('ln -s ' . $config['rrd_dir'] . ' ' . $tmpdir.'/rrd');
  114. } else {
  115.   print "Copying rrd/ directory...";
  116.   system('cp -a ' . $config['rrd_dir'] . ' ' . $tmpdir.'/rrd', $cprrdexitstatus);
  117.   if ($cprrdexitstatus==0) {
  118.     print "cp rrd/ succeeded.\n";
  119.   } else {
  120.     print "cp rrd/ failed.\n";
  121.     exit(1);
  122.   }
  123. }
  124.  
  125. $tarcmd = 'tar --dereference -cz';
  126. $tarcmd .= ' -f ' . $backupfilepath;
  127. $tarcmd .= ' -C ' . $tmpdir . ' .';
  128. print "Creating tar archive from tmp directory...";
  129. system ($tarcmd, $tarexitstatus);
  130. if ($tarexitstatus==0) {
  131.   print "tar succeeded.\n";
  132.   print "Deleting tmp directory...";
  133.   system ('rm -r ' . $tmpdir, $rmtmpdirexitstatus);
  134.   if ($rmtmpdirexitstatus==0) {
  135.     print "rm succeeded.\n";
  136.   } else {
  137.     print "rm failed.\n";
  138.     exit(1);
  139.   }
  140. } else {
  141.   print "tar failed.\n";
  142.   exit(1);
  143. }
  144.  
  145. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement