Created
April 16, 2023 14:16
-
-
Save 0xdeadbeer/be247747968840f3748ffa7a60d0f0be to your computer and use it in GitHub Desktop.
Backup to Google Drive through rclone
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: destination rclone drive") | |
print(" - third_param: destination folder") | |
print(" - fourth_param: backup filename prefix") | |
print(" - fifth_param: backups limit") | |
# first_param: list containing file locations to backup | |
# second_param: destination rclone drive | |
# third_param: destination folder | |
# fourth_param: backup filename prefix | |
# fifth_param: backups limit | |
def main(): | |
if (len(sys.argv) != 6): | |
help() | |
return | |
# set environment variables | |
print("ENC_PASSWORD " + os.getenv("ENC_PASSWORD")) | |
print("RCLONE_PASSWORD " + os.getenv("RCLONE")) | |
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) | |
destination_drive = sys.argv[2] | |
now = datetime.now().strftime("%d_%m_%y_%H_%M_%S") | |
backup_filename = f"{sys.argv[4]}_{now}" | |
remote_destination = sys.argv[3] | |
os.system(f"zip -r - {to_backup} | openssl aes-256-cbc -a -salt -pbkdf2 -in - -out /dev/stdout -pass pass:$ENC_PASSWORD | RCLONE_CONFIG_PASS=$RCLONE rclone rcat {destination_drive}:{remote_destination}/{backup_filename}.enc ") | |
# number of items in folder | |
number_of_backups_command = f"RCLONE_CONFIG_PASS=$RCLONE rclone ls {destination_drive}:{remote_destination} | wc -l" | |
backups_number = int(os.popen(number_of_backups_command).read()) | |
print("Number of backups " + str(backups_number)) | |
while (backups_number > int(sys.argv[5])): | |
awk_subcommand = "awk '{print $2 \" \" $3 \" \" $4}'" | |
awk_second_subcommand = "awk '{print $3}'" | |
oldest = os.popen(f"RCLONE_CONFIG_PASS=$RCLONE rclone lsl {destination_drive}:{remote_destination} | {awk_subcommand} | sort --stable --key=1,2 | {awk_second_subcommand} | head -n 1").read() | |
os.system(f"RCLONE_CONFIG_PASS=$RCLONE rclone delete {destination_drive}:{remote_destination}/{oldest}") | |
backups_number = int(os.popen(number_of_backups_command).read()) | |
# we are done babyyy | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment