Blame view

sources/apps/updater/lib/backup.php 1.53 KB
d1bafeea1   Kload   [fix] Upgrade to ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  <?php
  
  /**
   * ownCloud - Updater plugin
   *
   * @author Victor Dubiniuk
   * @copyright 2012-2013 Victor Dubiniuk victor.dubiniuk@gmail.com
   *
   * This file is licensed under the Affero General Public License version 3 or
   * later.
   */
  
  namespace OCA\Updater;
  
  class Backup {
  
  	/**
  	 * Path to the current Backup instance
  	 * @var string
  	 */
  	protected static $path = '';
  
  	/**
  	 * Perform backup
  	 * @return string
  	 */
  	public static function create() {
  		try {
  			$locations = Helper::getPreparedLocations();
  			Helper::mkdir(self::getPath(), true);
  			foreach ($locations as $type => $dirs) {
  				$backupFullPath = self::getPath() . '/' . $type . '/';
  				Helper::mkdir($backupFullPath, true);
  				
  				foreach ($dirs as $name => $path) {
  					Helper::copyr($path, $backupFullPath . $name);
  				}
  			}
  		} catch (\Exception $e){
  			App::log('Backup creation failed. Check permissions.');
  			self::cleanUp();
  			throw $e;
  		}
  
  		return self::getPath();
  	}
  
  	/**
  	 * Generate unique backup path
  	 * or return existing one
  	 * @return string
  	 */
  	public static function getPath() {
  		if (!self::$path) {
  			$backupBase = App::getBackupBase();
  			$currentVersion = \OCP\Config::getSystemValue('version', '0.0.0');
  			$path = $backupBase . $currentVersion . '-';
  
  			do {
  				$salt = substr(md5(time()), 0, 8);
  			} while (file_exists($path . $salt));
  
  			self::$path = $path . $salt;
  		}
  		return self::$path;
  	}
  
  	public static function cleanUp(){
  		if (self::$path) {
  			Helper::removeIfExists(self::$path);
  		}
  		Helper::removeIfExists(App::getTempBase());
  	}
  
  }