backup script for automation
parent
ea6011818d
commit
b9e698d314
|
@ -0,0 +1,87 @@
|
|||
#!/bin/bash
|
||||
|
||||
NAMESPACE="erpnext"
|
||||
LOCAL_BACKUP_DIR="$HOME/erpnext_backup"
|
||||
DATE=$(date +"%Y%m%d%H%M%S")
|
||||
SITE_NAME="192.168.49.2"
|
||||
|
||||
mkdir -p "$LOCAL_BACKUP_DIR"
|
||||
|
||||
log() {
|
||||
echo "[INFO] $1"
|
||||
}
|
||||
|
||||
debug() {
|
||||
echo "[DEBUG] $1"
|
||||
}
|
||||
|
||||
error() {
|
||||
echo "[ERROR] $1"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Database Backup
|
||||
log "Taking database backup..."
|
||||
MARIADB_POD="erpnext-mariadb-0"
|
||||
|
||||
if ! kubectl get pod "$MARIADB_POD" -n "$NAMESPACE" >/dev/null 2>&1; then
|
||||
error "MariaDB pod $MARIADB_POD not found in namespace $NAMESPACE"
|
||||
fi
|
||||
|
||||
SECRET_NAME="erpnext-mariadb"
|
||||
MYSQL_ROOT_PASSWORD=$(kubectl get secret "$SECRET_NAME" -n "$NAMESPACE" -o jsonpath="{.data.mariadb-root-password}" | base64 --decode)
|
||||
|
||||
if [[ -z "$MYSQL_ROOT_PASSWORD" ]]; then
|
||||
error "Failed to retrieve MariaDB root password from secret"
|
||||
fi
|
||||
|
||||
log "Executing database backup..."
|
||||
kubectl exec -n "$NAMESPACE" "$MARIADB_POD" -- bash -c "mysqldump -uroot -p'$MYSQL_ROOT_PASSWORD' --all-databases > /tmp/db_backup.sql"
|
||||
kubectl cp "$NAMESPACE/$MARIADB_POD:/tmp/db_backup.sql" "$LOCAL_BACKUP_DIR/db_backup_$DATE.sql"
|
||||
kubectl exec -n "$NAMESPACE" "$MARIADB_POD" -- rm -f /tmp/db_backup.sql
|
||||
log "Database backup completed."
|
||||
|
||||
# Site Files Backup
|
||||
log "Taking site files backup..."
|
||||
SITE_POD="erpnext-nginx-85f6dd98b7-p6vf2"
|
||||
|
||||
if ! kubectl get pod "$SITE_POD" -n "$NAMESPACE" >/dev/null 2>&1; then
|
||||
error "Nginx pod $SITE_POD not found in namespace $NAMESPACE"
|
||||
fi
|
||||
|
||||
# Backup public files
|
||||
log "Backing up public files..."
|
||||
kubectl exec -n "$NAMESPACE" "$SITE_POD" -- bash -c "cd /home/frappe/frappe-bench/sites/${SITE_NAME}/public && tar -czf /tmp/public-files.tar.gz files"
|
||||
kubectl cp "$NAMESPACE/$SITE_POD:/tmp/public-files.tar.gz" "$LOCAL_BACKUP_DIR/public-files_$DATE.tar.gz"
|
||||
kubectl exec -n "$NAMESPACE" "$SITE_POD" -- rm -f /tmp/public-files.tar.gz
|
||||
|
||||
# Backup private files
|
||||
log "Backing up private files..."
|
||||
kubectl exec -n "$NAMESPACE" "$SITE_POD" -- bash -c "cd /home/frappe/frappe-bench/sites/${SITE_NAME}/private && tar -czf /tmp/private-files.tar.gz files"
|
||||
kubectl cp "$NAMESPACE/$SITE_POD:/tmp/private-files.tar.gz" "$LOCAL_BACKUP_DIR/private-files_$DATE.tar.gz"
|
||||
kubectl exec -n "$NAMESPACE" "$SITE_POD" -- rm -f /tmp/private-files.tar.gz
|
||||
|
||||
# Backup site config
|
||||
log "Backing up site configuration..."
|
||||
kubectl exec -n "$NAMESPACE" "$SITE_POD" -- bash -c "cp /home/frappe/frappe-bench/sites/${SITE_NAME}/site_config.json /tmp/site_config_backup.json"
|
||||
kubectl cp "$NAMESPACE/$SITE_POD:/tmp/site_config_backup.json" "$LOCAL_BACKUP_DIR/site_config_$DATE.json"
|
||||
kubectl exec -n "$NAMESPACE" "$SITE_POD" -- rm -f /tmp/site_config_backup.json
|
||||
|
||||
# Backup common site config
|
||||
log "Backing up common site configuration..."
|
||||
kubectl exec -n "$NAMESPACE" "$SITE_POD" -- bash -c "cp /home/frappe/frappe-bench/sites/common_site_config.json /tmp/common_site_config_backup.json"
|
||||
kubectl cp "$NAMESPACE/$SITE_POD:/tmp/common_site_config_backup.json" "$LOCAL_BACKUP_DIR/common_site_config_$DATE.json"
|
||||
kubectl exec -n "$NAMESPACE" "$SITE_POD" -- rm -f /tmp/common_site_config_backup.json
|
||||
|
||||
# Backup entire sites directory
|
||||
log "Backing up complete sites directory..."
|
||||
kubectl exec -n "$NAMESPACE" "$SITE_POD" -- tar -czf /tmp/site-files.tar.gz -C /home/frappe/frappe-bench/sites .
|
||||
kubectl cp "$NAMESPACE/$SITE_POD:/tmp/site-files.tar.gz" "$LOCAL_BACKUP_DIR/site-files_$DATE.tar.gz"
|
||||
kubectl exec -n "$NAMESPACE" "$SITE_POD" -- rm -f /tmp/site-files.tar.gz
|
||||
|
||||
# List existing backups in the pod
|
||||
log "Existing backups in the pod:"
|
||||
kubectl exec -n "$NAMESPACE" "$SITE_POD" -- ls -l "/home/frappe/frappe-bench/sites/${SITE_NAME}/private/backups"
|
||||
|
||||
log "All backups completed successfully. Files are saved in $LOCAL_BACKUP_DIR:"
|
||||
ls -lh "$LOCAL_BACKUP_DIR"
|
Loading…
Reference in New Issue