Skip to content

Instantly share code, notes, and snippets.

@erickdsama
Last active April 5, 2024 20:11
Show Gist options
  • Save erickdsama/6eed3207296198751e4abbcc68f3b798 to your computer and use it in GitHub Desktop.
Save erickdsama/6eed3207296198751e4abbcc68f3b798 to your computer and use it in GitHub Desktop.
Restore database with the last backup in develop
import getpass
import gzip
import math
import os
import re
import stat
import subprocess
import sys
import time
import requests
session = requests.Session()
URL_HOST = "https://erp-development.zebrands.mx"
URL_COMPRESS = "https://github.com/erickdsama/Frappe-Snippets/releases/download/v0.0.1/compressor"
def download_compress_file():
print("Downloading Compress File")
response = session.get(URL_COMPRESS, stream=True)
with open("compressor", 'wb') as f:
for data in progressbar_file_download(response):
f.write(data)
st = os.stat('compressor')
os.chmod('compressor', st.st_mode | stat.S_IEXEC)
def convert_size(size_bytes):
if size_bytes == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return "%s %s" % (s, size_name[i])
def progressbar_file_download(response, prefix="", size=60, file=sys.stdout):
count = int(response.headers.get('content-length'))
downloaded = 0
def show(j):
x = int(size * j / count)
file.write("%s[%s%s] %s/%s\r" % (prefix, "#" * x, "." * (size - x), convert_size(j), convert_size(count)))
file.flush()
show(downloaded)
for data in response.iter_content(chunk_size=4096):
yield data
downloaded += len(data)
show(downloaded)
file.write("\n")
file.flush()
def login_session():
print('Please enter your Zecore credentials')
username = input("username: ")
password = getpass.getpass()
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
payload = f'usr={username}&pwd={password}'
response = session.post(f"{URL_HOST}/api/method/login", data=payload, headers=headers)
if response.status_code == 200:
print("Login Success...")
elif response.status_code == 503:
raise Exception("The server is updating")
else:
raise Exception("Password or user incorrect")
def get_backups():
payload = 'name=backups'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
print("Getting Backups")
response = session.post(f"{URL_HOST}/api/method/frappe.desk.desk_page.getpage", data=payload, headers=headers)
response_json = response.json()
docs = response_json.get("docs", [])
data_string = docs[0].get("script", "")
regex = "[0-9]{8}_[0-9]{6}-.*?sql.gz"
founds = re.findall(regex, data_string)
print(f"Found {founds} backups")
founds.sort()
last_file = founds[-1]
print(f"Backup {last_file} found it...")
return last_file
def download_backup(backup_name: str):
print("Trying to download backup")
local_backup = f'./{backup_name}'
if not os.path.exists(local_backup):
print("Downloading backup")
response = session.get(f"{URL_HOST}/backups/{backup_name}", stream=True)
with open(local_backup, 'wb') as f:
for data in progressbar_file_download(response):
f.write(data)
else:
print("Using Local File Backup")
return backup_name
def verify_pv_installed():
try:
subprocess.run(["pv", "--version"])
except FileNotFoundError:
subprocess.run(["sudo", "apt-get", "install", "pv", "-y", "-qq"])
def verify_compressor_exist():
if os.path.isfile('compressor'):
print("Compressor binary found")
else:
print("Compressor binary not found")
download_compress_file()
if __name__ == "__main__":
start_time = time.time()
login_session()
last_backup = get_backups()
local_file = download_backup(backup_name=last_backup)
verify_compressor_exist()
verify_pv_installed()
compress_min_file = "min_" + local_file
if os.path.exists(compress_min_file):
print("Removing old min file")
os.remove(compress_min_file)
subprocess.run(["./compressor", f"-input={local_file}"])
print(f"Restoring with bench restore using {compress_min_file}")
subprocess.run(f"bench restore {compress_min_file} --mariadb-root-username root --mariadb-root-password 123 --admin-password 123", shell=True)
print("--- %s seconds ---" % (time.time() - start_time))
@erickdsama
Copy link
Author

