-
Notifications
You must be signed in to change notification settings - Fork 0
/
LXC-ContainerBackup.sh
61 lines (51 loc) · 2.09 KB
/
LXC-ContainerBackup.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
#!/bin/bash
##############################################################
# Script description -scubamuc- https://scubamuc.github.io/ #
# LXC container incremental backup image to target directory #
##############################################################
## create target directory
## create crontab for automation
## crontab execution hourly/daily/weekly
## retention in days
##############################################################
# VARIABLES #
##############################################################
CONTAINERNAME="NEXTCLOUD" ## LXC Containername
DATE="`date +"%Y%m%d"`"
IMAGENAME="$CONTAINERNAME-$DATE"
TARGET="/media/EXT-HDD/LXC-BACKUP" ## Target directory
LOG="$TARGET/$CONTAINERNAME--LXC-BACKUP.log" ## Log file
RETENTION="30" ## Retention in days
##############################################################
# FUNCTIONS #
##############################################################
## Timestamp for Log ##
timestamp()
{
date +"%Y-%m-%d %T"
}
##############################################################
# SCRIPT #
##############################################################
## start log
echo "***********************************************************" >> "$LOG" ; ## log seperator
echo "$(timestamp) -- LXC-Backup $IMAGENAME Start" >> "$LOG" ; ## start log
## Container snapshot
lxc snapshot $CONTAINERNAME/$CONTAINERNAME --reuse ;
## publish Container with Alias
lxc publish $CONTAINERNAME/$CONTAINERNAME --alias $CONTAINERNAME ;
## create image from snapshot
lxc image export $CONTAINERNAME $TARGET/$IMAGENAME ;
## delete Image on Host
lxc image delete $CONTAINERNAME ;
## find old Images and delete older than $RETENTION days
find $TARGET/ -name "*.tar.gz" -mtime +$RETENTION -exec rm -f {} \; # find and delete
## end log
echo "$(timestamp) -- LXC-Backup $IMAGENAME End " >> "$LOG" ; ## end log
echo "" >> "$LOG" ; ## log linefeed
## Restore Image
## To restore and create a container from it, you can then do:
## lxc image import $TARGET/$IMAGENAME.tar.gz --alias $IMAGENAME
## lxc launch $IMAGENAME $CONTAINERNAME
## lxc image delete $IMAGENAME
exit