How to Automate Backups with Python and Cron Jobs on Linux

Chinmay Roy
3 min readDec 24, 2024

--

In today’s fast-paced digital world, ensuring data security is crucial. Automating data backups is one of the simplest and most effective ways to protect valuable information. This guide demonstrates how to automate backups using Python and schedule them with cron jobs on a Linux system.

Step 1: Writing the Python Script

The first step is to create a Python script that performs the backup. Below is a simple example:

import datetime
import os

# Data to be backed up
data_list = [
"Data entry 1",
"Data entry 2",
"Data entry 3",
"Data entry 4",
]

def backup_data():
# Define the backup directory
backup_dir = "/home/chinmay/ChinmayWorks/"
os.makedirs(backup_dir, exist_ok=True) # Ensure the directory exists

# Generate a timestamped backup file name
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
backup_filename = os.path.join(backup_dir, f"backup_{timestamp}.txt")

# Write the data to the backup file
with open(backup_filename, 'w') as f:
for item in data_list:
f.write(f"{item}\n")

print(f"Backup complete! Data saved to {backup_filename}")

if __name__ == "__main__":
backup_data()

This script:

  • Stores a list of data entries.
  • Creates a timestamped file in a designated backup directory.
  • Writes the data entries into the backup file.

Save the script as cron.py in the directory /home/chinmay/ChinmayWorks/.

Step 2: Installing Required Tools on Linux

Ensure that Python is installed on your Linux system. You can verify this by running:

python3 --version

If Python is not installed, install it using:

sudo apt update
sudo apt install python3

Additionally, ensure cron is installed and running:

sudo apt install cron
sudo systemctl enable cron
sudo systemctl start cron

Step 3: Scheduling with Cron Jobs

Cron jobs allow tasks to be scheduled and executed automatically at specified times on Linux.

  1. Edit the Crontab File
    Open the crontab file with the following command:
crontab -e
  • If it’s your first time using crontab, you may be prompted to choose an editor. You can select:
  • nano (a user-friendly text editor): Press 1 and hit Enter or Manually enter this command on terminal
EDITOR=nano crontab -e
  • vi (a more advanced editor): Press 2 and hit Enter.

2. Add a Cron Job
To run the backup script every minute, add the following line:

* * * * * /usr/bin/python3 /home/chinmay/ChinmayWorks/cron.py

3. Save and Exit

  • If you’re using nano, save by pressing Ctrl + O, hit Enter, and exit with Ctrl + X.
  • If you’re using vi, press Esc, type :wq, and press Enter.

4. Restart Cron (Optional)
If you’ve made significant changes, restart the cron service:

sudo systemctl restart cron

Step 4: Verify Execution

To confirm the script runs as expected:

  • Check Output Files: The backups should appear in /home/chinmay/ChinmayWorks/ with timestamped filenames.
  • Inspect Logs: If logging is enabled, you can check /home/chinmay/cron.log for any errors or confirmation messages.

Step 5: Redirect Logs for Troubleshooting

Redirecting cron job output and errors to a log file can simplify debugging. Update the cron job as follows:

* * * * * /usr/bin/python3 /home/chinmay/ChinmayWorks/cron.py >> /home/chinmay/cron.log 2>&1

Use the following command to view the log:

tail -f /home/chinmay/cron.log

For more detailed troubleshooting, inspect system logs specific to cron:

grep CRON /var/log/syslog

Advantages of This Approach

  1. Automation: Backups are created automatically without manual intervention.
  2. Reliability: Timestamped files ensure no overwriting.
  3. Linux Compatibility: Utilizes built-in Linux tools like cron for scheduling.
  4. Simplicity: Python’s built-in modules handle the logic, and cron manages scheduling.

Conclusion

By combining Python and cron jobs on Linux, you can create an automated backup system that ensures your data is always safe. Whether for personal projects or professional use, this setup is versatile and effective.

Next Steps: Enhance the script by adding error handling, email notifications, or compressing backup files to save space. (If need then comment down below, I will write another article)

--

--

Chinmay Roy
Chinmay Roy

No responses yet