Skip to content
Snippets Groups Projects
Commit 450c66f3 authored by arthur.yueh's avatar arthur.yueh
Browse files

Merge branch 'thinrope-master'

parents c6f629ae 7b5d6a5d
No related branches found
No related tags found
No related merge requests found
*.pyc *.pyc
.idea .idea
\ No newline at end of file __pycache__
_OUTPUT_
# Grafana Dashboard Backup Tool # Grafana Dashboard Backup Tool
Some python programs to call Grafana API to: A Python-based application to backup Grafana settings via [Grafana API](http://docs.grafana.org/http_api/overview/).
* Save every datasource to each datasource file.
* **saveDatasources.py**
* Save every folder to each datasource file.
* **saveFolders.py**
* Save every dashboard to each dashboard file.
* **saveDashboards.py**
* Create datasource from a backup file.
* **createDatasource.py**
* Create dashboard from a backup file.
* **createDashboard.py**
* Create folder from a backup file. (supported from Grafana v.5.0.*)
* **createFolder.py**
There a three convenient script files: ## Requirements
* bash
* python 2.7, python 3.x with `requests` library installed (`pip install requests` ?)
* access to a Grafana 3.0 API server
1. **backup_grafana.sh** ## Configuration
2. **restore_dashboards.sh**
3. **restore_datasources.sh**
3. **restore_folders.sh**
you can use them to `gafana-backup-tool` uses environment variables to define the connection to a Grafana server, or alternatively hard-coded settings in `src/grafanaSettings.py`.
You need to add the following to your `.bashrc` or execute once before using the tool:
```bash
export GRAFANA_URL=http://some.host.org:3000/
export GRAFANA_TOKEN=eyJrIjoidUhaU2ZQQndrWFN3RRVkUnVfrT56a1JoaG9KWFFObEgiLCJuIjoiYWRtaW4iLCJpZCI6MX0=
```
1. **backup all datasources, dashboards and folders.** There is [official documentation](http://docs.grafana.org/http_api/auth/) how to obtain the `TOKEN` for your installation.
* e.g.: sh backup_grafana.sh
2. **restore dashboards from your dashboard backup folder.**
* e.g: sh restore_dashboards.sh /tmp/dashboards/2016-10-10_12:00:00
3. **restore datasources from your datasource backup folder.**
* e.g.: sh restore_datasources.sh /tmp/datasources/2016-10-10_12:00:00
3. **restore folders from your folder backup folder.**
* e.g.: sh restore_folders.sh /tmp/folders/2016-10-10_12:00:00
[Grafana API document](http://docs.grafana.org/http_api/overview/)
## ENV:
* python 2.7, python 3
* Need to **pip install requests** library
* Garafana 3.0 API
## Setting
1. Export the environment variables bellow
2. GRAFANA_URL (the default url is http://localhost:3000)
3. GRAFANA_TOKEN
You can see how to get token from here: [Grafana Web page](http://docs.grafana.org/http_api/auth/)
## How to Use ## How to Use
* First edit **grafanaSettings.py** as above. * First perform **Configuration** as described above.
* Use **saveDashboards.py** to save each dashboard to each file. * Use `backup_grafana.sh` to backup all your dashboards, datasources and folders to the `_OUTPUT_` subdirectory of the current directory.
* ex: python saveDashboards.py **folder_path** For example:
```bash
* Use **saveDatasources.py** to save each datasources to each file under specific folder. $ ./backup_grafana.sh
* ex: python saveDatasources.py **folder_path** $ tree _OUTPUT_
_OUTPUT_/
* Use **saveFolders.py** to save each folders to each file under specific folder. └── 2019-05-13T08:48:03.tar.gz
* ex: python saveFolders.py **folder_path** ```
* Use `restore_grafana.sh` with a path to a previous backup to restore everything. **NOTE** this *may* result in data loss, by overwriting data on the server.
* Use **createDashboard.py** to read the dashboard file and create or update them on Grafana.
* ex: python createDashboard.py **file_path** ## Notes
* Please have a look at the two scripts in the root directory if you need to customize something.
* Use **createDatasource.py** to read the datasource file and create or update them on Grafana.
* ex: python createDatasource.py **file_path**
* Use **createFolder.py** to read the folder file and create or update them on Grafana.
* ex: python createFolder.py **file_path**
* Use **backup_grafana.sh** to backup all your dashboards, datasources and folders to **/tmp** folder.
* It will create
* three files:
1. **/tmp/dashboards.tar.gz**
2. **/tmp/datasources.tar.gz**
3. **/tmp/folders.tar.gz**
* three folders contain dashboard files, datasource files and folders file:
1. **/tmp/dashboards/$current_time**
2. **/tmp/datasources/$current_time**
3. **/tmp/folders/$current_time**
* e.g.:**sh backup_grafana.sh**
* result:
* **/tmp/dashboads.tar.gz**
* **/tmp/datasourcess.tar.gz**
* **/tmp/folders.tar.gz**
------
* **/tmp/dashboards/2016-10-10_12:00:00**
* **/tmp/datasources/2016-10-10_12:00:00**
* **/tmp/folders/2016-10-10_12:00:00**
#!/bin/bash #!/bin/bash
set -e trap 'echo -ne "\n:::\n:::\tCaught signal, exiting at line $LINENO, while running :${BASH_COMMAND}:\n:::\n"; exit' SIGINT SIGQUIT
current_path=`pwd` current_path=$(pwd)
current_time=`date +"%Y-%m-%d-%H%M%S"` backup_dir="_OUTPUT_"
backup_dir=/tmp/grafana_backup_$current_time timestamp=$(date +"%Y-%m-%dT%H:%M:%S")
backup_dir_compressed=$backup_dir.tar.gz
echo $current_time
dashboard_backup_path="$backup_dir/dashboards" [ -d "${backup_dir}" ] || mkdir -p "${backup_dir}"
datasource_backup_path="$backup_dir/datasources"
folders_backup_path="$backup_dir/folders"
if [ ! -d "$dashboard_backup_path" ]; then for i in dashboards datasources folders
mkdir -p "$dashboard_backup_path" do
fi F="${backup_dir}/${i}/${timestamp}"
[ -d "${F}" ] || mkdir -p "${F}"
python "${current_path}/src/save_${i}.py" "${F}"
done
if [ ! -d "$datasource_backup_path" ]; then tar -czvf "${backup_dir}/${timestamp}.tar.gz" ${backup_dir}/{dashboards,datasources,folders}/${timestamp}
mkdir -p "$datasource_backup_path" rm -rf ${backup_dir}/{dashboards,datasources,folders}/${timestamp}
fi
if [ ! -d "$folders_backup_path" ]; then
mkdir -p "$folders_backup_path"
fi
python "${current_path}/saveDashboards.py" $dashboard_backup_path || exit 0
python "${current_path}/saveDatasources.py" $datasource_backup_path || exit 0
python "${current_path}/saveFolders.py" $folders_backup_path || exit 0
tar -zcvf $backup_dir_compressed -C $backup_dir .
rm -rf $backup_dir
echo "create backup $backup_dir_compressed"
#!/bin/bash
current_path=`pwd`
dashboard_folder="$1"
if [ $# -ne 1 ]; then
echo "Please input the backup folder path. e.g. /tmp/dashboards/2018-01-01"
exit 1
fi
find "$dashboard_folder" -mindepth 1 -name "*.dashboard" | while read f ; do
echo "$f"
python $current_path/createDashboard.py "$f"
done
#!/bin/bash
current_path=`pwd`
datasource_folder="$1"
if [ $# -ne 1 ]; then
echo "Please input the backup folder path. e.g. /tmp/datasources/2018-01-01"
exit 1
fi
find "$datasource_folder" -mindepth 1 -name "*.datasource" | while read f ; do
echo "$f"
python $current_path/createDatasource.py "$f"
done
#!/bin/bash
current_path=`pwd`
folders_path="$1"
if [ $# -ne 1 ]; then
echo "Please input the backup folder path. e.g. /tmp/folders/2018-01-01"
exit 1
fi
find "$folders_path" -mindepth 1 -name "*.folder" | while read f ; do
echo "$f"
python $current_path/createFolder.py "$f"
done
#!/bin/bash -e #!/bin/bash
trap 'echo -ne "\n:::\n:::\tCaught signal, exiting at line $LINENO, while running :${BASH_COMMAND}:\n:::\n"; exit' SIGINT SIGQUIT
current_path=`pwd` current_path=$(pwd)
dashboard_folder="$1" archive_file="$1"
if [ $# -ne 1 ]; then if [ ! -f ${archive_file} ]; then
echo "Please input the backup archive path. e.g. /tmp/grafana_backup_2019-01-01-114306.tar.gz" echo -e "Usage:"
exit 1 echo -e "\t$0 <archive_file>"
echo -e " e.g. $0 '_OUTPUT_/2019-05-13T11:04:33.tar.gz'"
exit 1
fi fi
tmp_dir="/tmp/restore_grafana.$$"
tmp_dir="/tmp/grafana_restore" mkdir -p "$tmp_dir"
mkdir "$tmp_dir" tar -xzf ${archive_file} -C $tmp_dir
tar -xzf $1 -C $tmp_dir
for j in folder datasource dashboard
if [ -d $tmp_dir/datasources ] do
then find ${tmp_dir} -type f -name "*.${j}" | while read f
echo "restore datasources" do
bash $current_path/restore_datasources.sh $tmp_dir/datasources python "${current_path}/src/create_${j}.py" "${f}"
fi done
done
if [ -d $tmp_dir/folders ]
then
echo "restore folders"
bash $current_path/restore_folders.sh $tmp_dir/folders
fi
if [ -d $tmp_dir/dashboards ]
then
echo "restore dashboards"
bash $current_path/restore_dashboards.sh $tmp_dir/dashboards
fi
rm -rf $tmp_dir rm -rf $tmp_dir
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment