Skip to content

Instantly share code, notes, and snippets.

@praul
Last active April 21, 2021 13:07
Show Gist options
  • Save praul/cbec53763cd5226be64d8e8fde2763b6 to your computer and use it in GitHub Desktop.
Save praul/cbec53763cd5226be64d8e8fde2763b6 to your computer and use it in GitHub Desktop.
Borg Backup from stash scenes and galleries with rating and tag
#!/usr/env/python3
configpath = '/path/to/stash/config' #Path to your stash config files. No trailing slash
dockerpath = '/data/' #The path for media Stash uses internally. In docker containers this is often /data/. With trailing slash
realpath = '/real/path/to/your/media/' #The Path where your media files are at. Corresponds to dockerpath. With trailing slash
repo = '/path/to/your/borg/repo' #Your Borg Repo Path. No trailing slash
scriptpath = '/tmp/' #Temp Path - Bash script will be created here. With trailing slash
tagid = 197 #If you want to create a tag to flag files that should be backupped, enter the Id here. Else enter 0
minrating = 3 #Backup scenes and galleries from this rating up. Else enter 6
import sqlite3
import os
from datetime import datetime
def escape(st):
st = st.replace("$","\$").replace("`", "\`")
st = st.replace("\\\\","\\")
st = st.replace(dockerpath, realpath)
return st
def write(x):
#print (x)
datei = escape(x)
f.write('"' + str(datei) + '" ' + " ")
def files_rating(tablename):
if minrating < 6:
con = sqlite3.connect(db)
cur = con.cursor()
count = 0
for row in cur.execute('SELECT * FROM ' + tablename + ' WHERE rating >= ' + str(minrating) ):
x = row[1]
write(x)
count+= 1
con.close()
return (tablename + ' with rating >=' + str(minrating) + ': ' + str(count))
def files_tag(tablename1, tablename2, tag):
if tag > 0:
con = sqlite3.connect(db)
cur = con.cursor()
taglist = []
count = 0
for row in cur.execute('SELECT * FROM ' + tablename2 + ' WHERE tag_id = '+ str(tag) ):
taglist.append(row[0])
con.close()
con = sqlite3.connect(db)
cur = con.cursor()
for sid in taglist:
for row in cur.execute('SELECT * FROM ' + tablename1 + ' WHERE id = '+ str(sid)):
x = row[1]
write(x)
count+= 1
con.close()
return (tablename1 + 'with tag ' + str(tag) + ': ' + str(count))
#Get Date String
now = datetime.now()
d1 = str(now.strftime("%Y-%m-%d-%H-%M-%S"))
#DB Path
db = configpath + '/stash-go.sqlite'
#Prepare Bash File
f = open(scriptpath + "stashbackup.sh", "w", encoding="utf-8")
f.write ("#!/bin/bash \n")
f.write ('borg create --stats --list '+ repo +'::bestscenes-' + d1 + " ")
f.write('"' + str(configpath) + '" ' + " ") #add config folder to backup
#Write Filenames from DB to Bash Script
messages = []
messages.append( files_rating('scenes') )
messages.append( files_rating('galleries') )
messages.append( files_tag('scenes','scenes_tags', tagid) )
messages.append( files_tag('galleries','galleries_tags', tagid) )
#Close Bash File
f.close()
#Execute Bash File
print ('--------------------------------------------------------------------------------------')
print ('Running Borg')
os.system('bash ' + scriptpath + 'stashbackup.sh')
print (messages)
@praul
Copy link
Author

praul commented Apr 20, 2021

It's ugly code, but it get's the job done :)
os: linux
requirements: bash, borgbackup, python3

there is no duplicate detection, borg handles this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment