Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OpnSense
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
OpnSense
Commits
580d93d9
Commit
580d93d9
authored
Jul 31, 2015
by
Ad Schellevis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(gdrive backup) catch and log errors and apply code style
parent
eef2608a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
21 deletions
+35
-21
config.lib.inc
src/etc/inc/config.lib.inc
+35
-21
No files found.
src/etc/inc/config.lib.inc
View file @
580d93d9
...
@@ -355,37 +355,51 @@ function make_config_revision_entry($desc = null, $override_user = null) {
...
@@ -355,37 +355,51 @@ function make_config_revision_entry($desc = null, $override_user = null) {
* backup config to google drive and return current file list (/ info)
* backup config to google drive and return current file list (/ info)
*
*
*/
*/
function
backup_to_google_drive
()
{
function
backup_to_google_drive
()
$client
=
new
Google\API\Drive
()
;
{
$cnf
=
OPNsense\Core\Config
::
getInstance
();
$cnf
=
OPNsense\Core\Config
::
getInstance
();
if
(
$cnf
->
isValid
())
{
if
(
$cnf
->
isValid
())
{
$config
=
$cnf
->
object
()
;
$config
=
$cnf
->
object
();
if
(
isset
(
$config
->
system
->
remotebackup
)
&&
isset
(
$config
->
system
->
remotebackup
->
GDriveEnabled
)
&&
$config
->
system
->
remotebackup
->
GDriveEnabled
==
"on"
){
if
(
isset
(
$config
->
system
->
remotebackup
)
&&
isset
(
$config
->
system
->
remotebackup
->
GDriveEnabled
)
&&
$config
->
system
->
remotebackup
->
GDriveEnabled
==
"on"
)
{
$client
->
login
(
$config
->
system
->
remotebackup
->
GDriveEmail
->
__toString
(),
$config
->
system
->
remotebackup
->
GDriveP12key
->
__toString
()
);
try
{
$client
=
new
Google\API\Drive
();
$client
->
login
(
$config
->
system
->
remotebackup
->
GDriveEmail
->
__toString
(),
$config
->
system
->
remotebackup
->
GDriveP12key
->
__toString
());
}
catch
(
Exception
$e
)
{
log_error
(
"error connecting to Google Drive"
);
return
array
();
}
// backup source data to local strings (plain/encrypted)
// backup source data to local strings (plain/encrypted)
$confdata
=
file_get_contents
(
'/conf/config.xml'
)
;
$confdata
=
file_get_contents
(
'/conf/config.xml'
);
$confdata_enc
=
encrypt_data
(
$confdata
,
$config
->
system
->
remotebackup
->
GDrivePassword
->
__toString
())
;
$confdata_enc
=
encrypt_data
(
$confdata
,
$config
->
system
->
remotebackup
->
GDrivePassword
->
__toString
());
tagfile_reformat
(
$confdata_enc
,
$confdata_enc
,
"config.xml"
);
tagfile_reformat
(
$confdata_enc
,
$confdata_enc
,
"config.xml"
);
// read filelist (config-*.xml)
// read filelist (config-*.xml)
$files
=
$client
->
listFiles
(
$config
->
system
->
remotebackup
->
GDriveFolderID
->
__toString
());
try
{
$configfiles
=
array
();
$files
=
$client
->
listFiles
(
$config
->
system
->
remotebackup
->
GDriveFolderID
->
__toString
());
}
catch
(
Exception
$e
)
{
log_error
(
"error while fetching filelist from Google Drive"
);
return
array
();
}
$configfiles
=
array
();
foreach
(
$files
as
$file
)
{
foreach
(
$files
as
$file
)
{
if
(
fnmatch
(
"config-*.xml"
,
$file
[
'title'
]))
{
if
(
fnmatch
(
"config-*.xml"
,
$file
[
'title'
]))
{
$configfiles
[
$file
[
'title'
]]
=
$file
;
$configfiles
[
$file
[
'title'
]]
=
$file
;
}
}
}
}
krsort
(
$configfiles
);
krsort
(
$configfiles
);
// backup new file if changed (or if first in backup)
// backup new file if changed (or if first in backup)
$target_filename
=
"config-"
.
time
()
.
".xml"
;
$target_filename
=
"config-"
.
time
()
.
".xml"
;
if
(
count
(
$configfiles
)
>
1
)
{
if
(
count
(
$configfiles
)
>
1
)
{
// compare last backup with current, only save new
// compare last backup with current, only save new
$bck_data_enc_in
=
$client
->
download
(
$configfiles
[
array_keys
(
$configfiles
)[
0
]]);
$bck_data_enc_in
=
$client
->
download
(
$configfiles
[
array_keys
(
$configfiles
)[
0
]]);
$bck_data_enc
=
""
;
$bck_data_enc
=
""
;
tagfile_deformat
(
$bck_data_enc_in
,
$bck_data_enc
,
"config.xml"
)
;
tagfile_deformat
(
$bck_data_enc_in
,
$bck_data_enc
,
"config.xml"
)
;
$bck_data
=
decrypt_data
(
$bck_data_enc
,
$config
->
system
->
remotebackup
->
GDrivePassword
->
__toString
());
$bck_data
=
decrypt_data
(
$bck_data_enc
,
$config
->
system
->
remotebackup
->
GDrivePassword
->
__toString
());
if
(
$bck_data
==
$confdata
)
{
if
(
$bck_data
==
$confdata
)
{
$target_filename
=
null
;
$target_filename
=
null
;
...
@@ -393,7 +407,8 @@ function backup_to_google_drive() {
...
@@ -393,7 +407,8 @@ function backup_to_google_drive() {
}
}
if
(
!
is_null
(
$target_filename
))
{
if
(
!
is_null
(
$target_filename
))
{
log_error
(
"backup configuration as "
.
$target_filename
);
log_error
(
"backup configuration as "
.
$target_filename
);
$configfiles
[
$target_filename
]
=
$client
->
upload
(
$config
->
system
->
remotebackup
->
GDriveFolderID
->
__toString
(),
$target_filename
,
$confdata_enc
);
$configfiles
[
$target_filename
]
=
$client
->
upload
(
$config
->
system
->
remotebackup
->
GDriveFolderID
->
__toString
(),
$target_filename
,
$confdata_enc
);
krsort
(
$configfiles
);
krsort
(
$configfiles
);
}
}
...
@@ -402,16 +417,15 @@ function backup_to_google_drive() {
...
@@ -402,16 +417,15 @@ function backup_to_google_drive() {
$fcount
=
0
;
$fcount
=
0
;
foreach
(
$configfiles
as
$filename
=>
$file
)
{
foreach
(
$configfiles
as
$filename
=>
$file
)
{
if
(
$fcount
>=
$config
->
system
->
remotebackup
->
GDriveBackupCount
->
__toString
())
{
if
(
$fcount
>=
$config
->
system
->
remotebackup
->
GDriveBackupCount
->
__toString
())
{
log_error
(
"remove "
.
$filename
.
" from Google Drive"
);
log_error
(
"remove "
.
$filename
.
" from Google Drive"
);
$client
->
delete
(
$file
);
$client
->
delete
(
$file
);
}
}
$fcount
++
;
$fcount
++
;
}
}
}
}
// return filelist
// return filelist
return
$configfiles
;
return
$configfiles
;
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment