You are not logged in.

#1 2005-12-23 01:38:18

punkrockguy318
Member
From: New Jersey
Registered: 2004-02-15
Posts: 711
Website

Sweet backup script with python

I wrote a backup script that i thought others would enjoy.  Please give me your feedback!   big_smile

#!/usr/bin/env python
# sweetbackup version 0.1 - simple python script to backup the system using rsync
# written by lukas sabota
# 12/22/05
# 
# notes:
# * check out the globals and adjust them to your satisfaction
# * touch a backup_ok file into the directory of your back.  You may ask why.  
#       This is to ensure that the proper media is mounted to the filesystem.  It's a big
#       waste of space to backup the entire system onto the same media
# * if your backup fails you'll get a notification on your desktop
# * erm.. that's it.  i think
#
# todo:
# * maybe a conffile might be better than the globals
# * a log
# * some sort of backup notification of failure besides the file in the home folder
# * comment some stuff
# changes:
# 0.1 - initial release.  it's sweet


import os,sys,datetime,time

# important globals
backupdirs=['/etc', '/root', '/home', '/var/log', '/var/www']
dest="/media/usbdisk/backup"
binary='/usr/bin/rsync'
errorfile=os.environ['HOME']+'/Desktop/backupERROR'
options='-av --delete'

def oops(error ,n):
  message = 'Error code '+str(n)+': '+error
  print message
  file = open(errorfile, 'w')
  file.write(str(datetime.datetime.now()))
  file.write('n')
  file.write(message)
  file.write('n')
  file.close
  sys.exit(n)

print 'rsync: ',
if os.access(binary, os.X_OK):
  print 'okay =)'
else:
  print 'nope =('
  oops('The rsync could not be accessed at '+binary, -1)

print 'file: ',
if os.access(dest+'/backup_ok', os.F_OK):
  print 'okay! =)'
else:
  print 'nope =('
  oops(dest+'/backup_ok'+' does not exist.  Please create this file so this script knows that the installation media is available.', -2)

print 'write: ',
if os.access(dest, os.W_OK):
  print 'okay =)'
else:
  print 'nope =('

  oops('Write access is denied to '+dest, -3)
  
for x in backupdirs:
  print 'read ', x, ': '
  if os.access(x, os.R_OK):
    
    print 'good :)'
  else:
    print 'nope =('
    oops('Read access is not granted to'+x, -4)
command = binary + ' ' + options
for x in backupdirs:
  command = command + ' ' + x
command = command + ' ' + dest
if os.system(command) == 0:
  print 'success!'
  sys.exit(0)
else:
  oops('Rsync error.  Backup FAILED.', -5)
  

If I have the gift of prophecy and can fathom all mysteries and all knowledge, and if I have a faith that can move mountains, but have not love, I am nothing.   1 Corinthians 13:2

Offline

#2 2005-12-23 01:43:26

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: Sweet backup script with python

rsync for backups to a usb device?
I would recommend using tar instead.
Tar can do incremental backups as well as full backups.

If you only want to preserve the current 'snapshot' then a full backup/overwrite would likely still be better than just an rsync across..

added bonus - you can use compression (gz or bz2)

other than your choice of backup utilities.. looks good.
smile

I am sure many will find it useful.


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#3 2005-12-23 01:45:44

punkrockguy318
Member
From: New Jersey
Registered: 2004-02-15
Posts: 711
Website

Re: Sweet backup script with python

What options would you suggest for tar so that the same stuff isn't getting rebacked up every time you backup?  so only newly modified stuff gets backed up, and permissions and everything are kept?

basically, what's the tar equivilent of rsync -av --delete


If I have the gift of prophecy and can fathom all mysteries and all knowledge, and if I have a faith that can move mountains, but have not love, I am nothing.   1 Corinthians 13:2

Offline

#4 2005-12-23 01:49:11

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: Sweet backup script with python

http://www.linux-backup.net

there are a few links on the left that I find useful references..
"Full/Incremental Backups" and "Example Backup Commands"

eg:

Simplified  tar --newer  Incremental Backup example
    * LastBackupTime = `cat /Backup_Dir/Last.txt`
    * tar zcvf --newer $LastBackupTime /Backup_Dir/Date.x.tgz $DIRS
    * echo "Date" > /Backup_Dir/Last.txt 

"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#5 2005-12-23 02:30:26

punkrockguy318
Member
From: New Jersey
Registered: 2004-02-15
Posts: 711
Website

Re: Sweet backup script with python

cactus wrote:

http://www.linux-backup.net

there are a few links on the left that I find useful references..
"Full/Incremental Backups" and "Example Backup Commands"

eg:

Simplified  tar --newer  Incremental Backup example
    * LastBackupTime = `cat /Backup_Dir/Last.txt`
    * tar zcvf --newer $LastBackupTime /Backup_Dir/Date.x.tgz $DIRS
    * echo "Date" > /Backup_Dir/Last.txt 

using this example you would only have a couple files in each backup: only the files modified since the last backup.


If I have the gift of prophecy and can fathom all mysteries and all knowledge, and if I have a faith that can move mountains, but have not love, I am nothing.   1 Corinthians 13:2

Offline

#6 2005-12-23 04:33:30

dk
Member
Registered: 2004-04-20
Posts: 106

Re: Sweet backup script with python

I've been using a similar script:
http://applications.linux.com/article.p … 240&tid=13

I like the exclude feature.

Offline

Board footer

Powered by FluxBB