ShinChven's Blog

Backup Files on Google Colab with Cron Job

Sat May 20 2023

Google Colab is a great tool for machine learning. However, the virtual machine will be recycled once AFK for a while. If you don't save your files, you will lose them.

I've lost my files several times. So I decided to write a script to backup my files to Google Drive.

Auto file backup can be done with cron job. Here's the code snippet:

# mount google drive
from google.colab import drive

# install cron job
!apt-get install cron

# create a script to zip the files to the drive
!touch zip_file.sh
!echo "#!/bin/bash" >> /content/zip_file.sh
!echo 'cd <FOLDER_TO_BACKUP> && zip -r /content/drive/MyDrive/file_$(date +%Y-%m-%d).zip ./' >> /content/zip_file.sh

# add execution permission
!chmod +x /content/zip_file.sh

# create a cron job to run the script every 10 minutes
!echo "*/10 * * * * /bin/bash /content/zip_file.sh" | crontab -

# start cron job
!service cron start