Skip to content

Instantly share code, notes, and snippets.

@barezina barezina/backup.py
Last active Feb 13, 2020

Embed
What would you like to do?
ios backup extractor - restores an iTunes backup to it's original files
import sqlite3
import os
import uuid
import shutil
print('ios backup python script for linux (ubuntu)')
# this function helps traverse the returned results from the db
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
# set this to the path of your itunes backup
backuppath = '/home/bob/Desktop/feb_2020/'
# read the manifest file to figure out what to map the files/names to
conn = sqlite3.connect(backuppath + 'Manifest.db')
conn.row_factory = dict_factory
c = conn.cursor()
c.execute("""
select substr(fileId, 0, 3) as folder, fileID, relativePath
from Files
""")
results = c.fetchall()
# make folders to categorise the files
if not os.path.isdir(backuppath + '000_extracted_files/'):
os.makedirs(backuppath + '000_extracted_files/')
if not os.path.isdir(backuppath + '000_extracted_files/photos/'):
os.makedirs(backuppath + '000_extracted_files/photos')
if not os.path.isdir(backuppath + '000_extracted_files/videos/'):
os.makedirs(backuppath + '000_extracted_files/videos')
if not os.path.isdir(backuppath + '000_extracted_files/png/'):
os.makedirs(backuppath + '000_extracted_files/png')
if not os.path.isdir(backuppath + '000_extracted_files/databases/'):
os.makedirs(backuppath + '000_extracted_files/databases')
if not os.path.isdir(backuppath + '000_extracted_files/other/'):
os.makedirs(backuppath + '000_extracted_files/other')
# for every file in the database, map it's hashed name to its real name and
# copy to a folder that makes sense
for row in results:
# Get the information for this file out of the database row
folder = row['folder']
fileID = row['fileID']
relativePath = row['relativePath']
# make the whole path lowercase (and shorter, and replace out spaces and problem characters)
relativePath = relativePath.lower()
relativePath = relativePath.replace(' ', '_')
relativePath = relativePath.replace(':', '-')
relativePath = relativePath.replace('#', '-')
# split the path into segments (folder, filename, extension)
fileName = relativePath.split('/').pop()
prefix = str(uuid.uuid4())[:6]
pathElements = relativePath.split('/')
pathElements.pop()
pathOnly = '/'.join(pathElements)
fileNameElements = fileName.split('.')
fileNameOnly = fileNameElements.pop(0)
fileNameOnly = fileNameOnly[:26]
extension = '.'.join(fileNameElements)
if len(extension) < 1:
# if the file doesnt have an extension, just skip it
continue
newFolder = ''
# figure out which folder this file belongs in
if extension == 'jpg' or extension == 'jpeg' or extension == 'elif' or extension == 'cr2':
newFolder = 'photos/'
elif extension == 'png':
newFolder = 'png/'
elif extension == 'mp4' or extension == 'mov' or extension == 'avi':
newFolder = 'videos/'
elif extension == 'db' or extension == 'sqlite':
newFolder = 'databases/'
else:
newFolder = 'other/'
extension = '.' + extension
oldApplePath = backuppath + folder + '/' + fileID
newBackupPath = backuppath + '000_extracted_files/' + newFolder + fileNameOnly + '_' + prefix + extension
if os.path.exists(oldApplePath):
# If the old file exists, copy it to the new destination with the proper name
print(oldApplePath + ' -> ' + newBackupPath)
shutil.copyfile(oldApplePath, newBackupPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.