33
44use phpbu \App \Result ;
55use phpbu \Backup \Cli \Cmd ;
6+ use phpbu \Backup \Cli \Exec ;
67use phpbu \Backup \Sync ;
78use phpbu \Backup \Target ;
9+ use phpbu \Util ;
810
9- class Rsync implements Sync
11+ /**
12+ * Rsync
13+ *
14+ * @package phpbu
15+ * @subpackage Backup
16+ * @author Sebastian Feldmann <sebastian@phpbu.de>
17+ * @copyright Sebastian Feldmann <sebastian@phpbu.de>
18+ * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
19+ * @link http://www.phpbu.de/
20+ * @since Class available since Release 1.1.0
21+ */
22+ class Rsync extends Cli implements Sync
1023{
1124 /**
12- * Configuration
25+ * Raw args
26+ *
27+ * @var string
28+ */
29+ protected $ args ;
30+
31+ /**
32+ * Remote username
33+ *
34+ * @var string
35+ */
36+ protected $ user ;
37+
38+ /**
39+ * Target host
40+ *
41+ * @var string
42+ */
43+ protected $ host ;
44+
45+ /**
46+ * Target path
47+ *
48+ * @var string
49+ */
50+ protected $ path ;
51+
52+ /**
53+ * Files to ignore, extracted from config string seperated by ":"
1354 *
1455 * @var array
1556 */
16- protected $ config ;
57+ protected $ excludes ;
58+
59+ /**
60+ * Should only the created backup be synced or the complete directory
61+ *
62+ * @var boolean
63+ */
64+ protected $ isDirSync ;
65+
66+ /**
67+ * Remove deleted files remotely as well
68+ *
69+ * @var boolean
70+ */
71+ protected $ delete ;
1772
1873 /**
1974 * (non-PHPdoc)
2075 * @see \phpbu\Backup\Sync::setup()
2176 */
2277 public function setup (array $ config )
2378 {
24- $ this ->config = $ config ;
79+ if (isset ($ config ['args ' ])) {
80+ $ this ->args = $ config ['args ' ];
81+ } else {
82+ if (empty ($ config ['path ' ])) {
83+ throw new Exception ('option \'path \' is missing ' );
84+ }
85+ $ this ->path = $ config ['path ' ];
86+
87+ if (isset ($ config ['user ' ])) {
88+ $ this ->user = $ config ['user ' ];
89+ }
90+ if (isset ($ config ['hots ' ])) {
91+ $ this ->host = $ config ['host ' ];
92+ }
93+
94+ $ this ->excludes = isset ($ config ['exclude ' ])
95+ ? array_map ('trim ' , explode (': ' , $ config ['exclude ' ]))
96+ : array ();
97+ $ this ->delete = isset ($ config ['delete ' ])
98+ ? Util \String::toBoolean ($ this ->config ['delete ' ], false )
99+ : false ;
100+ $ this ->isDirSync = isset ($ config ['dirsync ' ])
101+ ? Util \String::toBoolean ($ this ->config ['dirsync ' ], false )
102+ : false ;
103+ }
25104 }
26105
27106 /**
@@ -30,38 +109,57 @@ public function setup(array $config)
30109 */
31110 public function sync (Target $ target , Result $ result )
32111 {
33- throw new Exception ('NotImplementedException ' );
34- $ rsync = new Cmd ('rsync ' );
112+ $ rsync = new Cmd (Util \Cli::detectCmdLocation ('rsync ' ));
113+ $ targetFile = $ target ->getFilenameCompressed ();
114+ $ targetDir = dirname ($ targetFile );
115+ // std err > dev null
116+ $ rsync ->silence ();
35117
36- if (isset ($ this ->config ['args ' ])) {
37- $ exec = 'rsync ' . $ args ;
118+ if ($ this ->args ) {
119+ // pro mode define all arguments yourself
120+ // WARNING! no escaping is done by phpbu
121+ $ result ->debug ('WARNING: phpbu uses your rsync args without escaping ' );
122+ $ rsync ->addOption ($ this ->replaceTargetPlaceholder ($ this ->args ), $ target );
38123 } else {
39124 // use archive mode, verbose and compress if not allready done
40125 $ options = '-av ' . $ target ->shouldBeCompressed () ? '' : 'z ' ;
41126 $ rsync ->addOption ($ options );
42- // sync folder
43- // --delete
44127
45- // add target as source
46- $ rsync ->addOption ($ target ->getFilenameCompressed ());
128+ if (count ($ this ->excludes )) {
129+ foreach ($ this ->excludes as $ ex ) {
130+ $ rsync ->addOption ('--exclude ' , $ ex );
131+ }
132+ }
47133
48- $ syncTarget = '' ;
134+ // source handling
135+ if ($ this ->isDirSync ) {
136+ // sync the whole folder
137+ // delete remote files as well?
138+ if ($ this ->delete ) {
139+ $ rsync ->addOption ('--delete ' );
140+ }
141+ $ rsync ->addOption ($ targetDir );
142+ } else {
143+ // sync just the created backup
144+ $ rsync ->addOption ($ targetFile );
145+ }
49146
147+ // target handling
148+ $ syncTarget = '' ;
50149 // remote user
51- if (isset ( $ this ->config [ ' user ' ]) ) {
52- $ syncTarget .= $ this ->config [ ' user ' ] . '@ ' ;
150+ if (null !== $ this ->user ) {
151+ $ syncTarget .= $ this ->user . '@ ' ;
53152 }
54-
55153 // remote host
56- if (isset ( $ this ->config [ ' host ' ]) ) {
57- $ syncTarget .= $ this ->config [ ' host ' ] . ': ' ;
154+ if (null !== $ this ->host ) {
155+ $ syncTarget .= $ this ->host . ': ' ;
58156 }
59-
60157 // remote path
61- if ( isset ( $ this ->config [ ' path ' ])) {
62- $ syncTarget .= $ this -> config [ ' path ' ];
63- }
158+ $ syncTarget .= $ this ->path ;
159+
160+ $ rsync -> addOption ( $ syncTarget );
64161
162+ $ this ->execute ($ rsync );
65163 }
66164 }
67165}
0 commit comments