summaryrefslogtreecommitdiffstats
path: root/SOURCE/CORE/core-backup.sh
blob: 7a0b6d119172cb0e4a50e9c560755554f60d6f11 (plain)
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
backup_data() {
	BACKUPDIR="$1"
    echo "backing up data in $BACKUPDIR"


    if [ -d $BACKUPDIR ]; then
    	if [ $(/bin/ls -A $BACKUPDIR) ]; then
	    	echo "$BACKUPDIR is not empty. Cannot continue"
	    	exit
	    else
	    	echo "$BACKUPDIR is ok. Continuing!"
	    fi
	else
		# BACKUPDIR doesn't exists
		echo "$BACKUPDIR doesn't exists"
		read -r -p "Do you want me to create it for you? (y/N) " ANSWER
		case $ANSWER in
			y|Y )
				mkdir -p $BACKUPDIR
				;;
			* )
				echo "No changes made. Exiting"
				exit
				;;
		esac
    fi
    # ok, we have a backup directory
    if [ -r $RCFILE ]; then
    	BCKUP_COMM=$(rsync -avz --progress ${RCFILE}* ${BASEDIR}/ ${BACKUPDIR})
    else
    	BCKUP_COMM=$(rsync -avz --progress ${BASEDIR}/ ${BACKUPDIR})
    fi
    # run the command
    if [ "$BCKUP_COMM" ]; then	
	    echo -e "All files backed up."
	    echo -e "BACKUP directory:\t$BACKUPDIR"
	    tree $BACKUPDIR | $PAGER
	    echo; echo "BACKUP COMPLETED"
	fi
}

backup_restore() {
	BACKUPDIR="$1"
	echo "restoring backup from $BACKUPDIR"
	echo "This will overwrite all your notes and configurations with the backup."
	read -r -p "Do you want to continue? (y/N) " ANSWER
	case $ANSWER in
		y|Y )
			# restoring rc file
			BACKUPRC=$(basename $RCFILE)
			if [ -r ${BACKUPDIR}/${BACKUPRC} ]; then
				if [ -r ${RCFILE} ]; then
					echo "Backing up current '${RCFILE}'...."
					mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M)
				fi
				cp --verbose ${BACKUPDIR}/${BACKUPRC} $RCFILE
			fi
			# restoring notes directory
			if [ -d $BACKUPDIR/notes ]; then
				if [ $(/bin/ls -A $NOTESDIR) ]; then
					rm --verbose $NOTESDIR/*
				fi
				cp -r --verbose $BACKUPDIR/notes $BASEDIR
			fi
			# restoring database
			BACKUPDB=$(basename $DB)
			if [ -f ${BACKUPDIR}/${BACKUPDB} ]; then
				if [ -r ${DB} ]; then
					echo "Backing up current '${DB}'...."
					mv -f ${DB} ${DB}.$(date +%Y%m%d_%H%M)
				fi
				cp --verbose ${BACKUPDIR}/${BACKUPDB} $DB
			fi
			# restoring git repo subdirectory
			if [ -d $BACKUPDIR/.git ]; then
				if [ /bin/ls -A ${BASEDIR}/.git ]; then
					rm -rf ${BASEDIR}/.git
				fi
				cp -r --verbose ${BACKUPDIR}/.git ${BASEDIR}/
			fi
			;;
		* )
			echo "No changes made. Exiting"
			exit
			;;
	esac
}