-
Notifications
You must be signed in to change notification settings - Fork 15
/
restore.j2
54 lines (42 loc) · 2.11 KB
/
restore.j2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
# {{ ansible_managed }}
#
# Duply never run any script on restore, as it could have potentially destructive effetcs.
#
# You must run duply profile_name restore /path/to/destination, check your data, and
# then load it into your database or directory.
#
# This script is never runned by Duply, it is only there as an example of which commands you
# should run to import data from your restore directory.
WORKDIR={{ item.work_dir|default(backup_work) }}/{{ item.name }}
{% if item.source.startswith('postgresql://') %}
DBNAME={{ item.source.split('postgresql://')[-1]}}
psql {{ ('-U %s' % backup_postgres_user) if backup_postgres_user else '' }} {{ ('-h %s' % backup_postgres_host) if backup_postgres_host else ''}} {{ ('-p %s' % backup_postgres_port) if backup_postgres_port else ''}} $DBNAME < $WORKDIR/dump
rm -rf $WORKDIR/dump
{% elif item.source.startswith('mysql://') %}
{% if item.source == 'mysql://' %}
# Restore all databases
mysql {{ '-u %s' % backup_mysql_user if backup_mysql_user else '' }} {{ ('-h %s' % backup_mysql_host) if backup_mysql_host else ''}} {{ '-p%s' % (backup_mysql_pass|quote) if backup_mysql_pass else '' }} < ${WORKDIR}/dump
{% else %}
# Restore the passed database
DBNAME={{ item.source.split('mysql://')[-1]}}
mysql {{ '-u %s' % backup_mysql_user if backup_mysql_user else '' }} {{ ('-h %s' % backup_mysql_host) if backup_mysql_host else ''}} {{ '-p%s' % (backup_mysql_pass|quote) if backup_mysql_pass else '' }} $DBNAME < ${WORKDIR}/dump
rm -rf $WORKDIR/dump
{% endif %}
{% elif item.source.startswith('mongo://') %}
mongorestore --drop $WORKDIR/dump
rm -rf $WORKDIR/dump
{% elif item.source.startswith('redis://') %}
if [$(cat /etc/redis/redis.conf |grep 'appendonly '|cut -d' ' -f2) != "no"]
then
echo "Does not work with AOF mode enabled"
exit 1
fi
systemctl start redis-server.service
REDIS_DB=$(redis-cli --raw config get dir | tail -1)/$(redis-cli --raw config get dbfilename | tail -1)
systemctl stop redis-server.service
mv $REDIS_DB $REDIS_DB.back
mv $WORKDIR/dump $REDIS_DB
chown {{ backup_redis_user }}:{{ backup_redis_group }} $REDIS_DB
systemctl start redis-server.service
{% endif %}