Copie de sécurité » Historique » Version 12
Patrice Nadeau, 2018-08-17 13:52
| 1 | 12 | Patrice Nadeau | # Copie de sécurité |
|---|---|---|---|
| 2 | 1 | Patrice Nadeau | |
| 3 | 3 | Patrice Nadeau | {{toc}} |
| 4 | |||
| 5 | 12 | Patrice Nadeau | ## Création |
| 6 | 1 | Patrice Nadeau | |
| 7 | 12 | Patrice Nadeau | ### Méthode manuelle |
| 8 | 9 | Patrice Nadeau | |
| 9 | 2 | Patrice Nadeau | Remplacer les items suivants : |
| 10 | * *username* : Usager de la base de donnés. |
||
| 11 | * *password* : Mot de passe de la base de donnés. |
||
| 12 | * *database* : Nom de la base de données. |
||
| 13 | * *path* : Emplacement pour recevoir le fichier. |
||
| 14 | |||
| 15 | <pre><code class="bash"> |
||
| 16 | /usr/bin/mysqldump -u username -p password database | gzip > /path/redmine_`date +%y_%m_%d`.gz |
||
| 17 | rsync -a /srv/redmine/files /path/ |
||
| 18 | 1 | Patrice Nadeau | </code></pre> |
| 19 | 2 | Patrice Nadeau | |
| 20 | 12 | Patrice Nadeau | ### Script un peu plus évolué |
| 21 | 2 | Patrice Nadeau | |
| 22 | 12 | Patrice Nadeau | ```bash |
| 23 | 2 | Patrice Nadeau | #!/bin/bash |
| 24 | # |
||
| 25 | # backup_redmine.sh |
||
| 26 | # Backup of a Redmine setup |
||
| 27 | # Last Changes: 2013-02-23 |
||
| 28 | # Maintainer: Patrice Nadeau <pnadeau@patricenadeau.com> |
||
| 29 | |||
| 30 | # TODO Verify the results (folder exist, enough disk pace , etc..) |
||
| 31 | |||
| 32 | ## The only variable needed to be changed |
||
| 33 | # Directory of the Redmine install |
||
| 34 | declare -r RAIL_ROOT='/srv/redmine' |
||
| 35 | # MySQL database |
||
| 36 | declare -r MYSQL_DB='' |
||
| 37 | # MySQL username for the Redemine db |
||
| 38 | declare -r MYSQL_USER='' |
||
| 39 | # MySQL password for the Redemine db |
||
| 40 | declare -r MYSQL_PASSWORD='' |
||
| 41 | # Directory for the backup (must exist and with no space in the name) |
||
| 42 | declare -r DIR='/root' |
||
| 43 | ## end |
||
| 44 | |||
| 45 | # Exit level |
||
| 46 | declare -ir EXIT_OK=0 |
||
| 47 | declare -ir EXIT_WARNING=1 |
||
| 48 | declare -ir EXIT_ERROR=2 |
||
| 49 | |||
| 50 | declare -i STATUS=$EXIT_OK |
||
| 51 | |||
| 52 | # The directory inside the archive |
||
| 53 | declare -r REDMINE='redmine' |
||
| 54 | TMP_DIR=$DIR/$REDMINE |
||
| 55 | |||
| 56 | # This will be used for the archive file |
||
| 57 | declare -r DST=$DIR/redmine_$(date +%Y%m%d_%H%M%S).tar.gz |
||
| 58 | |||
| 59 | # The temporary sql file |
||
| 60 | declare -r TMP_MYSQL=$TMP_DIR/$MYSQL_DB.mysql |
||
| 61 | |||
| 62 | echo "Backup in progress in $DST" |
||
| 63 | |||
| 64 | #### Create the temp directory #### |
||
| 65 | mkdir $TMP_DIR |
||
| 66 | |||
| 67 | #### backup MySQL #### |
||
| 68 | if [ $STATUS -eq $EXIT_OK ] |
||
| 69 | then |
||
| 70 | STEP='Creating MySQL backup' |
||
| 71 | mysqldump --user=$MYSQL_USER --password=$MYSQL_PASSWORD $MYSQL_DB \ |
||
| 72 | > $TMP_MYSQL |
||
| 73 | STATUS=$? |
||
| 74 | fi |
||
| 75 | |||
| 76 | #### backup the Redmine folder #### |
||
| 77 | if [ $STATUS -eq $EXIT_OK ] |
||
| 78 | then |
||
| 79 | STEP='Creating Redmine'"'"' files backup' |
||
| 80 | cp --recursive $RAIL_ROOT $TMP_DIR |
||
| 81 | STATUS=$? |
||
| 82 | fi |
||
| 83 | |||
| 84 | #### create the archive file #### |
||
| 85 | if [ $STATUS -eq $EXIT_OK ] |
||
| 86 | then |
||
| 87 | STEP="Creating archive" |
||
| 88 | tar --create --gzip --file $DST --directory=$DIR $REDMINE |
||
| 89 | STATUS=$? |
||
| 90 | fi |
||
| 91 | |||
| 92 | #### cleanup #### |
||
| 93 | if [ $STATUS -eq $EXIT_OK ] |
||
| 94 | then |
||
| 95 | STEP='Cleaning up' |
||
| 96 | rm --recursive --force $TMP_DIR |
||
| 97 | STATUS=$? |
||
| 98 | fi |
||
| 99 | |||
| 100 | #### exit #### |
||
| 101 | if [ $STATUS -eq $EXIT_OK ] |
||
| 102 | then |
||
| 103 | echo "Backup done" |
||
| 104 | else |
||
| 105 | 1 | Patrice Nadeau | echo "Bakup failed with error code $STATUS in step $STEP" |
| 106 | fi |
||
| 107 | 2 | Patrice Nadeau | |
| 108 | 1 | Patrice Nadeau | |
| 109 | exit $STATUS |
||
| 110 | 12 | Patrice Nadeau | ``` |
| 111 | 7 | Patrice Nadeau | |
| 112 | 12 | Patrice Nadeau | ### Mon script |
| 113 | 1 | Patrice Nadeau | |
| 114 | > Disponible à "dans la section fichiers":/attachments/download/137 |
||
| 115 | 8 | Patrice Nadeau | |
| 116 | 12 | Patrice Nadeau | #### Utilisation |
| 117 | 6 | Patrice Nadeau | |
| 118 | 1 | Patrice Nadeau | Simplement changer les 4 variables situées au début du script |
| 119 | * 3 pour MySQL |
||
| 120 | * 1 pour la destination de la copie |
||
| 121 | |||
| 122 | Rendre le script exécutable |
||
| 123 | * *chmod +x backup_redmine.sh* |
||
| 124 | |||
| 125 | Et exécuter |
||
| 126 | 4 | Patrice Nadeau | * *./backup_redmine.sh* |
| 127 | 10 | Patrice Nadeau | |
| 128 | h2. Récupération |
||
| 129 | |||
| 130 | Sur un serveur avec Redmine déjà installé |
||
| 131 | # Recopier le répertoire « Redmine » |
||
| 132 | # Remettre la base de données. |
||
| 133 | |||
| 134 | 11 | Patrice Nadeau | Remettre la base de données : |
| 135 | 10 | Patrice Nadeau | <pre><code class="bash"> |
| 136 | # Si la copie a été fait avec mysql |
||
| 137 | 1 | Patrice Nadeau | mysql -u redmine -p < redmine.sql |
| 138 | # Si la copie a été fait avec mysqldump |
||
| 139 | mysqldump -u redmine -p < redmine.sql |
||
| 140 | 10 | Patrice Nadeau | </code></pre> |
| 141 | 11 | Patrice Nadeau | >En supposant : |
| 142 | >* l'usager MySQL/MariaDB se nomme _redmine_ |
||
| 143 | >* la base de donnes se nomme _redmine.mysql_ |