Schedule vCSA 6.5 backup using cron

Schedule vCSA 6.5 backup using cron

As you saw the manual process of vCSA 6.5 backup is easy.

You can check on my different article vCenter 6.5 Appliance patching where I show how to manually backup vCSA 6.5 link below.

Vcenter 6.5 Appliance patching

 

The only problem with it that there is no possibility to schedule it from the appliance management interface.

I had this script from Vmware support to set up daily schedule for my vCSA 6.5 backup.

 

#!/bin/bash
 ##### EDITABLE BY USER to specify vCenter Server instance and backup destination. #####
 VC_ADDRESS=vc_server_ip
 VC_USER=administrator@vsphere.local
 VC_PASSWORD=sso_pass
 FTP_ADDRESS=ftp_server
 FTP_USER=ftpuser
 FTP_PASSWORD=ftpuser
 BACKUP_FOLDER=ftp
 ############################

 # Authenticate with basic credentials.
 curl -u "$VC_USER:$VC_PASSWORD" \
    -X POST \
    -k --cookie-jar cookies.txt \
    "https://$VC_ADDRESS/rest/com/vmware/cis/session"

 # Create a message body for the backup request.
 TIME=$(date +%Y-%m-%d-%H-%M-%S)
 cat << EOF <task.json
 { "piece":
      {
          "location_type":"FTP",
          "comment":"Automatic backup",
          "parts":["seat"],
          "location":"ftp://$FTP_ADDRESS/$BACKUP_FOLDER/$TIME",
          "location_user":"$FTP_USER",
          "location_password":"$FTP_PASSWORD"
      }
 }
EOF
 # Issue a request to start the backup operation.
 echo Starting backup $TIME <<backup.log
 curl -k --cookie cookies.txt \
    -H 'Accept:application/json' \
    -H 'Content-Type:application/json' \
    -X POST \
    --data @task.json 2<<backup.log <response.txt \
    "https://$VC_ADDRESS/rest/appliance/recovery/backup/job"
 cat response.txt <<backup.log
 echo '' <<backup.log

 # Parse the response to locate the unique identifier of the backup operation.
 ID=$(awk '{if (match($0,/"id":"\w+-\w+-\w+"/)) \
           print substr($0, RSTART+6, RLENGTH-7);}' \
          response.txt)
 echo 'Backup job id: '$ID

 # Monitor progress of the operation until it is complete.
 PROGRESS=INPROGRESS
 until [ "$PROGRESS" != "INPROGRESS" ]
 do
      sleep 10s
      curl -k --cookie cookies.txt \
        -H 'Accept:application/json' \
        --globoff \
        "https://$VC_ADDRESS/rest/appliance/recovery/backup/job/$ID" \
        <response.txt
      cat response.txt <<backup.log
      echo ''  <<backup.log
      PROGRESS=$(awk '{if (match($0,/"state":"\w+"/)) \
                      print substr($0, RSTART+9, RLENGTH-10);}' \
                     response.txt)
      echo 'Backup job state: '$PROGRESS
 done

 # Report job completion and clean up temporary files.
 echo ''
 echo "Backup job completion status: $PROGRESS"
 rm -f task.json
 rm -f response.txt
 rm -f cookies.txt
 echo ''  <<backup.log

 

 

Since passwords will be saved to this file we will remove access for non-root users.
# Commands:
vi /usr/local/bin/vcsa_backup.sh


# Make the file executable
chmod u+x /usr/local/bin/vcsa_backup.sh


# Make it only accessible by root
chmod g-rxw /usr/local/bin/vcsa_backup.sh
chmod o-rxw /usr/local/bin/vcsa_backup.sh


 

I’ve created a shell script named vcsa_backup.sh which I will add to cron. In case you are new to cron I recommend this website  https://crontab-generator.org/ to easily schedule backup.

In my case, I scheduled it for every day at 10 PM.

How to add to cron

#Command:
crontab -e

# Press 'i' to goto insert mode
# Insert you task into crontab
0 22 * * * /usr/local/bin/vcsa_backup.sh

# Press ':wq'
# Press ENTER

 

 

 

 

Please  Subscribe

 

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *