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