0

I am trying to download every file from my ftp server (as a kind of backup program), and then zip it. So far all i have is this code.

import os
from ftplib import FTP
from datetime import datetime

dt = datetime.today()
start=datetime.now()
ftp = FTP("xxxxxxxxxxxxxxxxx")
ftp.login("username","password")

path = "/home/pi/backups/"+str(dt.day)+"-"+str(dt.hour)+"-backup/"

try:
    os.mkdir(path)
except OSError:
    print ("Creation of the directory %s failed" % path)
else:
    print ("Successfully created the directory %s " % path)

files = ftp.nlst()
print(dt.minute)

directory_contents = os.listdir(path)
print(directory_contents)

for file in files:
    print("Getting "+file)
    ftp.retrbinary("RETR " + file,open(path + file, "wb").write)

ftp.close()

diff = dt - start
print("Backup took "+str(diff.seconds)+" seconds!")

What this code does, is it downloads every file, that isn't in a sub directory. I would like every single file and then it should download them to /home/pi/backups/"+str(dt.day)+"-"+str(dt.hour)+"-backup/. Thank you if i get an answer i have been experimenting for 2 hours!

  • See [Downloading a directory tree with ftplib](https://stackoverflow.com/q/2605119/850848). – Martin Prikryl Jun 21 '20 at 13:50
  • I got this error @MartinPrikryl Traceback (most recent call last): File "/home/pi/backup2.py", line 46, in downloadFiles(source,dest) File "/home/pi/backup2.py", line 31, in downloadFiles downloadFiles(path+file+"/",destination) File "/home/pi/backup2.py", line 35, in downloadFiles os.chdir(destination[0:len(destination)-1]+path) FileNotFoundError: [Errno 2] No such file or directory: '/rawbackups/21-15-backup/banned-ips.json/' –  Jun 21 '20 at 14:44
  • Edit your question with [mcve]. – Martin Prikryl Jun 21 '20 at 14:59

0 Answers0