Let’s delve into a more inventive task. In this scenario, we’ll establish a job aimed at capturing periodic backups from a specific directory within the system, for instance, /tmp/important.
The most challenging aspect revolves around devising the script responsible for executing the task. The subsequent steps entail instructing Jenkins to undertake this task periodically. Let’s examine how the script would appear:
Initially, we need to generate a tar archive for the designated directory. This can be achieved through a command such as tar -cvf /tmp/backup.tar /tmp/ important.
However, the challenge lies in creating multiple backups for the directory in a recurring manner. Each backup should be timestamped to denote the moment it was created and to prevent overwriting of backups. A timestamp can be effortlessly obtained in the bash shell using a command akin to the following: date +%Y%m%d_%H%M%S. This command yields a string resembling 20170729_152805, representing July 29th, 2017, at 03:08:05 PM.
Hence, we aim to construct a TAR file encompassing this timestamp. The comprehensive script is as follows:
stamp=$(date +%Y%m%d_%H%M%S)
tar -cvf /tmp/backup.$stamp.tar /tmp/important
gzip /tmp/backup.$stamp.tar
Proceed to the Jenkins Dashboard, initiate the creation of a new job titled “vagrant_home_backup,” and incorporate the aforementioned script as a build step. This time, navigate to the “Build Triggers” section and select “Build Periodically.”
If you possess familiarity with Linux cron jobs, employing Periodic Builds will feel intuitive. They function in a manner akin to cron jobs. Let’s establish a schedule for conducting backups daily at 03:00 PM: 0 15 * * *.
For those unfamiliar with cron jobs, let’s dissect the aforementioned pattern:
- 0: signifies minute zero
- 15: denotes 3 P.M.
- *: signifies “every” for the remaining fields, which are, in sequence: day of the month, month, and day of the week (with Monday as 1, progressing to Sunday, represented as 0 or 6).
Scheduled jobs can also be configured for more intricate scenarios:
- -: signifies a range of values. For instance, 0-6 in the hours section implies execution between 12 and 6 A.M., subject to other field values.
- /: denotes intervals. For instance, */5 in the minutes section implies execution every five minutes. It can be combined with ranges, for instance, 0-30/5 in the minutes field implies execution every 5 minutes solely between minutes 0 and 30 (resulting in six executions).
Alternatively, you can specify discrete values for execution times. For instance, 1,3,5 in the hours section implies execution only at hours 1, 3, and 5.
Jenkins introduces a distinctive feature absent in cron jobs: the hash. The hash generates a random value for a field, primarily used when multiple jobs are scheduled for simultaneous execution. For instance, if four jobs are slated for daily execution at 03:00 AM, the schedule can be written as follows: 0 3 * * *. However, this might strain system resources, especially if the jobs are collectively CPU intensive. A preferable approach involves using the hash, denoted as H, like so: H 3 * * *. This ensures each job executes at 3 A.M. but at a different minute, distributing the load evenly across time.“`
Establishing a backup project in Jenkins involves crafting a script to generate periodic backups from a specified directory. This entails creating TAR archives with timestamped filenames to prevent overwriting. By configuring periodic builds and scheduling them using cron-like expressions, Jenkins facilitates automated backup operations.