Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated backups #100

Merged
merged 4 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ root = true
[*]
# charset = utf-8
end_of_line = lf
# indent_size = 4
# indent_style = space
indent_size = 4
indent_style = space
insert_final_newline = true
# max_line_length = 120
# tab_width = 4
# tab_width = 2

[*.rs]
max_line_length = 100
Expand Down
80 changes: 80 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ serde = { version = "1.0", features = ["derive"], default_features = false }
sysinfo = { version = "0.16.1", default_features = false }
serde_json = "1.0"
daemonize = "0.4"
tar = "0.4"
flate2 = "1.0"


[dev-dependencies]
rand = "0.8.3"
Expand Down
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ ENV PASSWORD "12345"
ENV AUTO_UPDATE "0"
ENV AUTO_UPDATE_SCHEDULE "0 1 * * *"

# Auto Backup Configs
ENV AUTO_BACKUP "0"
ENV AUTO_BACKUP_SCHEDULE "*/15 * * * *"
ENV AUTO_BACKUP_REMOVE_OLD "1"
ENV AUTO_BACKUP_DAYS_TO_LIVE "3"
ENV AUTO_BACKUP_ON_UPDATE "0"
ENV AUTO_BACKUP_ON_SHUTDOWN "0"

COPY --chmod=755 ./src/scripts/*.sh /home/steam/scripts/
COPY --chmod=755 ./src/scripts/entrypoint.sh /entrypoint.sh
Expand Down
67 changes: 54 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,29 @@

### Environment Variables

| Variable | Default | Required | Description |
|----------------------|------------------------|----------|-------------|
| TZ | `America/Los_Angeles` | FALSE | Sets what timezone your container is running on. This is used for timestamps and cron jobs. [Click Here for which timezones are valid.](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) |
| PUID | `1000` | FALSE | Sets the User Id of the steam user. |
| PGID | `1000` | FALSE | Sets the Group Id of the steam user. |
| PORT | `2456` | TRUE | Sets the port your server will listen on. Take not it will also listen on +2 (ex: 2456, 2457, 2458) |
| NAME | `Valheim Docker` | TRUE | The name of your server! Make it fun and unique! |
| WORLD | `Dedicated` | TRUE | This is used to generate the name of your world. |
| PUBLIC | `1` | FALSE | Sets whether or not your server is public on the server list. |
| PASSWORD | `12345` | TRUE | Set this to something unique! |
| AUTO_UPDATE | `0` | FALSE | Set to `1` if you want your container to auto update! This means at 1 am it will update, stop, and then restart your server. |
| AUTO_UPDATE_SCHEDULE | `0 1 * * *` | FALSE | This works in conjunction with `AUTO_UPDATE` and sets the schedule to which it will run an auto update. [If you need help figuring out a cron schedule click here](https://crontab.guru/#0_1_*_*_*) |

| Variable | Default | Required | Description |
|--------------------------|------------------------|----------|-------------|
| TZ | `America/Los_Angeles` | FALSE | Sets what timezone your container is running on. This is used for timestamps and cron jobs. [Click Here for which timezones are valid.](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) |
| PUID | `1000` | FALSE | Sets the User Id of the steam user. |
| PGID | `1000` | FALSE | Sets the Group Id of the steam user. |
| PORT | `2456` | TRUE | Sets the port your server will listen on. Take not it will also listen on +2 (ex: 2456, 2457, 2458) |
| NAME | `Valheim Docker` | TRUE | The name of your server! Make it fun and unique! |
| WORLD | `Dedicated` | TRUE | This is used to generate the name of your world. |
| PUBLIC | `1` | FALSE | Sets whether or not your server is public on the server list. |
| PASSWORD | `12345` | TRUE | Set this to something unique! |
| AUTO_UPDATE | `0` | FALSE | Set to `1` if you want your container to auto update! This means at 1 am it will update, stop, and then restart your server. |
| AUTO_UPDATE_SCHEDULE | `0 1 * * *` | FALSE | This works in conjunction with `AUTO_UPDATE` and sets the schedule to which it will run an auto update. [If you need help figuring out a cron schedule click here]
| AUTO_BACKUP | `0` | FALSE | Set to `1` to enable auto backups. Backups are stored under `/home/steam/backups` which means you will have to add a volume mount for this directory. |
| AUTO_BACKUP_SCHEDULE | `*/15 * * * *` | FALSE | Change to set how frequently you would like the server to backup. [If you need help figuring out a cron schedule click here].
| AUTO_BACKUP_REMOVE_OLD | `1` | FALSE | Set to `0` to keep all backups or manually manage them. |
| AUTO_BACKUP_DAYS_TO_LIVE | `3` | FALSE | This is the number of days you would like to keep backups for. While backups are compressed and generally small it is best to change this number as needed. |
| AUTO_BACKUP_ON_UPDATE | `0` | FALSE | Create a backup on right before updating and starting your server. |
| AUTO_BACKUP_ON_SHUTDOWN | `0` | FALSE | Create a backup on shutdown. |

### Docker Compose

#### Simple

> This is a basic example of a docker compose, you can apply any of the variables above to the `environment` section below but be sure to follow each variables description notes!

```yaml
Expand All @@ -49,6 +56,37 @@ services:
- ./valheim/server:/home/steam/valheim
```

#### Everything but the kitchen sink

```yaml
version: "3"
services:
valheim:
image: mbround18/valheim:latest
ports:
- 2456:2456/udp
- 2457:2457/udp
- 2458:2458/udp
environment:
- PORT=2456
- NAME="Created With Valheim Docker"
- WORLD="Dedicated"
- PASSWORD="Strong! Password @ Here"
- TZ=America/Chicago
- PUBLIC=1
- AUTO_UPDATE=1
- AUTO_UPDATE_SCHEDULE="0 1 * * *"
- AUTO_BACKUP=1
- AUTO_BACKUP_SCHEDULE="*/15 * * * *"
- AUTO_BACKUP_REMOVE_OLD=1
- AUTO_BACKUP_DAYS_TO_LIVE=3
- AUTO_BACKUP_ON_UPDATE=1
- AUTO_BACKUP_ON_SHUTDOWN=1
volumes:
- ./valheim/saves:/home/steam/.config/unity3d/IronGate/Valheim
- ./valheim/server:/home/steam/valheim
- ./valheim/backups:/home/steam/backups
```


### [Odin]
Expand All @@ -58,6 +96,7 @@ This repo has a CLI tool called [Odin] in it! It is used for managing the server
## Versions:

- latest (Stable):
- [#100] Added backup feature to run based on cronjob.
- 1.2.0 (Stable):
- Readme update to include the versions section and environment variables section.
- [#18] Changed to `root` as the default user to allow updated steams User+Group IDs.
Expand All @@ -84,6 +123,7 @@ This repo has a CLI tool called [Odin] in it! It is used for managing the server
- Has a bug in which it does not read passed in variables appropriately to Odin. Env variables are not impacted see [#3].

[//]: <> (Github Issues below...........)
[#100]: https://github.com/mbround18/valheim-docker/pull/100
[#89]: https://github.com/mbround18/valheim-docker/pull/89
[#77]: https://github.com/mbround18/valheim-docker/pull/77
[#53]: https://github.com/mbround18/valheim-docker/pull/53
Expand All @@ -99,5 +139,6 @@ This repo has a CLI tool called [Odin] in it! It is used for managing the server
[//]: <> (Links below...................)
[Odin]: ./docs/odin.md
[Valheim]: https://www.valheimgame.com/
[If you need help figuring out a cron schedule click here]: https://crontab.guru/#0_1_*_*_*

[//]: <> (Image Base Url: https://github.com/mbround18/valheim-docker/blob/main/docs/assets/name.png?raw=true)
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ services:
volumes:
- ./tmp/saves:/home/steam/.config/unity3d/IronGate/Valheim
- ./tmp/server:/home/steam/valheim
- ./tmp/backups:/home/steam/backups
- odin-output:/home/steam/.odin

volumes:
Expand Down
15 changes: 14 additions & 1 deletion src/cli.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ args:
about: Will output the commands that it intends to run.

subcommands:
- init:
- configure:
about: Initializes Odin with its configuration variables.
version: "1.0"
author: mbround18
Expand Down Expand Up @@ -69,3 +69,16 @@ subcommands:
about: Stops Valheim
version: "1.1"
author: mbround18
- backup:
about: Backups the current saves to a specifc location
version: "1.0"
author: mbround18
args:
- INPUT_DIR:
about: Directory to back up
required: true
index: 1
- OUTPUT_FILE:
about: Sets the output file to use
required: true
index: 2
29 changes: 29 additions & 0 deletions src/commands/backup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use clap::ArgMatches;
use flate2::write::GzEncoder;
use flate2::Compression;
use log::{debug, error};
use std::fs::File;
use std::process::exit;

pub fn invoke(args: &ArgMatches) {
let input = args.value_of("INPUT_DIR").unwrap();
let output = args.value_of("OUTPUT_FILE").unwrap();
debug!("Creating archive of {}", input);
debug!("Output set to {}", output);
let tar_gz = match File::create(output) {
Ok(file) => file,
Err(_) => {
error!("Failed to create backup file at {}", output);
exit(1)
}
};
let enc = GzEncoder::new(tar_gz, Compression::default());
let mut tar = tar::Builder::new(enc);
match tar.append_dir_all("saves", input) {
Ok(_) => debug!("Successfully created backup zip at {}", output),
Err(_) => {
error!("Failed to add {} to backup file", input);
exit(1)
}
};
}
File renamed without changes.
3 changes: 2 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod initialize;
pub mod backup;
pub mod configure;
pub mod install;
pub mod start;
pub mod stop;
13 changes: 5 additions & 8 deletions src/commands/start.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::executable::create_execution;
use crate::files::config::{config_file, read_config};
use crate::utils::get_working_dir;
use crate::utils::{create_file, get_working_dir};
use clap::ArgMatches;
use daemonize::Daemonize;
use log::{error, info};
use std::fs::File;
use std::process::exit;

pub fn invoke(args: &ArgMatches) {
Expand All @@ -19,10 +18,8 @@ pub fn invoke(args: &ArgMatches) {
let dry_run: bool = args.is_present("dry_run");
info!("Looking for burial mounds...");
if !dry_run {
let stdout =
File::create(format!("{}/{}", get_working_dir(), "valheim_server.out")).unwrap();
let stderr =
File::create(format!("{}/{}", get_working_dir(), "valheim_server.err")).unwrap();
let stdout = create_file(format!("{}/logs/valheim_server.log", get_working_dir()).as_str());
let stderr = create_file(format!("{}/logs/valheim_server.err", get_working_dir()).as_str());
let daemonize = Daemonize::new()
.working_directory(get_working_dir())
.user("steam")
Expand Down Expand Up @@ -51,8 +48,8 @@ pub fn invoke(args: &ArgMatches) {
});

match daemonize.start() {
Ok(_) => println!("Success, daemonized"),
Err(e) => eprintln!("Error, {}", e),
Ok(_) => info!("Success, daemonized"),
Err(e) => error!("Error, {}", e),
}
} else {
info!(
Expand Down
Loading