Last active
April 29, 2024 20:58
-
-
Save 0xdeadbeer/1393329c9d08b858befe384cbf1e2142 to your computer and use it in GitHub Desktop.
Cronjob backup script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import os, os.path | |
import sys | |
import time | |
from datetime import datetime | |
def help(): | |
print("Data reaper!") | |
print("Written by: @0xdeadbeer") | |
print(" - first_param: list containing file locations to backup") | |
print(" - second_param: new backup output directory") | |
print(" - third_param: backup filename prefix") | |
print(" - fourth: backups limit") | |
print(" - $ZIP_PASSWORD: password for the zip") | |
# first_param: list containing file locations to backup | |
# second_param: new backup output directory | |
# third_param: backup filename prefix | |
# fourth_param: backups limit | |
def main(): | |
if (len(sys.argv) != 5): | |
help() | |
return | |
# set environment variables | |
print("ZIP_PASSWORD " + os.getenv("ZIP_PASSWORD")) | |
backup_links = open(sys.argv[1], "r") | |
to_backup = [] | |
for line in backup_links: | |
line = line.strip() | |
if (not line): continue | |
if (line[0] == "#"): continue | |
to_backup.append(line) | |
to_backup = " ".join(to_backup) | |
backup_dir = sys.argv[2] | |
now = datetime.now().strftime("%d_%m_%y_%H_%M_%S") | |
backup_filename = f"{sys.argv[3]}_{now}" | |
output_location = f"{backup_dir}/{backup_filename}.zip" | |
os.system(f"zip --password $ZIP_PASSWORD -r {output_location} {to_backup}") | |
# number of items in folder | |
backups_number = len([name for name in os.listdir(backup_dir)]) | |
print("Number of backups " + str(backups_number)) | |
while (backups_number > int(sys.argv[4])): | |
os.system(f"rm -rf $(ls -t -d {backup_dir}/* | tail -1)") | |
backups_number = len([name for name in os.listdir(backup_dir)]) | |
# we are done gg guys | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment