@@ -11,8 +11,10 @@ async function postData(options, log, tmpDir) {
1111
1212 const dashboardDir = path . join ( tmpDir , 'dashboards' ) . replace ( / \\ / g, '/' ) ;
1313 const datasourceDir = path . join ( tmpDir , 'datasource' ) . replace ( / \\ / g, '/' ) ;
14+ const folderDir = path . join ( tmpDir , 'folder' ) . replace ( / \\ / g, '/' ) ;
1415 const dashBoards = await fsAsync . readdir ( dashboardDir ) ;
1516 const dataSources = await fsAsync . readdir ( datasourceDir ) ;
17+ const folders = await fsAsync . readdir ( folderDir ) ;
1618
1719 const host = options . host ? options . host : options . grafana . host ;
1820 const port = options . port ? options . port : options . grafana . port ;
@@ -74,6 +76,52 @@ async function postData(options, log, tmpDir) {
7476 log . debug ( `Grafana datasource restore not possible: ${ err } ` ) ;
7577 }
7678
79+ // restore folders
80+ try {
81+ await Promise . all ( folders . map ( async ( folderFile ) => {
82+ const folderPath = path . join ( folderDir , folderFile ) . replace ( / \\ / g, '/' ) ;
83+ const folderJson = JSON . parse ( await fsAsync . readFile ( folderPath , 'utf8' ) ) ;
84+ const folderTitle = folderJson . title ;
85+ const folderUid = folderJson . uid ;
86+
87+ log . debug ( `Try to restore folder: ${ folderTitle } (${ folderUid } )` ) ;
88+
89+ await axios ( {
90+ method : 'POST' ,
91+ baseURL : `${ protocol } ://${ host } :${ port } ` ,
92+ url : '/api/folders' ,
93+ data : folderJson ,
94+ headers : {
95+ 'Content-Type' : 'application/json' ,
96+ // eslint-disable-next-line quote-props
97+ 'Authorization' : `Bearer ${ apiKey } `
98+ } ,
99+ httpsAgent : new https . Agent ( {
100+ rejectUnauthorized : signedCertificates ,
101+ } ) ,
102+ } ) . then ( result => {
103+ log . debug ( `Folder "${ folderTitle } " restored: ${ JSON . stringify ( result . data ) } ` ) ;
104+ } ) . catch ( err => {
105+ const message = err . response ?. data ?. message || err . message ;
106+ log . debug ( `Cannot restore folder "${ folderTitle } ": ${ message } ` ) ;
107+ } ) ;
108+
109+ } ) ) ;
110+ } catch ( err ) {
111+ log . debug ( `Grafana folder restore not possible: ${ err } ` ) ;
112+ }
113+
114+ const folderMapPath = path . join ( folderDir , 'dashboard_folder_map.json' ) . replace ( / \\ / g, '/' ) ;
115+ let dashboardFolderMap = { } ;
116+
117+ try {
118+ const mapData = await fsAsync . readFile ( folderMapPath , 'utf8' ) ;
119+ dashboardFolderMap = JSON . parse ( mapData ) ;
120+ log . debug ( `Loaded dashboard-folder mapping with ${ Object . keys ( dashboardFolderMap ) . length } entries` ) ;
121+ } catch ( err ) {
122+ log . debug ( `No dashboard-folder mapping found or invalid: ${ err } ` ) ;
123+ }
124+
77125 // post Dashboards
78126 try {
79127 await Promise . all ( dashBoards . map ( async ( dashBoard ) => {
@@ -94,7 +142,14 @@ async function postData(options, log, tmpDir) {
94142 } ) ,
95143 } ;
96144
97- await axios . post ( '/api/dashboards/db' , dashBoardFile , apiOptions )
145+ let dashJson = JSON . parse ( dashBoardFile ) ;
146+ const folderUid = dashboardFolderMap [ dashBoardName ] ;
147+
148+ if ( folderUid && folderUid !== 'general' ) {
149+ dashJson . folderUid = folderUid ;
150+ }
151+
152+ await axios . post ( '/api/dashboards/db' , dashJson , apiOptions )
98153 . then ( result =>
99154 log . debug ( `dashboard restore for "${ dashBoardName } " finish: ${ JSON . stringify ( result . data ) } ` ) )
100155 . catch ( err =>
0 commit comments