How To Zip Sub-folder Content Generated in the Last 24 Hours On AWS Lightsail (Linux)

Zipping Subfolder Content Generated in the Last 24 Hours On AWS Lightsail (Linux)

This post is in continuation to my last post about How To Create Daily Automated Backups On An Amazon Lightsail Instance?. While the previous post was what I needed to create a daily automated backup on my AWS Linux stack, the requirement quickly changed when the underlying data folder that I was archiving daily underwent a re-zig.

The current folder for which I was creating automatic daily backups on my AWS Bitnami LAMP stack now had multiple subfolders with files being created daily in each subfolder. So, the change needed now was 2-step.

  • Create an automatic daily archive with multiple folders inside the main content folder
  • Add only the files created in the last 24 hours, to the zip file

Let’s take 1 thing at a time.

How to zip a large folder with subfolders and files?

This step thankfully, did not need any real change to the command. The same is mentioned below:

#! /bin/bash

source_folder = "/opt/bitnami/apache/htdocs/website_root/storage/app/public/daily_images"
backup_folder = "/opt/bitnami/apache/htdocs/website_root/public"
backup_file = "$backup_folder/backup.zip"

zip -r "$backup_file" "$source_folder"

How to add files created in the last 24 hours, inside subfolders?

This was the tricky bit. What we were trying to do here was, get only those files that were created in the last 24 hours inside one or all the subfolders of the main content folder.

To do that, the command had to be modified significantly as we were doing multiple things now. Firstly, we had to go through each sub-folder and find only the files that were created in the past 24 hours.

Then, a zip file was to be created with only the files that were created in the last 24 hours.

Let’s see how the revised command looks like, below:

find /opt/bitnami/apache/htdocs/website_root/storage/app/public/images/daily_images/{SubFolder1,SubFolder2,SubFolder3}" -type f -ctime -1 -exec zip -r "/opt/bitnami/apache/htdocs/website_root/public/backup.zip {} +

This update does the job perfectly. It first finds all files that were created in the last 24 hours (-ctime -1) and then zips them archive to finally create the backup.zip file.

Share This Post

2 thoughts on “How To Zip Sub-folder Content Generated in the Last 24 Hours On AWS Lightsail (Linux)”

  1. Pingback: How To Create Daily Automated Backups on an Amazon Lightsail Instance? - Rajiv Verma

  2. Pingback: How To Set Timezone in Linux? - Rajiv Verma

Leave a Comment

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

Subscribe To my Future Posts

Get notified whenever I post something new

More To Explore