solo tienes que colocarlo en -> frappe-bench y seguir las instrucciones

@JulioJair
Copy link

🚀

@waflecl
Copy link

waflecl commented May 10, 2022

Y esta maravilla? Denle un metro cuadrado de cervezas a @erickdsama 🍻

@erickdsama
Copy link
Author

@waflecl :3 🍻

@erickdsama
Copy link
Author

package main

import (
	"bufio"
	"compress/gzip"
	"flag"
	"log"
	"os"
	"strings"
)

func main() {
	// Parse command-line arguments
	inputFileName := flag.String("input", "", "Input gzip-compressed SQL file name")
	flag.Parse()

	if *inputFileName == "" {
		log.Fatal("Input file name is required")
	}
	// List of strings to remove
	removedTabs := []string{
		"INSERT INTO `tabVersion",
		"INSERT INTO `tabInteraction",
		"INSERT INTO `tabPaymentMachine Log",
		"INSERT INTO `tabPayment Schedule",
		"INSERT INTO `tabRaw Event",
		"INSERT INTO `tabActivity Log",
		"INSERT INTO `tabStaffing Plan",
		"INSERT INTO `tabRaw Segment Event",
		"INSERT INTO `tabEmployee Onboarding",
		"INSERT INTO `tabRaw Event",
		"INSERT INTO `tabReschedule Log",
		"INSERT INTO `tabEvent Ack",
		"INSERT INTO `tabSubscriber Event Log",
		"INSERT INTO `tabQuotation Status Tracking",
		"INSERT INTO `tabCase Status Tracking",
		"INSERT INTO `tabOpportunity Status Tracking",
		"INSERT INTO `tabCommunication",
		"INSERT INTO `tabZeus App Notifications",
		"INSERT INTO `tabRaw Sendgrid Event",
		"INSERT INTO `tabAccess Log",
		"INSERT INTO `tabReturn Machine Log",
		"INSERT INTO `tabError Log",
		"INSERT INTO `tabView Log",
		"INSERT INTO `tabZeCore Shipping Communication Log",
		"INSERT INTO `tabActivity Log",
		"INSERT INTO `tabRoute History",
		"INSERT INTO `gl_map",
		"INSERT INTO `tabScheduled Job Log",
		"INSERT INTO `tabRaw Twilio WhatsApp Event",
		"INSERT INTO `tabAnnual Development Memorandum",
		"INSERT INTO `tabStores Sellout",
		"INSERT INTO `tabScheduled Job",
		"INSERT INTO `tabEmail Queue",
		"INSERT INTO `tabComment",
		"INSERT INTO `tabCommunication",
		"INSERT INTO `tabStaffing Plan Detail",
		"INSERT INTO `__global_search",
		"INSERT INTO `tabData Import",
		"INSERT INTO `tabMarketplace Error Log",
		"INSERT INTO `tabKey Event Log",
		"INSERT INTO `tabLog Tpl to Wms",
		"INSERT INTO `tabJob Offer",
		"INSERT INTO `tabRaw Yotpo Event",
		"INSERT INTO `tabShipping Machine Log",
		"INSERT INTO `tabRefund Machine Log",
		"INSERT INTO `tabShipping Core Item Change History",
		"INSERT INTO `tabClub Premier Dollar",
		"INSERT INTO `tabInterview Feedback Form",
		"INSERT INTO `tabRaw Scheduled Event",
		"INSERT INTO `tabJob Applicant",
		"INSERT INTO `tabError Snapshot",
		"INSERT INTO `tabAccess Log",
		"INSERT INTO `tabData Import",
		"INSERT INTO `tabEmployee Onboarding Template",
		"INSERT INTO `tabPaymentMachine Log",
		"INSERT INTO `tabState Transition Notification",
		"INSERT INTO `tabPayment Machine Log",
		"INSERT INTO `tabClub Premier Redeem",
		"INSERT INTO `tabStores Sellout",
		"INSERT INTO `tabZeus App Notifications",
		"INSERT INTO `tabRoute History",
		"INSERT INTO `tabAppraisal",
		"INSERT INTO `tabAnnual Development Memorandum",
		"INSERT INTO `tabTL Evaluation",
		"INSERT INTO `tabDeleted Document",
		"INSERT INTO `tabClub Premier Dollar",
		"INSERT INTO `tabLog Setting User",
		"INSERT INTO `tabShipping Core Item Change History",
		"INSERT INTO `tabLog TPL Middleware To WMS",
		"INSERT INTO `tabRefund Machine Log",
		"INSERT INTO `tabComment",
		"INSERT INTO `tabError Log",
		"INSERT INTO `tabZeCoreShippingCommunicationLogOLD",
		"INSERT INTO `tabInteraction",
		"INSERT INTO `tabLog Wms to Shipping",
		"INSERT INTO `tabVersion",
		"INSERT INTO `tabCase Interaction",
		"INSERT INTO `tabPayment Schedule",
		"INSERT INTO `tabView Log",
		"INSERT INTO `tabShipping Machine Log",
		"INSERT INTO `tabJob Opening",
		"INSERT INTO `tabJob Applicant",
		"INSERT INTO `tabEmail Queue",
		"INSERT INTO `tabRaw Firebase Event",
		"INSERT INTO `tabJob Aplicant Interview",
		"INSERT INTO `tabJob Offer",
		"INSERT INTO `tabStaffing Plan",
		"INSERT INTO `tabStaffing Plan Detail",
		"INSERT INTO `tabInterview Feedback Form",
		"INSERT INTO `tabEmployee Onboarding",
		"INSERT INTO `tabEmployee Onboarding Template",
		"INSERT INTO `tabReschedule Log",
		"INSERT INTO `tabRaw Scheduled Event",
		"INSERT INTO `tabState Transition",
		"INSERT INTO `tabMarketplace Error Log",
		"INSERT INTO `tabTL Evaluation",
		"INSERT INTO `tabLog Wms to Return",
		"INSERT INTO `tabZeCore Shipping Communication Log",
		"INSERT INTO `tabLog Inhouse to Wms",
		"INSERT INTO `tabReturn Machine Log",
		// Add more strings here as needed
	}

	// Open the input gzip-compressed SQL file
	inputFile, err := os.Open(*inputFileName)
	if err != nil {
		log.Fatalf("Error opening input file: %v", err)
	}
	defer inputFile.Close()

	// Create a gzip reader to read the input file
	log.Println("Create a gzip reader to read the input file")
	gzipReader, err := gzip.NewReader(inputFile)
	if err != nil {
		log.Fatalf("Error creating gzip reader: %v", err)
	}
	defer gzipReader.Close()

	// Create the output gzip-compressed file
	outputFile, err := os.Create("min_" + *inputFileName)
	if err != nil {
		log.Fatalf("Error creating output file: %v", err)
	}
	defer outputFile.Close()

	// Create a gzip writer to write to the output file
	gzipWriter := gzip.NewWriter(outputFile)
	defer gzipWriter.Close()

	// Create a scanner to read the input gzip reader line by line
	scanner := bufio.NewScanner(gzipReader)

	// Set the maximum token size to a large value
	const maxTokenSize = 1024 * 1024 * 100 // 64 KB
	buf := make([]byte, maxTokenSize)
	scanner.Buffer(buf, maxTokenSize)
	log.Println("Init scanner...")
	for scanner.Scan() {
		line := scanner.Text()
		shouldRemove := false

		// Check if the line contains any of the strings to remove
		for _, tab := range removedTabs {
			if strings.Contains(line, tab) {
				shouldRemove = true
				break
			}
		}

		// If the line does not contain any of the strings to remove, write it to the output gzip writer
		if !shouldRemove {
			_, err := gzipWriter.Write([]byte(line + "\n"))
			if err != nil {
				log.Fatalf("Error writing to output file: %v", err)
			}
		}
	}

	if err := scanner.Err(); err != nil {
		log.Fatalf("Error scanning input file: %v", err)
	}

	log.Println("Backup file with removed lines created successfully.")
}

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