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