If one doesn’t exist yet create a Google Service Account and Google Bucket.
Create an Object Storage bucket to hold your backup repository. Follow the Create a Bucket guide if you do not already have one.
As the pulsys
user install restic with the following command:
sudo apt -y install restic
Run the following command to confirm it is installed correctly:
restic version
-
Configure Restic to use Google Cloud Bucket access file (Step 6 in Service Account Keys Creation and to use the bucket you created in the Before You Begin section of this guide. and substitute
gs:postgres-version-backup:yourpath
below with your own values.export GOOGLE_PROJECT_ID=pul-gcdc export GOOGLE_APPLICATION_CREDENTIALS=~/.restic/pul-gcdc-filename.json restic -r gs:postgres-version-backup:yourpath init
-
Following the prompt, set a password to encrypt your repository’s data. Enter your desired password twice, and be sure to save it!
-
Add the encryption password to Lastpass and add it as a vaulted variable in princeton_ansible. Losing this password will make our backups inaccessible!
The access keyfile, and password are required every time Restic communicates with your repository. To make it easier to work with your repository, create a shell script containing your credentials.
-
To keep your credentials secure, using a text editor, copy and adapt the example script in the home directory of the user who will run the backup's home directory. Run all your Restic scripts as this user. The example uses the
postgres
user and the vim text editor.sudo su - postgres mkdir -p .restic vim .env.restic
Copy and paste the json keyfile’s content and replace and with your own Object Storage account’s keyfile.
### repository on google cloud export GOOGLE_APPLICATION_CREDENTIALS='/var/lib/postgresql/.restic/pul-gcdc-filename.json' export RESTIC_ARCHIVE_REPOSITORY='gs:postgres-version-backup:yourpath' export RESTIC_REPOSITORY=$RESTIC_ARCHIVE_REPOSITORY export RESTIC_PASSWORD_FILE='/var/lib/postgresql/.restic.pwd'
If you are backing up postgreSQL, use the path
/var/lib/postgresql
. If you are backing up mariadb, use the path/home/pulsys
. -
Create a password file to hold your Restic password:
sudo su - postgres vim ~/.restic.pwd
Enter your Rustic password and save the file/
~/.restic.pwd secretpassword # goes into lastpass and ansible vault
For postgresql use the postgresql scripts as a cronjob.
-
Copy all files above in your ~/.restic directory:
sudo su - postgres mkdir -p ~/.restic/log
-
Make all the scripts executable by the
postgres
user:chmod u+x ~/.restic/{common.sh,full_pg_backup.sh,pg_backup.sh,prune.sh}
-
Set Up automated backups by creating a cron job for the
postgres
user with the following:sudo su - postgres crontab -e
Add a line that points to the
full_pg_backup.sh
script
```file
0 5 * * * /var/lib/postgresql/.restic/full_pg_backup.sh
```
For mariadb do the following:
- Copy all the files under the mariadb to the
pulsys
user:mkdir -p ~/.restic/log
- Make all the scripts executable by the
pulsys
user:chmod u+x ~/.restic/maria_backup.sh
- Set Up automated backups by creating a cron job for the
pulsys
user with the following:Add a line that points to thecrontab -e
full_pg_backup.sh
script:
```file
0 5 * * * /home/pulsys/.restic/maria_backup.sh
```
To restore the latest usable postgresql backup from restic, run the following commands:
-
As the postgresql user run the following steps:
sudo su - postgres source .env.restic
Results of postgres-version-backup:yourpath below can be seen if you run
env
as a postgres user in theRESTIC_REPOSITORY
variablerestic -r gs:postgres-version-backup:yourpath -p /var/lib/postgresql/.restic.pwd snapshots
-
Find the hash key of the database you want to restore from and dump it with the following commands. In our example the hash will be
4f155a5e
Results of postgres-version-backup:yourpath below can be seen if you run
env
as a postgres user in theRESTIC_REPOSITORY
variablerestic -r gs:postgres-version-backup:yourpath -p /var/lib/postgresql/.restic.pwd restore 4f155a5e -t /tmp
This will restore your database at
/tmp/postgresql
To restore the latest usable mariadb backup from restic, run the following commands:
- As the
pulsys
user run the following steps:source .env.restic restic -r gs:mariadb-version-backup:yourpath -p /home/pulsys/.restic.pwd snapshots
- Find the hash key of the database you want to restore from and dump it with the following commands. In our example the hash will be
4f155a5e
This will restore your database atrestic -r gs:mariadb-version-backup:yourpath -p /home/pulsys/.restic.pwd restore 4f155a5e -t /tmp
/tmp/mariadb