Skip to content
Snippets Groups Projects
Commit fddbea58 authored by efer's avatar efer
Browse files

"main" backup function

parent d53e769e
No related branches found
No related tags found
No related merge requests found
......@@ -150,7 +150,7 @@ fi
# User Specific Variables
#
USER=$(whoami)
echo -n "\nEnter your DTU username [$USER]: "
echo -e "\nEnter your DTU username [$USER]: "
read ANS
case $ANS in
"")
......@@ -167,7 +167,7 @@ if [ ! -d $FILE_DIR ]; then
mkdir -p $FILE_DIR/old &>/dev/null
fi
echo -e "\nLooking for backup script..."
echo "Looking for backup script..."
if [ -x $BIN_DIR/$SCRIPTNAME-$RUSER ]; then
mv $BIN_DIR/$SCRIPTNAME-$RUSER $FILE_DIR/old/
fi
......@@ -409,7 +409,7 @@ LOGNAMESHORT="logfile_short.txt"
SNAPDIR=".zfs/snapshot"
# Rclone command
BACKUP_CMD="rclone sync / compbackup: --filter-from=$CONF_DIR/filter --delete-excluded --delete-before --skip-links --log-file=$LOG --log-level=INFO"
BACKUP_CMD="rclone sync / compbackup: --filter-from=\$CONF_DIR/filter --delete-excluded --delete-before --skip-links --log-file=\$LOG --log-level=INFO"
# OS version
if [ $HOST_OS = 'Darwin' ]; then
......@@ -423,11 +423,19 @@ cat > $BIN_DIR/$SCRIPTNAME << EOF
#!/bin/bash
version=$VERSION
TARGETNODE=$TARGETNODE
LOG=$LOG
LOGSHORT=$LOGSHORT
CONF_DIR=$CONF_DIR
SSHKEY=$SSHKEY
FILTER=$FILTER
OS_VER=$OS_VER
# macOS specific fix
export LC_CTYPE=
# Check connection
nc -z $TARGETNODE 22 &>/dev/null
# Connection check
nc -z \$TARGETNODE 22 &>/dev/null
if [ \$? -ne 0 ]; then
echo "No connection..."
exit 1
......@@ -435,16 +443,25 @@ fi
# Log check
logcheck(){
lsize=\$(du $LOG|awk '{print \$1}')
lsize=\$(du \$LOG|awk '{print \$1}')
if [ \$lsize -gt 8192 ]; then
gzip -c $LOG > $LOG.\$(date +%Y-%m-%d).gz
echo > $LOG
gzip -c \$LOG > \$LOG.\$(date +%Y-%m-%d).gz
echo > \$LOG
fi
}
# Log update
logupdate(){
echo -e "Serial number: $SERIALNO" > \$LOGSHORT
echo -e "$SCRIPTNAME version: \$version" >> \$LOGSHORT
echo -e "OS version: \$OS_VER" >> \$LOGSHORT
tail -n 6 \$LOG >> \$LOGSHORT
rsync -a -e "ssh -i \$SSHKEY" \$LOGSHORT $RUSER@\$TARGETNODE:$RHOME/$SCRIPTNAME/$HOST/$LOGNAMESHORT
}
# Get snapshots from backup server
snapshots(){
fileList=\$(ssh -i $SSHKEY $RUSER@$TARGETNODE ls -1 $SNAPDIR/ | grep pyznap)
fileList=\$(ssh -i \$SSHKEY $RUSER@\$TARGETNODE ls -1 $SNAPDIR/ | grep pyznap)
if [ -z "\$fileList" ]; then
echo -e "\e[31mNo files found or failed to connect.\e[0m"
......@@ -490,50 +507,33 @@ snapshots(){
echo -e "Next run these commands:\n"
echo -e "$G_ source /tmp/rclone_snapshot\n rclone ls compbackup:\n$_N"
echo -e "Example of copying a folder back:\n"
echo -e "$G_ rclone copy compbackup:${RHOME#?}/Myproject ~/Myproject_restore $_N"
echo -e "$G_ rclone copy compbackup:${RHOME#?}/Myproject ~/Myproject_restore\n $_N"
}
# Use snapshot feature
while [ \$# == 1 ]; do
case \$1 in
-r|restore)
snapshots
exit 0
;;
*)
echo "Unknown option \$1"
exit 1
;;
esac
done
if [ -f $LOG ]; then
logcheck
fi
# Check rclone filter
FILTER=$FILTER
defaultfilter() {
curl -s \$FILTER|grep -q $SCRIPTNAME
if [ \$? -eq 0 ]; then
# Check for default & user filter
if [ ! -f $CONF_DIR/filter.orig ]; then
curl -s \$FILTER|sed s/user/$RUSER/g > $CONF_DIR/filter.orig
if [ ! -f $CONF_DIR/filter ]; then
cp $CONF_DIR/filter.orig $CONF_DIR/filter
if [ ! -f \$CONF_DIR/filter.orig ]; then
curl -s \$FILTER|sed s/user/$RUSER/g > \$CONF_DIR/filter.orig
if [ ! -f \$CONF_DIR/filter ]; then
cp \$CONF_DIR/filter.orig \$CONF_DIR/filter
fi
elif [ ! -f $CONF_DIR/filter ]; then
cp $CONF_DIR/filter.orig $CONF_DIR/filter
elif [ ! -f \$CONF_DIR/filter ]; then
cp \$CONF_DIR/filter.orig \$CONF_DIR/filter
else
# Check for updated default filter
curl -s \$FILTER|sed s/user/$RUSER/g|diff $CONF_DIR/filter.orig - &>/dev/null
curl -s \$FILTER|sed s/user/$RUSER/g|diff \$CONF_DIR/filter.orig - &>/dev/null
if [ \$? -ne 0 ]; then
diff $CONF_DIR/filter $CONF_DIR/filter.orig &>/dev/null
diff \$CONF_DIR/filter \$CONF_DIR/filter.orig &>/dev/null
if [ \$? -ne 0 ]; then
curl -s \$FILTER|sed s/user/$RUSER/g > $CONF_DIR/filter.orig
curl -s \$FILTER|sed s/user/$RUSER/g > \$CONF_DIR/filter.orig
else
curl -s \$FILTER|sed s/user/$RUSER/g > $CONF_DIR/filter.orig
cp $CONF_DIR/filter.orig $CONF_DIR/filter
curl -s \$FILTER|sed s/user/$RUSER/g > \$CONF_DIR/filter.orig
cp \$CONF_DIR/filter.orig \$CONF_DIR/filter
fi
fi
fi
......@@ -541,10 +541,13 @@ else
echo "Cannot download filter template from server..."
fi
}
defaultfilter
# Backup command
run_backup(){
if [ -f \$LOG ]; then
logcheck
fi
$BACKUP_CMD
if [ \$? -eq 0 ]; then
......@@ -555,15 +558,26 @@ else
$NOTIFY1
fi
}
run_backup
# Log update
echo -e "Serial number: $SERIALNO" > $LOGSHORT
echo -e "$SCRIPTNAME version: \$version" >> $LOGSHORT
export OS_VER=$OS_VER
echo -e "OS version: \$OS_VER" >> $LOGSHORT
tail -n 6 $LOG >> $LOGSHORT
rsync -a -e "ssh -i $SSHKEY" $LOGSHORT $RUSER@$TARGETNODE:$RHOME/$SCRIPTNAME/$HOST/$LOGNAMESHORT
# Main
if [ \$# -eq 0 ]; then
defaultfilter
run_backup
logupdate
else
while [ \$# -eq 1 ]; do
case \$1 in
-r|restore)
snapshots
exit 0
;;
*)
echo "Unknown option \$1"
exit 1
;;
esac
done
fi
EOF
chmod +x $BIN_DIR/$SCRIPTNAME
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment