mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
[enh] Implement compression and add ignore_apps argument to backup_backup
This commit is contained in:
parent
bd5d74ca28
commit
c3cadc1d3c
3 changed files with 77 additions and 21 deletions
|
@ -563,6 +563,10 @@ backup:
|
||||||
api: POST /backup
|
api: POST /backup
|
||||||
configuration:
|
configuration:
|
||||||
lock: false
|
lock: false
|
||||||
|
arguments:
|
||||||
|
--ignore-apps:
|
||||||
|
help: Do not backup apps
|
||||||
|
action: store_true
|
||||||
|
|
||||||
### backup_restore()
|
### backup_restore()
|
||||||
restore:
|
restore:
|
||||||
|
|
75
backup.py
75
backup.py
|
@ -29,43 +29,92 @@ import json
|
||||||
import errno
|
import errno
|
||||||
import time
|
import time
|
||||||
import shutil
|
import shutil
|
||||||
|
import tarfile
|
||||||
|
|
||||||
from moulinette.core import MoulinetteError
|
from moulinette.core import MoulinetteError
|
||||||
|
from moulinette.utils.log import getActionLogger
|
||||||
|
|
||||||
def backup_backup():
|
backup_path = '/home/yunohost.backup'
|
||||||
|
archives_path = '%s/archives' % backup_path
|
||||||
|
|
||||||
|
logger = getActionLogger('yunohost.backup')
|
||||||
|
|
||||||
|
|
||||||
|
def backup_backup(ignore_apps=False):
|
||||||
"""
|
"""
|
||||||
Create an encrypted backup tarball
|
Create an encrypted backup tarball
|
||||||
|
|
||||||
|
Keyword arguments:
|
||||||
|
ignore_apps -- Do not backup apps
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from yunohost.hook import hook_add
|
from yunohost.hook import hook_add
|
||||||
from yunohost.hook import hook_callback
|
from yunohost.hook import hook_callback
|
||||||
|
|
||||||
backup_dirname = int(time.time())
|
timestamp = int(time.time())
|
||||||
backup_dir = "/home/yunohost.backup/tmp/%s" % backup_dirname
|
tmp_dir = "%s/tmp/%s" % (backup_path, timestamp)
|
||||||
|
|
||||||
# Create directory
|
# Create temporary directory
|
||||||
try: os.listdir(backup_dir)
|
if os.path.isdir(tmp_dir):
|
||||||
except OSError: os.makedirs(backup_dir)
|
logger.warning("temporary directory for backup '%s' already exists", tmp_dir)
|
||||||
os.system('chmod 755 /home/yunohost.backup /home/yunohost.backup/tmp')
|
os.system('rm -rf %s' % tmp_dir)
|
||||||
os.system('chown -hR admin: %s' % backup_dir)
|
try:
|
||||||
|
os.mkdir(tmp_dir, 0750)
|
||||||
|
except OSError:
|
||||||
|
# Create temporary directory recursively
|
||||||
|
os.makedirs(tmp_dir, 0750)
|
||||||
|
os.system('chown -hR admin: %s' % backup_path)
|
||||||
|
else:
|
||||||
|
os.system('chown -hR admin: %s' % tmp_dir)
|
||||||
|
|
||||||
# Add app's backup hooks
|
# Add app's backup hooks
|
||||||
|
if not ignore_apps:
|
||||||
try:
|
try:
|
||||||
for app_id in os.listdir('/etc/yunohost/apps'):
|
for app_id in os.listdir('/etc/yunohost/apps'):
|
||||||
hook = '/etc/yunohost/apps/'+ app_id +'/scripts/backup'
|
hook = '/etc/yunohost/apps/'+ app_id +'/scripts/backup'
|
||||||
if os.path.isfile(hook):
|
if os.path.isfile(hook):
|
||||||
hook_add(app_id, hook)
|
hook_add(app_id, hook)
|
||||||
else:
|
else:
|
||||||
|
logger.warning("unable to find app's backup hook '%s'", hook)
|
||||||
msignals.display(m18n.n('unbackup_app', app_id),
|
msignals.display(m18n.n('unbackup_app', app_id),
|
||||||
'warning')
|
'warning')
|
||||||
|
except IOError as e:
|
||||||
|
logger.info("unable to add app's backup hooks: %s", str(e))
|
||||||
|
|
||||||
except IOError:
|
# Run hooks
|
||||||
pass
|
m18n.display(m18n.n('backup_running_hooks'))
|
||||||
|
hook_callback('backup', [tmp_dir])
|
||||||
|
|
||||||
# Run hook
|
# TODO: Add a backup info file
|
||||||
hook_callback('backup', [backup_dir])
|
|
||||||
|
|
||||||
#TODO: Compress & encrypt
|
# Create the archive
|
||||||
|
m18n.display(m18n.n('backup_creating_archive'))
|
||||||
|
archive_file = "%s/%s.tar.gz" % (archives_path, timestamp)
|
||||||
|
try:
|
||||||
|
tar = tarfile.open(archive_file, "w:gz")
|
||||||
|
except:
|
||||||
|
tar = None
|
||||||
|
|
||||||
|
# Create the archives directory and retry
|
||||||
|
if not os.path.isdir(archives_path):
|
||||||
|
os.mkdir(archives_path, 0750)
|
||||||
|
try:
|
||||||
|
tar = tarfile.open(archive_file, "w:gz")
|
||||||
|
except:
|
||||||
|
logger.exception("unable to open the archive '%s' for writing " \
|
||||||
|
"after creating directory '%s'",
|
||||||
|
archive_file, archive_dir)
|
||||||
|
tar = None
|
||||||
|
else:
|
||||||
|
logger.exception("unable to open the archive '%s' for writing",
|
||||||
|
archive_file)
|
||||||
|
if tar is None:
|
||||||
|
raise MoulinetteError(errno.EIO, m18n.n('backup_archive_open_failed'))
|
||||||
|
tar.add(tmp_dir, arcname='')
|
||||||
|
tar.close()
|
||||||
|
|
||||||
|
# Remove temporary directory
|
||||||
|
os.system('rm -rf %s' % tmp_dir)
|
||||||
|
|
||||||
msignals.display(m18n.n('backup_complete'), 'success')
|
msignals.display(m18n.n('backup_complete'), 'success')
|
||||||
|
|
||||||
|
|
|
@ -128,6 +128,9 @@
|
||||||
"packages_upgrade_failed" : "Unable to upgrade all packages",
|
"packages_upgrade_failed" : "Unable to upgrade all packages",
|
||||||
"system_upgraded" : "System successfully upgraded",
|
"system_upgraded" : "System successfully upgraded",
|
||||||
|
|
||||||
|
"backup_running_hooks" : "Running backup hooks...",
|
||||||
|
"backup_creating_archive" : "Creating the backup archive...",
|
||||||
|
"backup_archive_open_failed" : "Unable to open the backup archive",
|
||||||
"backup_complete" : "Backup complete",
|
"backup_complete" : "Backup complete",
|
||||||
"invalid_restore_package" : "Invalid restore package",
|
"invalid_restore_package" : "Invalid restore package",
|
||||||
"restore_complete" : "Restore complete",
|
"restore_complete" : "Restore complete",
|
||||||
|
|
Loading…
Add table
Reference in a new issue