-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockerbackup.sh
executable file
·119 lines (100 loc) · 3.43 KB
/
dockerbackup.sh
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
##
## Usage : ./docker_backup.sh
## Author : Werner Macho <werner.macho@gmail.com>
##
## Idea taken from : http://blog.stefanxo.com/category/docker/
##
## Exports running docker containers
##
# Delete old backups? Any files older than $daystokeep will be deleted
# Default option : 0
# Recommended option : 1
purgeoldbackups=1
# How many days should we keep the backups
daystokeep="7"
# This directory should have enough space to hold all docker containers
# at the same time
# Subdirectories will be automatically created and deleted after finish
backupdir="/tmp"
echo -e ""
echo -e "\e[1;31mBackup Docker\e[00m"
echo -e "\e[1;33m"$(date)"\e[00m"
echo -e ""
# Timestamp (sortable AND readable)
stamp=`date +"%Y_%m_%d"`
# List all running docker instances
instances=`docker ps -q --no-trunc`
exportdir="$backupdir/docker"
if `test -d ${exportdir}`; then
echo -e "Backupfolder is already there, skipping creation"
else
mkdir ${exportdir}
fi
if [[ $# -eq 0 ]] ; then
# Loop the instances
for container in ${instances}; do
# Get info on each Docker container
instancename=`docker inspect --format='{{.Name}}' ${container} | tr '/' '_'`
imagename=`docker inspect --format='{{.Config.Image}}' ${container} | tr '/' '_'`
# Define our filenames
filename="$stamp-$instancename-$imagename.docker.tar.gz"
exportfile="$exportdir/$filename"
# Feedback
echo -e "backing up \e[1;36m$container\e[00m"
echo -e " container \e[1;36m$instancename\e[00m"
echo -e " from image \e[1;36m$imagename\e[00m"
# Dump and gzip
echo -e " creating \e[0;35m$exportfile\e[00m"
docker export "$container" | gzip -c > "$exportfile"
done;
else
didBackup=false
for container in ${instances}; do
# Get info on each Docker container
instancename=`docker inspect --format='{{.Name}}' ${container} | tr '/' '_'`
imagename=`docker inspect --format='{{.Config.Image}}' ${container} | tr '/' '_'`
if [[ ${instancename} == _$1 ]] ; then
# Define our filenames
filename="$stamp-$instancename-$imagename.docker.tar.gz"
exportfile="$exportdir/$filename"
# Feedback
echo -e "backing up \e[1;36m$container\e[00m"
echo -e " container \e[1;36m$instancename\e[00m"
echo -e " from image \e[1;36m$imagename\e[00m"
# Dump and gzip
echo -e " creating \e[0;35m$exportfile\e[00m"
docker export "$container" | gzip -c > "$exportfile"
didBackup=true
fi
done;
if ! ${didBackup} ; then
echo -e "Given name not found in list of available Containers"
exit 1
fi
fi
# Purge old backups
if [[ "$purgeoldbackups" -eq "1" ]]
then
echo -e " \e[1;35mRemoving old backups...\e[00m"
olderThan=`date -d "$daystokeep days ago" +%s`
curDate=`date +%s`
ls ${exportdir} | while read -r line;
do
fileDate=`stat -L --format %Y $exportdir/${line}`
#echo -e $fileDate
fileAge=`expr $curDate - $fileDate`
#echo -e $fileAge
if [[ ${fileDate} -lt ${olderThan} ]]
then
fileName=`echo ${line}`
echo -e " Removing outdated backup \e[1;31m$fileName\e[00m"
if [[ ${fileName} != "" ]]
then
rm "$fileName"
fi
fi
done;
fi
# We're done
echo -e "\e[1;32mDocker Backup done!\e[00m"