-
Notifications
You must be signed in to change notification settings - Fork 1
Create Backup Script
Easy creation of extensible modules to create your custom backup script
Please see example included in autobackup-dcm 📦
🔥 To developing:
-
The script name should be as describe in name convention
backupType_backup.pyfor example:mysql_backup.py -
The script name should be as describe in name convention
backupType_config.jsonfor example:mysql_config.json2.1. Put basic JSON structure as described in setup wiki and put your custom configuration required by your script inside
customJSON objectRemember that this configuration file is an array of JSON Objects
appsto backup.All applications defined in this array must have the same structure
[ { "cfg": { "local_backup_dir": "../backup_test_files/jom_backups/", "remote_backup_dir": "google_folder_id", "google_credentials_name": "jom_google_credentials.json", "app_name": "jom", "google_authorized": false }, "bk": { "last_week": 0, "last_year": 0, "last_day": 0, "last_month": 0 }, "rotate": { "local": { "monthly": 12, "yearly": 2, "weekly": 4, "daily": 7 }, "remote": { "monthly": 12, "yearly": 3, "weekly": 4, "daily": 7 }, "include_list": [], "exclude_list": [], "dry_run": false, "ionice": "idle", }, "custom": { "custom_filed1":"custom_value1", "custom_filed2":"custom_value3", } } ]
-
First, import libraries required to code your script, then import
GenericBackupCM# Modules included in our package. from core.generic_backup import GenericBackupCM
-
Now define things required as logging and path to config file (created in 2.):
#logging log = logging.getLogger('dacopancm.YourScriptName') # replace YourScriptName to your script name for example (dacopancm.mysql) # constants CONFIG_FILE = '../config/YourScriptName_config.json' # replace YourScriptName to your script name for example (mysql_config.json)
-
Create a
classthat extends fromGenericBackupCMclass MysqlBackupCM(GenericBackupCM): def __init__(self): super().__init__(log, CONFIG_FILE)
The
GenericBackupCMread and parse config file, iterate over array of apps to backup defined in config file and call to functiondo_backupthat must be implemented in your script. This function receive 2 parameters:appthat contains all configuration defined in JSON object and the parameterbackup_typeif it isDAILY, WEEKLY, MONTHLY or YEARLY. -
Define function
do_backupin your script
Indo_backupfunction you should insert your logic to create backup file and then callupload_backupfunction defined in parent class, and then returnTrueorFalse
def do_backup(self, app, backup_type):
# backup_file name must have the tiemstamp ex. jom_2016-03-28_09-17_yearly
filestamp = time.strftime('%Y-%m-%d_%H-%M')
backup_file = '{}{}_{}_{}.{}'.format(app['cfg']['local_backup_dir'], app['cfg']['app_name'], filestamp,
backup_type,
'gz')
backup_created = # here your logic to create backup
if backup_created:
return self.upload_backup(app, backup_file)- That's all folks! 👽