Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Cette page explique comment ajouter des scripts à un modèle de sauvegarde. Il décrit les différentes phases exécutées par un script lors d'une tâche de sauvegarde.
Vous pouvez écrire vos scripts et les exécuter pour effectuer une action au début d'une tâche de sauvegarde, pendant celle-ci ou après sa fin. Ils sont également appelés "scripts pré- et post-script".
Emplacement des scripts
Un dossier de script est créé automatiquement lors de l'installation de l'agent, là où le système de fichiers ou la base de données à protéger est en cours d'exécution. Pour les exécuter, vous devez placer les scripts dans les dossiers suivants:
Pour l'hôte Windows: \Program Files\Google Cloud Backup and DR\scripts
Pour un hôte Linux: /act/scripts et définissez le script comme exécutable (chmod 755 *.sh)
Phases du script
Les scripts s'exécutent dans les phases suivantes. Si votre script ne s'exécute pas, vérifiez qu'il se trouve dans le dossier scripts.
INIT: cette phase commence lorsque l'appliance de sauvegarde/restauration se connecte à l'agent. Au cours de cette phase, la tâche est initialisée et les identifiants sont validés.
PRE: cette phase commence juste avant l'opération principale de la tâche.
Pour les instantanés, cette phase commence avant le blocage de l'application. Pour les tâches de type "mount", cela se produit après la mise en correspondance des appareils avec l'hôte, mais avant le démarrage des opérations basées sur des agents telles que le réexamen, l'importation et l'installation de systèmes de fichiers.
POST: cette phase commence immédiatement après la fin de l'opération principale de la tâche. Pour les tâches de type sauvegarde, cela se produit après le dégivrage de l'application. Pour les jobs de type "mount", cette phase commence une fois toutes les opérations d'importation, de montage ou de mise en ligne des applications terminées.
FINAL (FINAL) : cette phase correspond à la fin de la tâche. Sauf si le script renvoie un code non nul qui entraîne l'échec de la tâche, la tâche est terminée.
ABORT: cette phase gère les tâches abandonnées en cas d'échec.
Exemple: scripts
L'exemple suivant montre comment exécuter un script côté hôte à différentes phases du processus de tâche de sauvegarde. Il comporte cinq scripts qui affichent la date ou l'heure, le nom de l'application (nom du système de fichiers ou de la base de données), le type de tâche, le nom de la tâche, la valeur de rétention des journaux en jours et un texte indiquant la phase de la tâche de sauvegarde en cours de traitement.
```sh $ cat /act/scripts/init.sh #!/bin/sh if [ "${ACT_JOBTYPE}" = "logbackup" ]; then echo `date` "${ACT_JOBNAME}_${ACT_APPNAME}_ARCHIVELOG_BACKUP JobPhase:INIT" >> /tmp/backup_status.log else echo `date` "${ACT_JOBNAME}_${ACT_APPNAME}_INCREMENTAL_BACKUP JobPhase:INIT" >> /tmp/backup_status.log fi $ cat /act/scripts/pre.sh #!/bin/sh if [ "${ACT_JOBTYPE}" = "logbackup" ]; then echo `date` "${ACT_JOBNAME}_${ACT_APPNAME}_ARCHIVELOG_BACKUP JobPhase:PRE" >> /tmp/backup_status.log else echo `date` "${ACT_JOBNAME}_${ACT_APPNAME}_INCREMENTAL_BACKUP JobPhase:PRE" >> /tmp/backup_status.log fi $ cat /act/scripts/post.sh #!/bin/sh if [ "${ACT_JOBTYPE}" = "logbackup" ]; then echo `date` "${ACT_JOBNAME}_${ACT_APPNAME}_ARCHIVELOG_BACKUP JobPhase:POST" >> /tmp/backup_status.log else echo `date` "${ACT_JOBNAME}_${ACT_APPNAME}_INCREMENTAL_BACKUP JobPhase:POST" >> /tmp/backup_status.log fi $ cat /act/scripts/fini.sh #!/bin/sh if [ "${ACT_JOBTYPE}" = "logbackup" ]; then echo `date` "${ACT_JOBNAME}_${ACT_APPNAME}_ARCHIVELOG_BACKUP JobPhase:FINAL" >> /tmp/backup_status.log else echo `date` "${ACT_JOBNAME}_${ACT_APPNAME}_INCREMENTAL_BACKUP JobPhase:FINAL" >> /tmp/backup_status.log fi $ cat /act/scripts/abort.sh #!/bin/sh if [ "${ACT_JOBTYPE}" = "logbackup" ]; then echo `date` "${ACT_JOBNAME}_${ACT_APPNAME}_ARCHIVELOG_BACKUP JobPhase:ABORT" >> /tmp/backup_status.log else echo `date` "${ACT_JOBNAME}_${ACT_APPNAME}_INCREMENTAL_BACKUP JobPhase:ABORT" >> /tmp/backup_status.log fi```
Dans l'exemple précédent, ACT_JOBTYPE = logbackup indique que la tâche en cours d'exécution est une tâche de sauvegarde de journaux, et non une tâche de sauvegarde complète de la base de données.
Il se peut que vous n'ayez pas besoin d'exécuter les cinq phases pour obtenir la sortie de journal de votre choix.
Vous ne pouvez utiliser que les phases d'initialisation et de finalisation pour indiquer le début et la fin du processus de sauvegarde.
Vous pouvez également personnaliser les scripts pour effectuer d'autres tâches, comme arrêter un service ou exécuter d'autres commandes.
Ajouter des scripts à un modèle de sauvegarde
Suivez les instructions ci-dessous pour ajouter les scripts à un modèle de sauvegarde.
Cliquez sur Plans de sauvegarde, puis sélectionnez Modèles dans la liste déroulante.
Sélectionnez un modèle qui inclut une stratégie OnVault.
La page Modèles s'ouvre avec le modèle de sauvegarde sélectionné.
Dans la section Règles de la page Modèles, cliquez sur + Ajouter à côté de Instantané.
Cliquez sur Paramètres de stratégie avancés.
Faites défiler la page jusqu'à Nom du script, puis copiez et collez le script suivant:
Le résultat s'affiche pour deux bases de données différentes. L'une est une tâche de sauvegarde des journaux, et l'autre un instantané incrémentiel.
$ cat /tmp/backup_status.log
Mer 22 nov 06:11:49 UTC 2023 Job_0275501_ORCLCDB_ARCHIVELOG_BACKUP JobPhase:INIT
Mer 22 nov 06:13:52 UTC 2023 Job_0275501_ORCLCDB_ARCHIVELOG_BACKUP JobPhase:PRE
Mer 22 nov 06:15:01 UTC 2023 Job_0275501_ORCLCDB_ARCHIVELOG_BACKUP JobPhase:POST
Mer 22 nov 06:15:01 UTC 2023 Job_0275501_ORCLCDB_ARCHIVELOG_BACKUP JobPhase:FINAL
Mer 22 nov 06:18:00 UTC 2023 Job_0275637_SMALL_INCREMENTAL_BACKUP JobPhase:INIT
Mer 22 nov 06:20:15 UTC 2023 Job_0275637_SMALL_INCREMENTAL_BACKUP JobPhase:PRE
Mer 22 nov 06:22:06 UTC 2023 Job_0275637_SMALL_INCREMENTAL_BACKUP JobPhase:POST
Mer 22 nov 06:22:06 UTC 2023 Job_0275637_SMALL_INCREMENTAL_BACKUP JobPhase:FINAL
Transmettre des variables d'environnement dans un fichier journal
Vous pouvez exporter les informations d'environnement de la tâche vers le script à l'aide des variables d'environnement de la liste suivante commençant par ACT_.
Toutes les variables d'environnement ne s'appliquent pas à tous les types de jobs.
ACT_APPID: ID de la base de données de l'application.
ACT_APPNAME: nom de l'application.
ACT_HOSTNAME: nom de l'hôte cible de cette tâche.
ACT_JOBNAME: nom de la tâche.
ACT_JOBTYPE: version textuelle de la classe de travail.
ACT_LOGSMART_TYPE: la seule valeur valide est "db". Il doit être présent pour que les journaux de la base de données soient capturés.
ACT_OPTIONS: options de stratégie qui s'appliquent à cette tâche
ACT_PHASE: chaîne de texte décrivant la phase de la tâche.
ACT_POLICY: nom de la règle associée à cette tâche.
ACT_POLICYOPT_logbackupretention: nombre de jours de conservation pour les tâches de sauvegarde des journaux. Cette option n'apparaît que dans les tâches de sauvegarde des journaux de base de données.
ACT_PROFILE: nom du profil.
ACT_SCRIPT_TMOUT: délai avant expiration de la superscripting. Si la réponse n'est pas reçue dans le délai avant expiration (60 secondes par défaut), le script échoue.
ACT_SOURCEHOST: nom de l'hôte qui était la source de cette application.
ACT_TEMPLATE: nom du modèle associé à la tâche.
ACT_TIMEOUT: durée définie du script, en secondes. Si le script ne se termine pas avant l'expiration du délai, la tâche échoue.
ACT_VOLUMES: pour les applications génériques, liste des volumes configurés pour la sauvegarde. Si vous avez besoin d'aide pour connaître les variables d'environnement que vous pouvez utiliser, ajoutez cette ligne à votre script.
echoprintenv >> /tmp/backup_status.log
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/04 (UTC)."],[[["\u003cp\u003eThis guide details how to add and use scripts within a backup template to perform actions during different phases of a backup job, such as at the start, during, or after completion.\u003c/p\u003e\n"],["\u003cp\u003eScripts must be placed in specific folders depending on the host operating system: \u003ccode\u003e\\Program Files\\Google Cloud Backup and DR\\scripts\u003c/code\u003e for Windows, and \u003ccode\u003e/act/scripts\u003c/code\u003e for Linux, where the Linux scripts also need to be executable.\u003c/p\u003e\n"],["\u003cp\u003eScripts can execute in five different phases: INIT (job initialization), PRE (before the main job operation), POST (after the main job operation), FINAL (job completion), and ABORT (job failure), allowing for tailored actions at each stage.\u003c/p\u003e\n"],["\u003cp\u003eEnvironment variables prefixed with \u003ccode\u003eACT_\u003c/code\u003e can be used within the scripts to pass job-specific information like job name, job type, and application details into the script for conditional logic and logging purposes.\u003c/p\u003e\n"],["\u003cp\u003eScripts can be added to a backup template through the "Advanced Policy Settings" section, using a specific syntax to define the path and phase of each script, and a timeout can be defined for each script phase to prevent scripts from running indefinitely.\u003c/p\u003e\n"]]],[],null,["# Scripts to a backup template\n\nThis page explains how to add scripts to a backup template. It describes\nthe different phases that a script executes during a backup job.\n\nYou can write your scripts and run them to perform an action at the start of\na backup job, during it, or after it's completed. These are also called pre\nand post scripts.\n\nScripts location\n----------------\n\nA script folder is created automatically during agent installation where\nthe file system or database that needs protection is running. You need to place\nthe scripts in the following folders in order to execute them:\n\n- For Windows host: `\\Program Files\\Google Cloud Backup and DR\\scripts`\n- For Linux host: `/act/scripts` and set the script to executable (chmod 755 \\*.sh)\n\nScript phases\n-------------\n\nScripts execute in the following phases. If your script isn't executing,\ncheck if it is in the [scripts folder](#location).\n\n- **INIT**: This phase starts when the backup/recovery appliance connects to the agent, In this phase, the job is initialized and the credentials are verified.\n- **PRE**: This phase starts just before the major operation of the job. For snapshots, this phase starts before the application is frozen. For mount type jobs, this is after devices are mapped to the host but before agent based operations like rescan, import, and mounting of file systems is started.\n- **POST**: This phase starts immediately after the major operation of the job is completed. For backup type jobs, this is after the application is unfrozen. For mount type jobs this phase starts after all import, mount, or bring applications online operations are complete.\n- **FINAL**: This phase is the end of the job. Unless the script returns a non-zero code that causes the job to fail, the job is complete.\n- **ABORT**: This phase handles aborted jobs when they fail.\n\nExample: scripts\n----------------\n\nThe following example shows how to run a host side script at various phases\nof the backup job process. It has five scripts that output the date or time,\napp name (file system or database name), job type, job name, log retention value\nin days, and text to indicate the phase of the backup job that is processing. \n\n ```sh\n $ cat /act/scripts/init.sh\n #!/bin/sh\n if [ \"${ACT_JOBTYPE}\" = \"logbackup\" ]; then\n echo `date` \"${ACT_JOBNAME}_${ACT_APPNAME}_ARCHIVELOG_BACKUP JobPhase:INIT\" \u003e\u003e /tmp/backup_status.log\n else\n echo `date` \"${ACT_JOBNAME}_${ACT_APPNAME}_INCREMENTAL_BACKUP JobPhase:INIT\" \u003e\u003e /tmp/backup_status.log\n fi\n\n $ cat /act/scripts/pre.sh\n #!/bin/sh\n if [ \"${ACT_JOBTYPE}\" = \"logbackup\" ]; then\n echo `date` \"${ACT_JOBNAME}_${ACT_APPNAME}_ARCHIVELOG_BACKUP JobPhase:PRE\" \u003e\u003e /tmp/backup_status.log\n else\n echo `date` \"${ACT_JOBNAME}_${ACT_APPNAME}_INCREMENTAL_BACKUP JobPhase:PRE\" \u003e\u003e /tmp/backup_status.log\n fi\n\n $ cat /act/scripts/post.sh\n #!/bin/sh\n if [ \"${ACT_JOBTYPE}\" = \"logbackup\" ]; then\n echo `date` \"${ACT_JOBNAME}_${ACT_APPNAME}_ARCHIVELOG_BACKUP JobPhase:POST\" \u003e\u003e /tmp/backup_status.log\n else\n echo `date` \"${ACT_JOBNAME}_${ACT_APPNAME}_INCREMENTAL_BACKUP JobPhase:POST\" \u003e\u003e /tmp/backup_status.log\n fi\n\n $ cat /act/scripts/fini.sh\n #!/bin/sh\n if [ \"${ACT_JOBTYPE}\" = \"logbackup\" ]; then\n echo `date` \"${ACT_JOBNAME}_${ACT_APPNAME}_ARCHIVELOG_BACKUP JobPhase:FINAL\" \u003e\u003e /tmp/backup_status.log\n else\n echo `date` \"${ACT_JOBNAME}_${ACT_APPNAME}_INCREMENTAL_BACKUP JobPhase:FINAL\" \u003e\u003e /tmp/backup_status.log\n fi\n\n $ cat /act/scripts/abort.sh\n #!/bin/sh\n if [ \"${ACT_JOBTYPE}\" = \"logbackup\" ]; then\n echo `date` \"${ACT_JOBNAME}_${ACT_APPNAME}_ARCHIVELOG_BACKUP JobPhase:ABORT\" \u003e\u003e /tmp/backup_status.log\n else\n echo `date` \"${ACT_JOBNAME}_${ACT_APPNAME}_INCREMENTAL_BACKUP JobPhase:ABORT\" \u003e\u003e /tmp/backup_status.log\n fi\n ```\n\nIn the preceding example, `ACT_JOBTYPE = logbackup` indicates the job being run\nis a log backup task, rather than a full database backup task.\n\nYou might not need to run all five phases to achieve your chosen log output.\nYou can use only the init and final phases to indicate the start and end of\nthe backup process.\n\nYou can also customize the scripts to perform other tasks, such as stopping a\nservice or running other commands.\n\nAdd scripts to a backup template\n--------------------------------\n\nUse the following instructions to add the [scripts](#phases) to a backup\ntemplate.\n\n1. Click **Backup plans** and select **Templates** from the drop-down list.\n2. Select a template that includes an OnVault policy. The **Templates** page opens with the selected backup template.\n3. In the **Policies** area of the **Templates** page, click **+Add** next to **Snapshot**.\n4. Click **Advanced Policy Settings**.\n5. Scroll to **Script Name**, then copy and paste the following script:\n\n path=init.sh:phase=init;path=pre.sh:phase=pre;path=post.sh:phase=post;path=fini.sh:phase=final:timeout=300;path=abort.sh:phase=abort\n\n | **Note:** The use of timeout and arguments is not mandatory, but they can be used for each different phase where required. We added a timeout of 300 seconds to the final phase for this example, but you are able to add timeout values to any phase of the script. If the timeout is reached and the script is still running, the script will exit. The default timeouts for pre, post, and final scripts are 60 seconds, while init scripts have a default timeout of 120 seconds.\n6. Click **Save Changes**.\n\n7. [Run an on-demand job](/backup-disaster-recovery/docs/monitor-reports/monitor-jobs#run).\n\n The output is displayed for two different databases. One is a log backup job, and one is an incremental snapshot.\n\n $ cat /tmp/backup_status.log\n Wed Nov 22 06:11:49 UTC 2023 Job_0275501_ORCLCDB_ARCHIVELOG_BACKUP JobPhase:INIT\n Wed Nov 22 06:13:52 UTC 2023 Job_0275501_ORCLCDB_ARCHIVELOG_BACKUP JobPhase:PRE\n Wed Nov 22 06:15:01 UTC 2023 Job_0275501_ORCLCDB_ARCHIVELOG_BACKUP JobPhase:POST\n Wed Nov 22 06:15:01 UTC 2023 Job_0275501_ORCLCDB_ARCHIVELOG_BACKUP JobPhase:FINAL\n Wed Nov 22 06:18:00 UTC 2023 Job_0275637_SMALL_INCREMENTAL_BACKUP JobPhase:INIT\n Wed Nov 22 06:20:15 UTC 2023 Job_0275637_SMALL_INCREMENTAL_BACKUP JobPhase:PRE\n Wed Nov 22 06:22:06 UTC 2023 Job_0275637_SMALL_INCREMENTAL_BACKUP JobPhase:POST\n Wed Nov 22 06:22:06 UTC 2023 Job_0275637_SMALL_INCREMENTAL_BACKUP JobPhase:FINAL\n\nPassing environment variables into a log file\n---------------------------------------------\n\nYou can export the environment information from the job to the script using\nthe environment variables in the following list that are prefixed with `ACT_`.\nNot all of the environment variables will be applicable to every job type.\n\n- `ACT_APPID`: the database ID of the application.\n- `ACT_APPNAME`: the name of the application.\n- `ACT_HOSTNAME`: the name of the host which is the target of this job.\n- `ACT_JOBNAME`: the name of the job.\n- `ACT_JOBTYPE`: a text version of the job class.\n- `ACT_LOGSMART_TYPE`: the only valid value is \"db\". This must be present for database logs to be captured.\n- `ACT_OPTIONS`: the policy options that apply to this job\n- `ACT_PHASE`: a text string that describes the job phase.\n- `ACT_POLICY`: the name of the policy related to this job.\n- `ACT_POLICYOPT_logbackupretention`: the number of days of retention for log backup jobs. This only appears in database log backup jobs.\n- `ACT_PROFILE`: the name of the profile.\n- `ACT_SCRIPT_TMOUT`: the superscripting timeout. If the response is not received within the timeout value (default 60 seconds), then the script will fail.\n- `ACT_SOURCEHOST`: the name of the host that was the source for this application.\n- `ACT_TEMPLATE`: the name of the template related to the job.\n- `ACT_TIMEOUT`: the defined duration of the script, in seconds. If the script does not complete within the timeout period, the job will fail.\n- `ACT_VOLUMES`: for generic applications, the list of volumes that are configured for backup. If you need help learning the possible environment variables you can use, add this line to your script.\n\n echo printenv \u003e\u003e /tmp/backup_status.log"]]