[파이썬] 자동화된 백업 스크립트

Have you ever accidentally lost important data or files? It can be frustrating and time-consuming to recover or recreate them. But fear not! With the power of automation, you can save yourself from such headaches by using an automated backup script.

In this blog post, we will walk you through how to create a simple yet effective backup script using Python. This script will automatically create backups of your files and directories at specified intervals, ensuring that your data is always safe and secure.

Getting Started

To get started, make sure you have Python installed on your machine. You can check if Python is installed by opening a terminal or command prompt and running the following command:

python --version

If you don’t have Python installed, visit the official Python website and follow the instructions to download and install it.

Setting Up the Script

First, let’s create a new Python script file and import the necessary modules:

import shutil
import os
import datetime

The shutil module provides a high-level interface for copying files and directories, while the os module gives us access to various operating system functionalities. Finally, we import the datetime module to generate timestamps for our backup filenames.

Defining the Backup Location

Next, we need to define the backup location. Choose a directory where you want to store your backups, and assign its path to the backup_dir variable:

backup_dir = "/path/to/backup/directory"

Replace “/path/to/backup/directory” with the actual path to your desired backup directory.

Specifying Files and Directories to Backup

Now, let’s specify the files and directories that we want to include in our backups. Create a list named backup_list and add the file and directory paths to it:

backup_list = [
    "/path/to/file1.txt",
    "/path/to/file2.docx",
    "/path/to/folder1",
    "/path/to/folder2"
]

Replace each “/path/to/file” and “/path/to/folder” with the actual paths of the files and directories you want to include in your backups.

Creating the Backup Script

Now it’s time to write the main backup script. Add the following code snippet to your script file:

def backup_files():
    backup_name = f"backup_{datetime.datetime.now().strftime('%Y-%m-%d')}.zip"
    backup_path = os.path.join(backup_dir, backup_name)
    shutil.make_archive(backup_path, 'zip', *backup_list)
    print("Backup Successful!")

backup_files()

The backup_files function creates a backup name based on the current date and time. It then generates a full path to the backup file by joining the backup_dir and backup_name variables. Finally, it uses the shutil.make_archive function to create a zip file containing all the files and directories specified in backup_list.

Automating the Backup

To automate the backup process, we can use the built-in cron or Task Scheduler functionalities of our operating system. These tools allow us to schedule the execution of our backup script at regular intervals.

On Unix-like systems (Linux, macOS), you can add a cron job by running the following command:

crontab -e

Then, add the following line to run the backup script daily at 2:00 AM:

0 2 * * * python /path/to/backup_script.py

On Windows systems, you can use the Task Scheduler to schedule the execution of the backup script. Open the Task Scheduler, create a new task, and specify the script to be executed at the desired interval.

Conclusion

With the power of automation, we have created a simple yet effective backup script using Python. By scheduling the execution of this script, you can ensure that your important files and directories are regularly backed up, providing peace of mind and protection against data loss.

Feel free to customize the script further to suit your specific backup needs. You can add additional functionality such as compression, encryption, or sending notifications upon backup completion.

Remember, it’s always better to be safe than sorry when it comes to your valuable data!