Created
April 27, 2019 16:24
-
-
Save fahimalizain/61dc4c2e08808eae21ac29132d505c77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
mysqlPWD=frappe | |
if [ "$#" -ne 3 ]; then | |
echo "Please pass the v10 URL, sitename and the backup timestamp" | |
exit | |
fi | |
if [ ! -d "./v10-data" ]; then | |
mkdir v10-data | |
fi | |
host=$1 | |
sitename=$2 | |
backupNum=$3 | |
if [ ! -d "./v10-data/$sitename" ]; then | |
echo "Creating directory v10-data/$sitename" | |
mkdir "./v10-data/$sitename" | |
fi | |
siteDataPath="$(pwd)/v10-data/$sitename" | |
echo "Site Data Path: $siteDataPath" | |
echo "Deleting everything in v10-data/$sitename" | |
rm -rf $siteDataPath/* | |
echo "Downloading Backup Data" | |
dbFile=$backupNum-$sitename-database.sql.gz | |
filesTar=$backupNum-$sitename-files.tar | |
privateFilesTar=$backupNum-$sitename-private-files.tar | |
wget $host/protected/private/backups/$dbFile -P $siteDataPath | |
wget $host/protected/private/backups/$filesTar -P $siteDataPath | |
wget $host/protected/private/backups/$privateFilesTar -P $siteDataPath | |
echo "Checking for existing website with the same name" | |
if [ -d "./sites/$sitename" ]; then | |
echo "Dropping site" | |
bench drop-site $sitename --root-password $mysqlPWD --force | |
rm -rf archived_sites/$sitename | |
fi | |
echo "Creating a new bench site" | |
bench new-site $sitename --mariadb-root-password $mysqlPWD --admin-password leamadmin | |
echo "Restoring Site data" | |
bench --site $sitename --force restore $siteDataPath/$dbFile --with-public-files $siteDataPath/$filesTar --with-private-files $siteDataPath/$filesTar --mariadb-root-password $mysqlPWD | |
# Update installed_apps | |
# Update Item duplicate item_codes | |
./env/bin/python v10-patch.py $sitename | |
bench --site $sitename migrate | |
bench --site all clear-cache | |
bench restart | |
rm -rf $siteDataPath/* | |
# The following is the .py part | |
import frappe, sys, json | |
def patch_installed_apps(): | |
print("Patching installed_apps to include renovation_core") | |
installed = json.loads(frappe.db.get_global("installed_apps") or "[]") | |
if "renovation_core" in installed: | |
print("Already patched") | |
return | |
installed.insert(1, "renovation_core") | |
frappe.db.sql("UPDATE `tabDefaultValue` SET defValue=%s WHERE defKey='installed_apps'", (json.dumps(installed))) | |
frappe.db.commit() | |
print("Done") | |
def patch_item_codes(): | |
if not frappe.db.get_value("DocType", {"name": "Item"}, "name"): | |
print("Item doctype doesnt exist") | |
print("Patching item_codes") | |
ic = {} | |
for i in frappe.get_list("Item", ["name", "item_code"]): | |
if i.item_code in ic: | |
ic[i.item_code] = ic[i.item_code] + 1 | |
frappe.db.set_value("Item", i.name, "item_code", "{}-{}".format(i.item_code, ic[i.item_code])) | |
print("Duplcate Item Code {}".format(i.item_code)) | |
else: | |
ic[i.item_code] = 0 | |
frappe.db.commit() | |
print("Done") | |
if __name__ == "__main__": | |
siteName = sys.argv[1] | |
if not siteName: | |
raise Exception('Please provide the sitename') | |
print("Using sitename: {}".format(siteName)) | |
frappe.init(site=siteName, sites_path="./sites") | |
print("Frappe Init") | |
frappe.connect() | |
print("Frappe Connected") | |
frappe.local.lang = frappe.db.get_default("lang") | |
patch_installed_apps() | |
patch_item_codes() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment