📜 ⬆️ ⬇️

Backup files, database and server settings in Dropbox

For several years now, the opportunity has appeared for any mortal who wants to rent not only a shared-hosting, but also a “full-fledged” server with root-access and the ability to configure it the way you want yourself. Set up, for example, in addition to the web-server also a bunch of other services.
I did the same a few years ago. At first I rented one server, then another, and transferred the settings using handles, finding the necessary files in the / etc directory.

For a couple of years, several friends' blogs have settled on my server, and even a mail server, as I haven’t wanted Google for a long time. About the safety of data was thought after each article on Habré, but it was somehow not up to it. And, as they say, admins are divided into three categories: those who do not back up, those who already do, and those who even check for recoverability from backups. It happened with me, even though the hoster is very good, but they had an accident with hard drives. Yes, such that a week they tried to restore the discs and preliminary estimates were very disappointing. And I did not have backups. What mood I had in those days you can imagine.

But after a few days, the hosting technicians managed to restore the data and launch all the virtual servers on that node. And I thought about backups. I thought so - the backup should not be on the same server (naturally!), It is desirable that it be on my computer, but not in one instance. I thought about installing FTP on my home computer and even sending archives with letters, but all these options did not suit me. And I realized that I had to try Dropbox, which I had already used for a couple of years by that time, and I had about 18 free gigs.
')

Benefits of backup in Dropbox:



What does the script submitted by me do?



How it's done?


In short, an “application” is created from the point of view of the Dropbox platform, authorized by the user (that is, us), and a script is written that uses authorization data and uploads backup files to Dropbox.

Or rather?


Step 1 - Creating the Application

Go to the App Console page, click the “Create app” button, select the “Dropbox API app” type, select the “Files and datastores” option, since we are going to work with files, and in the next paragraph we answer “Yes - My app only needs access to files it creates ", this means that your application will be limited only to its own separate subfolder in the App folder, it will not have access to other files. We come up with the name of your application and click "Create app".
You will see a whole page of settings for the created application, but there is no need to customize anything. But do not close it yet.

Step 2 - Download and Install the SDK

To write applications that will work with files in your Dropbox, you need to go to the Core API section. There we can download the SDKs we need, read the documentation and go through study tours.
Since I believe that the best language for scripting for me is Python, I downloaded the SDK for myself and installed it. Installation is very simple, everything is limited to downloading, unzipping the SDK itself and installing it using the commands " python setup.py install ", or " pip install dropbox ".

Step 3 - Authorization

The Core API library uses OAuth v2 , but Dropbox's Python SDK will take care of how to use it, so you have nothing to worry about and don't have to write everything from scratch.
It is time to build a small script:

#  Dropbox SDK import dropbox #   app_key  app_secret       1 app_key = 'INSERT_APP_KEY' app_secret = 'INSERT_APP_SECRET' flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret) #      authorize_url = flow.start() print '1.   : ' + authorize_url print '2.  "Allow"' print '3.   .' code = raw_input("   : ").strip() #     ,         access_token,    access_token, user_id = flow.finish(code) #        client = dropbox.client.DropboxClient(access_token) print 'linked account: ', client.account_info() #   access_token          print 'access_token: ', access_token 

Step 4 - create a temporary folder and token file

The backup.py script itself is in my / root folder, it also has a temporary backup folder and a dropbox_token.txt file. You also need to create them and write a token to the file from the previous step. The token consists of two lines, in the file they are just like that, with a line break.

Step last - we write a backup script

Expand
 #!/usr/bin/python import os import sys import time import string from os.path import getsize curDate = time.strftime("%d.%m.%Y", time.gmtime()) curDay = time.strftime("%d", time.gmtime()) backupDelay = time.time()-86400 if curDay == "01" or curDay == "15": backupDelay = 0 print "curDate:", curDate # Include the Dropbox SDK libraries from dropbox import client, rest, session # Get your app key and secret from the Dropbox developer website APP_KEY = ' ' APP_SECRET = '  ' # ACCESS_TYPE should be 'dropbox' or 'app_folder' as configured for your app ACCESS_TYPE = 'app_folder' sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE) oauth_token = '' oauth_token_secret = '' f = open("dropbox_token.txt",'r') if f: oauth_token = string.strip(f.readline()) oauth_token_secret = string.strip(f.readline()) f.close() print "oauth token found:", oauth_token, oauth_token_secret if oauth_token == '' or oauth_token_secret == '': request_token = sess.obtain_request_token() # Authorize the application on dropbox site url = sess.build_authorize_url(request_token) print "url:", url print "Please visit this website and press the 'Allow' button, then hit 'Enter' here." raw_input() # This will fail if the user didn't visit the above URL and hit 'Allow' access_token = sess.obtain_access_token(request_token) f = open("dropbox_token.txt","wb") f.write(access_token.key + '\n') f.write(access_token.secret) f.close() else: sess.set_token(oauth_token, oauth_token_secret) client = client.DropboxClient(sess) print "linked account:", client.account_info() def sync_dir(dir): rootdir = dir print "Syncing directory:", rootdir startTime = backupDelay for root, subFolders, files in os.walk(rootdir): for file in files: fname = os.path.join(root,file) if os.path.getmtime(fname)>startTime: #print root, file os.system("mkdir -p 'backup"+root+"'") os.system("cp '"+fname+"' 'backup"+fname+"'") print "Making dump of MySQL databases..." os.system("mysqldump --all-databases -uroot -pROOT__MYSQL -r backup/backup.sql") sync_dir("/var/www") sync_dir("/var/spool/virtual") sync_dir("/home/user") backupName = 'backup_'+curDate+'.7z' print "Creating archive with name", backupName os.system("7z a -p_ "+backupName+" backup/* /etc") f = open(backupName,'rb') if f: fsize = getsize(backupName) uploader = client.get_chunked_uploader(f, fsize) print "Uploading file", fsize, "bytes..." while uploader.offset < fsize: try: upload = uploader.upload_chunked() print "." except rest.ErrorResponse, e: # perform error handling and retry logic print "error uploading file!" uploader.finish("/"+backupName) f.close() print "File uploaded successfully." print "Deleting temp files..." os.system("rm -r backup/*") os.system("rm " + backupName); 

Afterword

Source: https://habr.com/ru/post/236483/


All Articles