forked from experimental-berlin/muzhack-backup-rethinkdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
94 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
end_of_line = lf | ||
insert_final_newline = true | ||
max_line_length = 79 | ||
|
||
[*.py] | ||
indent_size = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,18 @@ | ||
FROM debian | ||
MAINTAINER "MuzHack" <[email protected]> | ||
|
||
RUN apt-get update && apt-get install -y cron python3 python3-pip lsb-release wget | ||
RUN apt-get update && apt-get install -y python3 python3-pip lsb-release wget | ||
RUN echo "deb http://download.rethinkdb.com/apt `lsb_release -cs` main" | tee /etc/apt/sources.list.d/rethinkdb.list | ||
RUN wget -qO- https://download.rethinkdb.com/apt/pubkey.gpg | apt-key add - | ||
RUN apt-get update && apt-get install -y rethinkdb | ||
RUN apt-get clean -y && apt-get autoclean -y && apt-get autoremove -y && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* | ||
|
||
WORKDIR /app | ||
COPY ./crontab /etc/cron.d/ | ||
COPY ./requirements.txt /app/ | ||
RUN mkdir -p /app/rethinkdb | ||
COPY ./rethinkdb/backup.py /app/rethinkdb/ | ||
|
||
COPY ./requirements.txt /app/ | ||
RUN pip3 install -r requirements.txt | ||
RUN rm -rf requirements.txt | ||
|
||
CMD python3 cron.py | ||
COPY ./schedule-rethinkdb-backup.py /app/ | ||
|
||
CMD python3 schedule-rethinkdb-backup.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/usr/bin/env python3 | ||
"""Script to schedule RethinkDB backup.""" | ||
import asyncio | ||
import subprocess | ||
from datetime import datetime, date, timedelta | ||
import sys | ||
import logging | ||
import contextlib | ||
|
||
|
||
def _configure_logging(): | ||
logging.getLogger().setLevel(logging.WARNING) | ||
logger = logging.getLogger('app') | ||
logger.setLevel(logging.DEBUG) | ||
|
||
ch = logging.StreamHandler() | ||
formatter = logging.Formatter( | ||
'%(asctime)s - %(name)s - %(levelname)s - %(message)s') | ||
ch.setFormatter(formatter) | ||
logger.addHandler(ch) | ||
|
||
return logger | ||
|
||
|
||
_logger = _configure_logging() | ||
|
||
|
||
def _schedule_backup(loop): | ||
hour = 18 | ||
|
||
now = datetime.now() | ||
today = date.today() | ||
desired_time = datetime(today.year, today.month, today.day, hour=hour) | ||
if now.hour >= hour: | ||
_logger.debug( | ||
'Delaying until {} next day since we\'re already at {}'.format( | ||
hour, now.hour)) | ||
desired_time = desired_time + timedelta(days=1) | ||
else: | ||
_logger.debug('Delaying until {} same day'.format(hour)) | ||
desired_seconds = (desired_time - now).seconds | ||
|
||
_logger.debug( | ||
'Delaying for {} second(s) before backup'.format(desired_seconds)) | ||
loop.call_later(desired_seconds, _backup, loop) | ||
|
||
|
||
def _backup(loop): | ||
"""Perform actual backup.""" | ||
now = datetime.now() | ||
_logger.info('Backing up at {}...'.format( | ||
now.strftime('%Y-%m-%d %H:%M:%S'))) | ||
_logger.info('Backed up successfully!') | ||
_logger.debug('Scheduling next backup') | ||
_schedule_backup(loop) | ||
|
||
|
||
def _main(): | ||
with contextlib.closing(asyncio.get_event_loop()) as loop: | ||
_schedule_backup(loop) | ||
try: | ||
loop.run_forever() | ||
except KeyboardInterrupt: | ||
_logger.info('Interrupted') | ||
sys.exit(0) | ||
|
||
|
||
if __name__ == '__main__': | ||
_main() |