Advertisement
ryanpetris

Multicraft - Backup Using API

May 10th, 2015
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.42 KB | None | 0 0
  1. <?php
  2. require('MulticraftAPI.php');
  3.  
  4. $api = new MulticraftAPI('http://<your multicraft url>/api.php', '<your username>', '<your api key>');
  5.  
  6. function getData($result) {
  7.     if (!$result['success']) {
  8.         die($result['errors'][0]);
  9.     }
  10.  
  11.     return $result['data'];
  12. }
  13.  
  14.  
  15. $date = date('YmdHis');
  16.  
  17. exec('mkdir /tmp/minecraft-backups-' . $date);
  18.  
  19. $servers = getData($api->listServers());
  20.  
  21. foreach ($servers['Servers'] as $server_id => $server_name) {
  22.     print("Backing up server " . $server_name . " (id " . $server_id . ")\n");
  23.     print("    Disabling automatic saving...\n");
  24.  
  25.     $api->sendConsoleCommand($server_id, 'save-on');
  26.     $api->sendConsoleCommand($server_id, 'save-all');
  27.     $api->sendConsoleCommand($server_id, 'save-off');
  28.  
  29.     sleep(10);
  30.  
  31.     print("    Compressing server directory...\n");
  32.  
  33.     exec('mkdir /tmp/minecraft-backups-' . $date . '/' . $server_name);
  34.     exec('tar czf /tmp/minecraft-backups-' . $date . '/' . $server_name . '/' . $date . '.tar.gz /<your multicraft path>/servers/server' . $server_id . '/* 2>/dev/null');
  35.  
  36.     print("    Enabling automatic saving...\n");
  37.  
  38.     $api->sendConsoleCommand($server_id, 'save-on');
  39. }
  40.  
  41. print("Sending backups to backup server...\n");
  42.  
  43. exec('RSYNC_PASSWORD="<your rsync password>" rsync -rt /tmp/minecraft-backups-' . $date . '/* rsync://<your rsync url>/');
  44. exec('rm -rf /tmp/minecraft-backups-' . $date);
  45.  
  46. print("Backup complete.\n");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement