Auto back up using python

36 views Asked by At

I am utilizing the Python code below to take an automated backup of my files.

import shutil
import os
def backup_folder(source_folder, backup_folder):
    try:
        # Check if the source folder exists
        if not os.path.exists(source_folder):
            print(f"Error: Source folder '{source_folder}' does not exist.")
            return
        # Create the backup folder if it doesn't exist
        if not os.path.exists(backup_folder):
            os.makedirs(backup_folder)
        # Copy contents of source folder to backup folder
        shutil.copytree(source_folder, os.path.join(backup_folder, os.path.basename(source_folder)))
        print(f"Backup completed successfully.")
    except Exception as e:
        print(f"Error: {e}")
# Example usage
source_folder = '/path/to/source_folder'
backup_folder = '/path/to/backup_folder'

However, when I run it, the backup folder is not getting created. I have tried running it as root, but that did not work either. I am not sure what the issue is. python3 <name.py>

Are there any other considerations that I may have overlooked? In addition, do you have any ideas on how to automate this process? I'm wondering if it would be possible to use crontab or a Jenkins Job.

0

There are 0 answers