diff --git a/examples/stable/mssql/README.md b/examples/stable/mssql/README.md index 4599adee13..d09e8650b1 100644 --- a/examples/stable/mssql/README.md +++ b/examples/stable/mssql/README.md @@ -60,6 +60,8 @@ kind: Deployment metadata: name: mssql-deployment namespace: sqlserver + labels: + app: mssql spec: replicas: 1 selector: diff --git a/examples/stable/mssql/blueprint-v2/mssql-blueprint.yaml b/examples/stable/mssql/blueprint-v2/mssql-blueprint.yaml new file mode 100644 index 0000000000..2c8af7bd21 --- /dev/null +++ b/examples/stable/mssql/blueprint-v2/mssql-blueprint.yaml @@ -0,0 +1,88 @@ +apiVersion: cr.kanister.io/v1alpha1 +kind: Blueprint +metadata: + name: mssql-blueprint +actions: + backup: + outputArtifacts: + mssqlCloudDump: + kopiaSnapshot: "{{ .Phases.dumpToObjectStore.Output.kopiaOutput }}" + phases: + - func: KubeTask + name: dumpToObjectStore + objects: + mssql: + kind: Secret + name: '{{ index .Object.metadata.labels "app" }}' + namespace: '{{ .Deployment.Namespace }}' + args: + image: ghcr.io/kanisterio/mssql-tools:0.71.0 + command: + - bash + - -o + - errexit + - -o + - pipefail + - -c + - | + kopiaPath="backup.tar.gz" + root_password="{{ index .Phases.dumpToObjectStore.Secrets.mssql.Data "SA_PASSWORD" | toString }}" + root_uname="sa" + mssql_pod="{{ index .Deployment.Namespace }}/{{ index .Deployment.Pods 0 }}" + server_name="{{ index .Deployment.Name }}.{{index .Deployment.Namespace}}.svc.cluster.local" + databases=$(/opt/mssql-tools/bin/sqlcmd -S ${server_name} -U ${root_uname} -P ${root_password} -Q "SET NOCOUNT ON; SELECT name FROM sys.databases WHERE name NOT IN ('master','model','msdb','tempdb')" -b -s "," -h -1) + for database in $databases; do /opt/mssql-tools/bin/sqlcmd -S ${server_name} -U ${root_uname} -P ${root_password} -Q "backup database $database to disk = '/tmp/backup/$database.bak' with format;"; done + kubectl cp ${mssql_pod}:/tmp/backup /tmp/backup + tar zcvf - -C /tmp/ backup | kando location push --profile '{{ toJson .Profile }}' --path "${kopiaPath}" --output-name "kopiaOutput" - + kubectl exec -it {{ index .Deployment.Pods 0 }} -n {{ index .Deployment.Namespace }} -- rm -r /tmp/backup + restore: + inputArtifactNames: + - mssqlCloudDump + phases: + - func: KubeTask + name: restoreFromObjectStore + objects: + mssql: + kind: Secret + name: '{{ index .Object.metadata.labels "app" }}' + namespace: '{{ .Deployment.Namespace }}' + args: + image: ghcr.io/kanisterio/mssql-tools:0.71.0 + command: + - bash + - -o + - errexit + - -o + - pipefail + - -c + - | + kopiaPath="backup.tar.gz" + kopia_snap='{{ .ArtifactsIn.mssqlCloudDump.KopiaSnapshot }}' + root_password="{{ index .Phases.restoreFromObjectStore.Secrets.mssql.Data "SA_PASSWORD" | toString }}" + root_uname="sa" + mssql_pod="{{ index .Deployment.Namespace }}/{{ index .Deployment.Pods 0 }}" + server_name="{{ index .Deployment.Name }}.{{ index .Deployment.Namespace }}.svc.cluster.local" + kando location pull --profile '{{ toJson .Profile }}' --path "${kopiaPath}" --kopia-snapshot ${kopia_snap} - | tar zxvf - -C /tmp/ + kubectl cp /tmp/backup ${mssql_pod}:/tmp/backup + backup_files=$(ls /tmp/backup) + for script in $backup_files; do database="$(cut -d'.' -f1 <<<"$script")"; /opt/mssql-tools/bin/sqlcmd -S ${server_name} -U ${root_uname} -P ${root_password} -Q "restore database $database from disk = '/tmp/backup/$script' with replace"; done + kubectl exec -it {{ index .Deployment.Pods 0 }} -n {{ index .Deployment.Namespace }} -- rm -r /tmp/backup + delete: + inputArtifactNames: + - mssqlCloudDump + phases: + - func: KubeTask + name: deleteFromBlobStore + args: + image: ghcr.io/kanisterio/mssql-tools:0.71.0 + command: + - bash + - -o + - errexit + - -o + - pipefail + - -c + - | + kopiaPath="backup.tar.gz" + kopia_snap='{{ .ArtifactsIn.mssqlCloudDump.KopiaSnapshot }}' + kando location delete --profile '{{ toJson .Profile }}' --path ${kopiaPath} --kopia-snapshot "${kopia_snap}"