My own host and a backup script in python

Some time ago I decided that instead of sharing some space in a hosting service to have all my websites I was going to rent my own private server. I could had plugged one old machine to the network at home and save the money but if I have to pay every month a fee I’m sure I will make good use of it.

Now two years later the experience have proved to be great, I manage remotely my own Ubuntu machine (my knowledge in Unix systems have improved a lot), my own http server (nginx), I have services running (GIT, some node.js scripts) and I’m hosting several websites from some of my friends.

I’m using linode.com (great service) for a virtual machine of 256 MB of RAM (more than what I need), it costs me 20 Euros per month which I think is fair.

Thanks to that I’ve been able to code all kind of crazy ideas which it would had been impossible without my own remote machine, and I encourage to all the webmasters to give it a try one day to pass to a next level.
Something I miss when working in Unix is a good backup service, so today I made my first step into the backups system of my host.

I have made a little script to manage the backups, I wanted to do it in Javascript using Node.js but I end up using Python which has better modules for file handling.

Now when I am in some random folder I can call this command – backmeup name * – it copies all files from this folder matching the wildcard to the backups/$data/name folder. A simple way to have some backups of folders of great importance.

Here is the script:

#!/usr/bin/python
 
print "backing you up, bro"
backups_folder = "/home/tamat/backups/"
 
import os,sys,glob,datetime,getpass
from subprocess import call
 
wildcard = "*"
 
if len(sys.argv) < 2:
    print "sorry bro, I need a backup name"
    sys.exit(0)
 
backup_name = sys.argv[1]
 
files = sys.argv[2:]
 
if len(files) == 0:
    print "no files to backup, justsayin'"
    sys.exit(0)
 
now = datetime.datetime.now()
folder_name = backups_folder + now.strftime("%Y-%m-%d") + "/"
 
if not os.path.isdir(folder_name):
    os.mkdir( folder_name )
 
if backup_name != "" and backup_name != "this":
    folder_name = folder_name + backup_name + "/"
 
if not os.path.isdir(folder_name):
    print "creating folder, yo: " + backup_name
    os.mkdir( folder_name )
 
text_file = open(folder_name + "backup.log", "a")
text_file.write("saved from '"+os.getcwd()+"' by '"+getpass.getuser()+"' on " + now.strftime("%Y-%m-%d %H:%M") + "\n")
text_file.close()
 
for file in files:
	print " + " + file
	call(["cp","-R",file, folder_name])
 
print "didit"

3 Responses to “My own host and a backup script in python”

  1. bram Says:

    Javi! You should have a look at rsnapshot. It’s very simple to use and it has some awesome features. It does incremental backups while keeping the space needed very low using symbolic links… If you need some help I can help.

  2. tamat Says:

    Interesting… I will give it a look, thanks a lot for the info! the other option was to use GIT in a more general way but I Was scared of messing the whole system.

  3. bram Says:

    on our servers I use backupninja (which takes dumps of databases, very easy to set up also) together with rsnapshot. So I have daily, weekly and monthly backups, but the whole thing takes less than 2x the space for one full backup. The same on our network attached storage….

Leave a Reply


× 6 = six