aboutsummaryrefslogtreecommitdiffstats
path: root/doc/admin/hub_snapshots.md
blob: 7e4ba95b2c3345709b2107697a7945eaa9438256 (plain) (blame)
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
120
121
122
123
124
125
126
127
# Hub Snapshot Tools

Hubzilla developers frequently need to switch between branches that might have 
incompatible database schemas or content. The following two scripts create and 
restore complete snapshots of a Hubzilla instance, including both the hub web 
root and the entire database state. Each script requires a config file called 
`hub-snapshot.conf` residing in the same folder and containing the specific 
directories and database details of your hub.

# Config

The format of the config file is very strict. There must be no spaces between the 
variable name and the value. Replace only the content inside the quotes with your 
configuration. Save this file as `hub-snapshot.conf` alongside the scripts.

    # Location of hub root. Typically this is the location of the Hubzilla repo clone.
    HUBROOT="/var/www/"
    # MySQL database name
    DBNAME="hubzilla"
    # MySQL database user
    DBUSER="hubzilla"
    # MySQL database password
    DBPWD="akeufajeuwfb"
    # The target snapshot folder where the git repo will be initialized
    SNAPSHOTROOT="/root/snapshots/hubzilla/"
    
# Snapshot

Example usage:

    sh hub-snapshot.sh my-hub.conf "Commit message for the snapshot" 

**hub-snapshot.sh**:

    #!/bin/bash
    
    if ! [ -f "$1" ]; then
    	echo "$1 is not a valid file. Aborting..."
    	exit 1
    fi
    source "$1"
    #echo "$DBNAME"
    #echo "$DBUSER"
    #echo "$DBPWD"
    #echo "$HUBROOT"
    #echo "$SNAPSHOTROOT"
    MESSAGE="snapshot: $2"
    
    if [ "$DBPWD" == "" -o "$SNAPSHOTROOT" == "" -o "$DBNAME" == "" -o "$DBUSER" == "" -o "$HUBROOT" == "" ]; then
    	echo "Required variable is not set. Aborting..."
    	exit 1
    fi
    
    if [ ! -d "$SNAPSHOTROOT"/db/ ]; then
    	mkdir -p "$SNAPSHOTROOT"/db/
    fi
    if [ ! -d "$SNAPSHOTROOT"/www/ ]; then
    	mkdir -p "$SNAPSHOTROOT"/www/
    fi
    
    if [ ! -d "$SNAPSHOTROOT"/www/ ] || [ ! -d "$SNAPSHOTROOT"/db/ ]; then
    	echo "Error creating snapshot directories. Aborting..."
    	exit 1
    fi
    
    echo "Export database..."
    mysqldump -u "$DBUSER" -p"$DBPWD" "$DBNAME" > "$SNAPSHOTROOT"/db/"$DBNAME".sql
    echo "Copy hub root files..."
    rsync -va --delete --exclude=.git* "$HUBROOT"/ "$SNAPSHOTROOT"/www/
    
    cd "$SNAPSHOTROOT"
    
    if [ ! -d ".git" ]; then
    	git init
    fi
    if [ ! -d ".git" ]; then
    	echo "Cannot initialize git repo. Aborting..."
    	exit 1
    fi
    
    git add -A
    echo "Commit hub snapshot..."
    git commit -a -m "$MESSAGE"
    
    exit 0

# Restore

    #!/bin/bash
    # Restore hub to a previous state. Input hub config and commit hash
    
    if ! [ -f "$1" ]; then
            echo "$1 is not a valid file. Aborting..."
            exit 1
    fi
    source "$1"
    COMMIT=$2
    
    if [ "$DBPWD" == "" -o "$SNAPSHOTROOT" == "" -o "$DBNAME" == "" -o "$DBUSER" == "" -o "$HUBROOT" == "" ]; then
            echo "Required variable is not set. Aborting..."
            exit 1
    fi
    RESTOREDIR="$(mktemp -d)/"
    
    if [ ! -d "$RESTOREDIR" ]; then
    	echo "Cannot create restore directory. Aborting..."
    	exit 1
    fi
    echo "Cloning the snapshot repo..."
    git clone "$SNAPSHOTROOT" "$RESTOREDIR"
    cd "$RESTOREDIR"
    echo "Checkout requested snapshot..."
    git checkout "$COMMIT"
    echo "Restore hub root files..."
    rsync -a --delete --exclude=.git* "$RESTOREDIR"/www/ "$HUBROOT"/
    echo "Restore hub database..."
    mysql -u "$DBUSER" -p"$DBPWD" "$DBNAME" < "$RESTOREDIR"/db/"$DBNAME".sql
    
    chown -R www-data:www-data "$HUBROOT"/{store,extend,addon,.htlog,.htconfig.php}
    
    echo "Restored hub to snapshot $COMMIT"
    echo "Removing temporary files..."
    
    rm -rf "$RESTOREDIR"
    
    exit 0