// NewBackupCronJob Returns the CronJob object for the Database Backup
NewBackupCronJob Returns the CronJob object for the Database Backup
// NewBackupCronJob Returns the CronJob object for the Database Backup
func NewBackupCronJob(bkp *v1alpha1.Backup, db *v1alpha1.MariaDB, scheme *runtime.Scheme) *v1beta1.CronJob {
	bkpPVClaimName := GetMariadbBkpVolumeClaimName(bkp)
	dbPort := db.Spec.Port
	hostname := mariadbBkpServiceName(bkp) + "." + bkp.Namespace
	filename := "/var/lib/mysql/backup_`date +%F_%T`.sql"
	backupCommand := "echo 'Starting DB Backup'  &&  " +
		"mysqldump -P " + fmt.Sprint(dbPort) + " -h '" + hostname +
		"' --lock-tables --all-databases > " + filename +
		"&& echo 'Completed DB Backup'"
	cron := &v1beta1.CronJob{
		ObjectMeta: v1.ObjectMeta{
			Name:      bkp.Name,
			Namespace: bkp.Namespace,
			Labels:    utils.Labels(db, "mariadb"),
		},
		Spec: v1beta1.CronJobSpec{
			Schedule: bkp.Spec.Schedule,
			JobTemplate: v1beta1.JobTemplateSpec{
				Spec: batchv1.JobSpec{
					Template: corev1.PodTemplateSpec{
						Spec: corev1.PodSpec{
							ServiceAccountName: "mariadb-operator",
							Volumes: []corev1.Volume{
								{
									Name: pvStorageName,
									VolumeSource: corev1.VolumeSource{
										PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
											ClaimName: bkpPVClaimName,
										},
									},
								},
							},
							Containers: []corev1.Container{
								{
									Name:    bkp.Name,
									Image:   db.Spec.Image,
									Command: []string{"/bin/sh", "-c"},
									Args:    []string{backupCommand},
									VolumeMounts: []corev1.VolumeMount{
										{
											Name:      pvStorageName,
											MountPath: "/var/lib/mysql",
										},
									},
									Env: []corev1.EnvVar{
										{
											Name:  "MYSQL_PWD",
											Value: db.Spec.Rootpwd,
										},
										{
											Name:  "USER",
											Value: "root",
										},
									},
								},
							},
							RestartPolicy: corev1.RestartPolicyOnFailure,
						},
					},
				},
			},
		},
	}
	controllerutil.SetControllerReference(bkp, cron, scheme)
	return cron
}
func NewBackupCronJob ( bkp * v1alpha1 . Backup , db * v1alpha1 . MariaDB , scheme * runtime . Scheme ) * v1beta1 . CronJob { bkpPVClaimName := GetMariadbBkpVolumeClaimName ( bkp ) dbPort := db . Spec . Port hostname := mariadbBkpServiceName ( bkp ) + " " + bkp . Namespace filename := " " backupCommand := " " + " " + fmt . Sprint ( dbPort ) + " " + hostname + " " + filename + " " cron := & v1beta1 . CronJob { ObjectMeta : v1 . ObjectMeta { Name : bkp . Name , Namespace : bkp . Namespace , Labels : utils . Labels ( db , " " ) , } , Spec : v1beta1 . CronJobSpec { Schedule : bkp . Spec . Schedule , JobTemplate : v1beta1 . JobTemplateSpec { Spec : batchv1 . JobSpec { Template : corev1 . PodTemplateSpec { Spec : corev1 . PodSpec { ServiceAccountName : " " , Volumes : [ ] corev1 . Volume { { Name : pvStorageName , VolumeSource : corev1 . VolumeSource { PersistentVolumeClaim : & corev1 . PersistentVolumeClaimVolumeSource { ClaimName : bkpPVClaimName , } , } , } , } , Containers : [ ] corev1 . Container { { Name : bkp . Name , Image : db . Spec . Image , Command : [ ] string { " " , " " } , Args : [ ] string { backupCommand } , VolumeMounts : [ ] corev1 . VolumeMount { { Name : pvStorageName , MountPath : " " , } , } , Env : [ ] corev1 . EnvVar { { Name : " " , Value : db . Spec . Rootpwd , } , { Name : " " , Value : " " , } , } , } , } , RestartPolicy : corev1 . RestartPolicyOnFailure , } , } , } , } , } , } controllerutil . SetControllerReference ( bkp , cron , scheme ) return cron }
NewBackupCronJob Returns the CronJob object for the Database Backup
NewBackupCronJob Returns the CronJob object for the Database Backup
// currentTime := time.Now() //formatedDate := currentTime.Format("2006-01-02_15:04:05") // filename := "/var/lib/mysql/backup/backup_" + formatedDate + ".sql"