Skip to content

Commit

Permalink
Backup/Restore SQL Server using Kopia Artifacts (#1188)
Browse files Browse the repository at this point in the history
* Added mssql blueprint using kopiaSnapshot as output

* updating kando command with kopia snapshot

* fixing kopia path while restoring

* tar/untar using kando streaming

* review fixes
  • Loading branch information
chaitanya-baraskar authored Jan 14, 2022
1 parent 13e0bbe commit eb2799d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/stable/mssql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ kind: Deployment
metadata:
name: mssql-deployment
namespace: sqlserver
labels:
app: mssql
spec:
replicas: 1
selector:
Expand Down
88 changes: 88 additions & 0 deletions examples/stable/mssql/blueprint-v2/mssql-blueprint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
apiVersion: cr.kanister.io/v1alpha1
kind: Blueprint
metadata:
name: mssql-blueprint
actions:
backup:
outputArtifacts:
mssqlCloudDump:

This comment has been minimized.

Copy link
@pavannd1

pavannd1 Jan 14, 2022

Contributor

This should work with file stores as well which means this is not necessarily a cloud dump.
Would make it something like snapshotInfo

kopiaSnapshot: "{{ .Phases.dumpToObjectStore.Output.kopiaOutput }}"
phases:
- func: KubeTask
name: dumpToObjectStore

This comment has been minimized.

Copy link
@pavannd1

pavannd1 Jan 14, 2022

Contributor

Similar to the above comment, this works with file stores so something like dumpToStore makes more sense

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

This comment has been minimized.

Copy link
@pavannd1

pavannd1 Jan 14, 2022

Contributor

There is an option - with compression that can be used with sqlcmd backup. Have you explored that? Are there any special settings required for that?

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

This comment has been minimized.

Copy link
@pavannd1

pavannd1 Jan 14, 2022

Contributor

This is kinda dangerous. We need to find alternatives.

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}"

0 comments on commit eb2799d

Please sign in to comment.