Guest User

Untitled

a guest
Oct 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. This script creates a timestamped database backup,
  4. and cleans backups older than a set number of dates
  5.  
  6. """
  7.  
  8. from __future__ import print_function
  9. from __future__ import unicode_literals
  10.  
  11. import argparse
  12. import sqlite3
  13. import shutil
  14. import time
  15. import os
  16.  
  17. DESCRIPTION = """
  18. Create a timestamped SQLite database backup, and
  19. clean backups older than a defined number of days
  20. """
  21.  
  22. # How old a file needs to be in order
  23. # to be considered for being removed
  24. NO_OF_DAYS = 7
  25.  
  26. def sqlite3_backup(dbfile, backupdir):
  27. """Create timestamped database copy"""
  28.  
  29. if not os.path.isdir(backupdir):
  30. raise Exception("Backup directory does not exist: {}".format(backupdir))
  31.  
  32. backup_file = os.path.join(backupdir, os.path.basename(dbfile) +
  33. time.strftime("-%Y%m%d-%H%M%S"))
  34.  
  35. connection = sqlite3.connect(dbfile)
  36. cursor = connection.cursor()
  37.  
  38. # Lock database before making a backup
  39. cursor.execute('begin immediate')
  40. # Make new backup file
  41. shutil.copyfile(dbfile, backup_file)
  42. print ("nCreating {}...".format(backup_file))
  43. # Unlock database
  44. connection.rollback()
  45.  
  46. def clean_data(backup_dir):
  47. """Delete files older than NO_OF_DAYS days"""
  48.  
  49. print ("n------------------------------")
  50. print ("Cleaning up old backups")
  51.  
  52. for filename in os.listdir(backup_dir):
  53. backup_file = os.path.join(backup_dir, filename)
  54. if os.stat(backup_file).st_ctime < (time.time() - NO_OF_DAYS * 86400):
  55. if os.path.isfile(backup_file):
  56. os.remove(backup_file)
  57. print ("Deleting {}...".format(ibackup_file))
  58.  
  59. def get_arguments():
  60. """Parse the commandline arguments from the user"""
  61.  
  62. parser = argparse.ArgumentParser(description=DESCRIPTION)
  63. parser.add_argument('db_file',
  64. help='the database file that needs backed up')
  65. parser.add_argument('backup_dir',
  66. help='the directory where the backup'
  67. 'file should be saved')
  68. return parser.parse_args()
  69.  
  70. if __name__ == "__main__":
  71. args = get_arguments()
  72. sqlite3_backup(args.db_file, args.backup_dir)
  73. clean_data(args.backup_dir)
  74.  
  75. print ("nBackup update has been successful.")
  76.  
  77. if os.stat(backup_file).st_ctime < (time.time() - NO_OF_DAYS * 86400):
  78. if os.path.isfile(backup_file):
  79.  
  80. if os.path.isfile(backup_file):
  81. if os.stat(backup_file).st_ctime < (time.time() - NO_OF_DAYS * 86400):
  82.  
  83. def get_arguments():
  84. """Parse the commandline arguments from the user"""
  85. parser = argparse.ArgumentParser(description=__doc__)
Add Comment
Please, Sign In to add comment