Backup your configuration to Dropbox - martikainen87/Home-Automation GitHub Wiki
Change user to 'homeassistant' and download the dropbox uploader package
sudo su -s /bin/bash homeassistant
cd ~
git clone https://github.com/andreafabrizi/Dropbox-Uploader
Go to dropbox developers to create a new app for the uploader Dropbox developers
Do the following
- Click on "My Apps"
- Click "Create App"
- Choose "Dropbox API"
- Choose "App folder"
- Select a name, for example "HomeAssistant-Backup"
- Click the "Agree to the agreements" thingy..
- Click on "Generated access token - Generate"
- Save the string
Go back to the ssh console
cd Dropbox-Uploader
./dropbox_uploader.sh
Enter the access token you just generated and click enter + y enter
nano dropbox_sync.py
Copy the following script
import os
import subprocess
from subprocess import Popen, PIPE
#The directory to sync
syncdir="/home/homeassistant/.homeassistant"
#Path to the Dropbox-uploaded shell script
uploader = "/home/homeassistant/Dropbox-Uploader/dropbox_uploader.sh"
#If 1 then files will be uploaded. Set to 0 for testing
upload = 1
#If 1 then don't check to see if the file already exists just upload it, if 0 don't upload if already exists
overwrite = 1
#If 1 then crawl sub directories for files to upload
recursive = 1
#Delete local file on successfull upload
deleteLocal = 0
#Prints indented output
def print_output(msg, level):
print((" " * level * 2) + msg)
#Gets a list of files in a dropbox directory
def list_files(path):
p = Popen([uploader, "list", path], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output = p.communicate()[0].decode("utf-8")
fileList = list()
lines = output.splitlines()
for line in lines:
if line.startswith(" [F]"):
line = line[5:]
line = line[line.index(' ')+1:]
fileList.append(line)
return fileList
#Uploads a single file
def upload_file(localPath, remotePath):
p = Popen([uploader, "upload", localPath, remotePath], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output = p.communicate()[0].decode("utf-8").strip()
if output.startswith("> Uploading") and output.endswith("DONE"):
return 1
else:
return 0
#Uploads files in a directory
def upload_files(path, level):
fullpath = os.path.join(syncdir,path)
print_output("Syncing " + fullpath,level)
if not os.path.exists(fullpath):
print_output("Path not found: " + path, level)
else:
#Get a list of file/dir in the path
filesAndDirs = os.listdir(fullpath)
#Group files and directories
files = list()
dirs = list()
for file in filesAndDirs:
filepath = os.path.join(fullpath,file)
if os.path.isfile(filepath):
files.append(file)
if os.path.isdir(filepath):
dirs.append(file)
print_output(str(len(files)) + " Files, " + str(len(dirs)) + " Directories",level)
#If the path contains files and we don't want to override get a list of files in dropbox
if len(files) > 0 and overwrite == 0:
dfiles = list_files(path)
#Loop through the files to check to upload
for f in files:
print_output("Found File: " + f,level)
if upload == 1 and (overwrite == 1 or not f in dfiles):
fullFilePath = os.path.join(fullpath,f)
relativeFilePath = os.path.join(path,f)
print_output("Uploading File: " + f,level+1)
if upload_file(fullFilePath, relativeFilePath) == 1:
print_output("Uploaded File: " + f,level + 1)
if deleteLocal == 1:
print_output("Deleting File: " + f,level + 1)
os.remove(fullFilePath)
else:
print_output("Error Uploading File: " + f,level + 1)
#If recursive loop through the directories
if recursive == 1:
for d in dirs:
print_output("Found Directory: " + d, level)
relativePath = os.path.join(path,d)
upload_files(relativePath, level + 1)
#Start
upload_files("",1)
Test the uploader
python ~/Dropbox-Uploader/dropbox_sync.py
The first upload will take some time since all files in the /homeassistant/.homeassistant folder will be uploaded.
setting up scheduled backup
We're going to setup a crontab job to execure the backupscript once a day.
crontab -e
add this line at the bottom, it will execute the script at 0200 every day
0 2 * * * python ~/Dropbox-Uploader/dropbox_sync.py