-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapsnapshot.sh
57 lines (47 loc) · 1.96 KB
/
snapsnapshot.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
#!/bin/bash
##############################################################
# Script description -scubamuc- https://scubamuc.github.io/ #
# Nextcloud-snap backup with Snap snapshot
##############################################################
## create target directory "sudo mkdir /media/SNAPBACKUP"
## snapshot rotation 30 days
## create crontab as root for automation
## 0 1 * * 0 su $USER /home/$USER/bin/snapsnapshot.sh
##############################################################
# VARIABLES #
##############################################################
SNAPNAME="nextcloud"
TARGET="/media/SNAPBACKUP" ## target directory
LOG="$TARGET/snapbackup-nc.log" ## logfile
SOURCE="/var/lib/snapd/snapshots" ## source directory
RETENTION="30" ## retention in days
##############################################################
# FUNCTIONS #
##############################################################
## Timestamp for Log ##
timestamp()
{
date +"%Y-%m-%d %T"
}
##############################################################
# SCRIPT #
##############################################################
## must be root, enter sudo password for manual snapshot
## sudo pwd
## start log
echo "********************************************************" >> "$LOG" ; ## log seperator
echo "$(timestamp) -- Snapbackup "$SNAPNAME" Start" >> "$LOG" ; ## start log
## optional stop snap for snapshot
sudo snap stop "$SNAPNAME" ;
## create snap snapshot
sudo snap save --abs-time "$SNAPNAME" ;
## optional if stopped restart snap after snapshot
sudo snap start "$SNAPNAME" ;
## find and move snapshot to $TARGET
sudo find "$SOURCE"/ -name "*.zip" -exec mv '{}' "$TARGET"/ \ ; # find and move
## find old snapshots and delete snapshots older than $RETENTION days
sudo find "$TARGET"/ -name "*.zip" -mtime +"$RETENTION" -exec rm -f {} \ ; # find and delete
## end log
echo "$(timestamp) -- Snapbackup "$SNAPNAME" End " >> "$LOG" ; ## end log
echo "" >> "$LOG" ; ## log linefeed
exit